2015-11-09 7 views
6

Ich versuche, Spring Security zu verwenden, und ich habe einen Anwendungsfall, bei dem ich verschiedene Anmeldeseiten und verschiedene URLs sichern möchte.Spring Security: Mehrere HTTP Config funktioniert nicht

Hier ist meine Konfiguration:

@Configuration 
@Order(1) 
public static class ProviderSecurity extends WebSecurityConfigurerAdapter{ 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/", "/home").permitAll() 
       .antMatchers("/admin/login").permitAll() 
       .antMatchers("/admin/**").access("hasRole('BASE_USER')") 
       .and() 
      .formLogin() 
       .loginPage("/admin/login").permitAll() 
       .defaultSuccessUrl("/admin/home") 
       .failureUrl("/admin/login?error=true").permitAll() 
       .usernameParameter("username") 
       .passwordParameter("password") 
       .and() 
      .csrf()      
       .and() 
      .exceptionHandling().accessDeniedPage("/Access_Denied");    
    } 
} 


@Configuration 
@Order(2) 
public static class ConsumerSecurity extends WebSecurityConfigurerAdapter { 

    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .authorizeRequests() 
       .antMatchers("/consumer/login").permitAll() 
       .antMatchers("/consumer/**").access("hasRole('BASE_USER')") 
       .anyRequest().authenticated() 
       .and() 
      .formLogin() 
       .loginPage("/consumer/login").permitAll() 
       .defaultSuccessUrl("/consumer/home") 
       .failureUrl("/consumer/login?error=true").permitAll() 
       .usernameParameter("username") 
       .passwordParameter("password") 
       .and().csrf()     
       .and() 
      .exceptionHandling().accessDeniedPage("/Access_Denied"); 
    } 
} 

Diese Klassen sind innere Klassen einer anderen Klasse MultipleHttpSecurityConfig die Annotation @EnableWebSecurity hat.

Die Sicherheit für admin/** funktioniert gut, aber keine der consumer/** Seiten sind gesichert, keine Umleitung geschieht für die Anmeldeseite. Ich habe nach anderen Antworten gesucht, aber keine funktionierte.

Antwort

8

Blick auf die Spring Security Reference:

@EnableWebSecurity 
public class MultiHttpSecurityConfig { 
    @Autowired 
    public void configureGlobal(AuthenticationManagerBuilder auth) { 1 
     auth 
      .inMemoryAuthentication() 
       .withUser("user").password("password").roles("USER").and() 
       .withUser("admin").password("password").roles("USER", "ADMIN"); 
    } 

    @Configuration 
    @Order(1)              2 
    public static class ApiWebSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
       .antMatcher("/api/**")        3 
       .authorizeRequests() 
        .anyRequest().hasRole("ADMIN") 
        .and() 
       .httpBasic(); 
     } 
    }  

    @Configuration             4 
    public static class FormLoginWebSecurityConfigurerAdapter extends WebSecurityConfigurerAdapter { 

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

1 Configure-Authentifizierung als normale

2 eine Instanz von WebSecurityConfigurerAdapter erstellen, die angeben @Order enthält, die WebSecurityConfigurerAdapter sollte zuerst in Betracht gezogen werden.

3 Die http.antMatcher heißt es, dass diese HttpSecurity nur auf URLs anwendbar sein wird, die mit /api/

4 eine weitere Instanz von WebSecurityConfigurerAdapter erstellen starten. Wenn die URL nicht mit /api/ beginnt, wird diese Konfiguration verwendet. Diese Konfiguration wird nach ApiWebSecurityConfigurationAdapter berücksichtigt, da sie einen @Order-Wert nach 1 aufweist (keine @Order Standardvorgaben).

Ihre zweite Konfiguration wird nicht verwendet, da Ihre erste Konfiguration /** entspricht. Und Ihre erste Konfiguration beschränkt nur /admin/**.

8

Ihre erste WebSecurityConfigurerAdapter ‚s

http 
      .authorizeRequests() 

passt alle URLs, begrenzen sie nur URLs beginnen mit /admin von antMatcher mit:

@Configuration 
@Order(1) 
public static class ProviderSecurity extends WebSecurityConfigurerAdapter{ 
    @Override 
    protected void configure(HttpSecurity http) throws Exception { 
     http 
      .antMatcher("/admin/**") 
       .authorizeRequests() 
       .antMatchers("/admin/login").permitAll() 
       .antMatchers("/admin/**").access("hasRole('BASE_USER')") 
       .and() 

       ... 
Verwandte Themen