2016-05-23 7 views
0

更新:Springセキュリティ、なぜ2番目のWebSecurityConfigurerAdapterが機能しないのですか?

最初のWebSecurityConfigurerAdapterに.antMatcher("/admin/**")を追加すると、今すぐ動作するのですが、それはなぜですか?今最初のWebSecurityConfigurerAdapterの方法を設定

は次のとおりです。

 protected void configure(HttpSecurity http) throws Exception { 
      http 
       .antMatcher("/admin/**")     // <<<<<<<<<<<<<<--- newly added 
        .authorizeRequests() 
        .antMatchers("/admin/**").hasRole("ADM") 
        .and() 
       .formLogin() 
        .loginPage("/admin/login") 
        .permitAll() 
       .logout() 
        .permitAll(); 
     } 

私のサイトには、2つの部分、ユーザー用と管理者用の別に分かれているので、私はそれらのそれぞれのために私のWebアプリで2 WebSecurityConfigurerAdaptersを設定し。このような

@Configuration 
@EnableWebSecurity 
public class WebSecurityConfig { 

    @Configuration 
    @Order(1) 
    public static class ManagerSecurityConfigurationAdapter extends WebSecurityConfigurerAdapter { 
     @Autowired 
     private ManagerDetailsService managerDetailsService; 

     //allow access to static resouces 
     @Override 
     public void configure(WebSecurity web) throws Exception { 
      web.ignoring().antMatchers("/pc/css/**", "/pc/js/**", "/pc/img/**"); 
      web.ignoring().antMatchers("/mobile/css/**", "/mobile/js/**", "/mobile/img/**"); 
     } 

     protected void configure(HttpSecurity http) throws Exception { 
      http 
        .authorizeRequests() 
        .antMatchers("/admin/**").hasRole("ADM") 
        .and() 
       .formLogin() 
        .loginPage("/admin/login") 
        .permitAll() 
       .logout() 
        .permitAll(); 
     } 

     @Override 
     public void configure(AuthenticationManagerBuilder auth) throws Exception { 
      auth 
       .userDetailsService(managerDetailsService); 
     } 
    } 

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

     @Autowired 
     private WeUserDetailsService weUserDetailsService; 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 

      http 
       .authorizeRequests() 
        .antMatchers("/user/**").hasRole("USER") 
        .anyRequest().authenticated() 
        .and() 
       .formLogin() 
        .loginPage("/user/login") 
        .permitAll() 
       .logout() 
        .permitAll(); 

      http.addFilterBefore(new WeAuthenticationFilter(authenticationManager()), BasicAuthenticationFilter.class); 
     } 

     @Override 
     protected void configure(AuthenticationManagerBuilder auth) throws Exception { 
      auth.userDetailsService(weUserDetailsService); 
     } 
    } 
} 

問題は、私が入力した場合には、第2 WebSecurityConfigurerAdapterは、動作していないということである:予想通りブラウザに/管理者は、それは/管理/ログインに私を取るだろう、

私は/ userを入力すると、セキュリティフィルタを渡して、コントローラのアクションに直接行きます。

なぜですか?

+0

ref:http://stackoverflow.com/a/33608459/404145、ほぼ同じ問題、公式のリンクが役に立つかもしれません:http://docs.spring.io/spring-security/site/docs/現在/参照/ htmlsingle /#複数 - httpsecurity – DiveInto

答えて

1

理由は、これは、すべてのURLにマッチする最初のWebSecurityConfigurerAdapterの

 http 
      .authorizeRequests() 

で、 .antMatcher("/admin/**")を追加すると、働くことができる理由です、antMatcherはそれが一致させることができるURLを制限することができます追加、二UserLoginWebSecurityConfigurerAdapterは無用になります。

関連する問題