2016-04-28 22 views
0

Ich habe mit Feder MVC App gearbeitet. Gehen Sie, um Sicherheit hinzuzufügen. Alles funktioniert korrekt.Frühling 4 MVC + Sicherheitsproblem

gemäß der doc http://docs.spring.io/autorepo/docs/spring-security/4.0.4.RELEASE/reference/htmlsingle/#abstractsecuritywebapplicationinitializer-with-spring-mvc

hinzugefügt nächste Umsetzung.

  1. MessageSecurityWebApplicationInitializer
  2. MessageWebApplicationInitializer
  3. SecurityConfig

Login-Formular funktioniert.

Nach Erfolg Genehmigung haben - HTTP-Status 404-

Auch kann ich Methode debuggen in AppController nach der Autorisierung, ich meine nächsten Methode public String listPatients(ModelMap model).

Warum habe ich 404? Was soll ich reparieren?

public class MessageSecurityWebApplicationInitializer 
     extends AbstractSecurityWebApplicationInitializer { 
} 


public class MessageWebApplicationInitializer extends AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class[]{AppConfig.class}; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class[]{AppConfig.class}; 
    } 


    @Override 
    protected String[] getServletMappings() { 
     return new String[] {"/*"}; 
    } 

} 

@Configuration 
@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception { 
     auth.inMemoryAuthentication().withUser("user").password("user").roles("USER"); 
    } 

    protected void configure(HttpSecurity http) throws Exception { 
     http 
       .authorizeRequests() 
       .anyRequest().authenticated() 
       .and() 
       .formLogin() 
       .and() 
       .httpBasic(); 
    } 

} 

@Controller 
@RequestMapping("/") 
public class AppController { 

    public static final String jsonPage = "json"; 
    @RequestMapping(value = {"/"}, method = RequestMethod.GET) 
    public String listPatients(ModelMap model) { 
     model.addAttribute("json", "test"}"); 
     return jsonPage; 
    } 

BR!

+0

Ich empfehle auf würde Drehen mit Spring Security Anmeldung + Frühling M VC und sehen, was es macht. – Kurtymckurt

+1

Für Starter aufhören, Ihre Anwendung zweimal zu laden. Ihre 'AppConfig' lädt die' AppConfig' sowohl für den Root- als auch den Servlet-Kontext und dupliziert damit effektiv Ihre gesamte Anwendung. Teilen Sie Ihre Konfiguration im Root (Services, Repositories, Infrastruktur) und im Servlet-Kontext (Web-bezogene Dinge wie Controller etc.). –

+0

Ich stimme @ M.Deinum zu. Wenn Sie die Protokolle beim Start überprüfen, können Sie sehen, dass Sie zwei Kontexte erstellen. Tun Sie das nicht und teilen Sie Ihre Konfigurationen (überprüfen Sie meine Antwort unten) –

Antwort

0

Das ist, was ich habe (in Betrieb):

AppInitializer

public class AppInitializer extends 
     AbstractAnnotationConfigDispatcherServletInitializer { 

    @Override 
    protected Class<?>[] getRootConfigClasses() { 
     return new Class[] { 
       SpringRootConfig.class, 
       DatabaseConfig.class, 
       SecurityConfig.class, 
       SecurityInitializer.class, 
       WsdlMarshallerConfig.class 
     }; 
    } 

    @Override 
    protected Class<?>[] getServletConfigClasses() { 
     return new Class[] { SpringWebConfig.class }; 
    } 

    @Override 
    protected String[] getServletMappings() { 
     return new String[] { "/" }; 
    } 

} 

SecurityInitializer

public class SecurityInitializer extends AbstractSecurityWebApplicationInitializer { 
} 

SecurityConfig

@EnableWebSecurity 
public class SecurityConfig extends WebSecurityConfigurerAdapter { 

    @Autowired 
    private DataSource dataSource; 

    @Override 
    protected void configure(AuthenticationManagerBuilder auth) 
      throws Exception { 
     // @formatter:off 
     auth 
      .jdbcAuthentication() 
       .dataSource(dataSource) 
        .usersByUsernameQuery("SELECT username, password, enabled FROM users WHERE username = ?") 
        .authoritiesByUsernameQuery("SELECT username, role FROM user_roles WHERE username = ?"); 
     // @formatter:on 
    } 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     // @formatter:off 
     http 
      .authorizeRequests() 
       .antMatchers("/resources/**").permitAll() 
       .antMatchers("/login").permitAll() 
       .anyRequest().authenticated() 
       .and() 
      .formLogin() 
       .loginPage("/login") 
       .and() 
      .csrf() 
       .disable(); 
     // @formatter:on 
    } 

}