2016-10-16 13 views
0

我有兩個獨立的安全域,管理區和前端區域下面的春季安全配置類:如何AUTH成功處理程序分配給多個春季安全領域

@Configuration 
@EnableWebSecurity 
@EnableGlobalMethodSecurity(prePostEnabled=true) 
public class WebSecurityConfig extends WebSecurityConfigurerAdapter { 
    @Autowired 
    private CustomUserDetailsServiceImpl userDetailsService; 

    @Configuration 
    @Order(1) 
    public static class AdminAreaConfiguration extends WebSecurityConfigurerAdapter { 
     @Autowired 
     private AuthSuccessAdmin authSuccessAdmin; 

     @Override 
     protected void configure(HttpSecurity http) throws Exception { 
      http 
       .requestMatcher(new AntPathRequestMatcher("/admin/**")) 
       .csrf().disable() 
       .authorizeRequests() 
        .antMatchers("/admin/login/login.html").permitAll() 
        .antMatchers("/admin/**").hasRole("ADMIN") 
        .anyRequest().authenticated() 
        .and() 
       .formLogin() 
        .loginPage("/admin/login.html") 
        .permitAll() 
        .successHandler(authSuccessAdmin) 
        .and() 
       .logout() 
        .permitAll(); 

     } 
    } 

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

     @Autowired 
     private AuthSuccessFrontend authSuccessFrontend; 

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

      http 
        .requestMatcher(new AntPathRequestMatcher("/**")) 
        .csrf().disable() 
        .authorizeRequests() 
         .antMatchers("/about", "/register").permitAll() 
         .antMatchers("/**").hasRole("USER") 
         .anyRequest().authenticated() 
         .and() 
        .formLogin() 
         .loginPage("/login") 
         .permitAll() 
         .successHandler(authSuccessFrontend) 
         .and() 
        .logout() 
         .permitAll(); 
     } 
    } 
} 

當應用程序啓動時,管理區域的身份驗證成功處理程序由前端區域的身份驗證處理程序覆蓋,後者在第一個之後加載。登錄到管理區時會導致錯誤的重定向(重定向到前端驗證成功處理程序中定義的url)。我如何將disctinct處理程序分配給單獨的配置?

回答

1

這個問題似乎是在RequestMatcher模式。 你用戶應用有RequestMatcher模式「/ **」(指後/什麼,其中將包括路徑/ admin作爲孔)將覆蓋ADMIN RequestMatcher模式/管理/ ** 更改用戶RequestMatcher到/用戶/ **

+0

好吧,我明白了。如果我在兩個區域都有兩種不同的網址格式,它現在就可以運行我最初的理解是,第一個特定規則優先於後面指定的規則。因此,我想首先確保管理區域(/ admin/**),然後對其他所有url(/ **)應用安全性。這是可以實現的嗎?還是我必須定義兩個互斥區域(/ admin/**和/ user/**)? – user1337