簡體   English   中英

如何將身份驗證成功處理程序分配給多個Spring安全領域

[英]How to assign auth success handler to multiple spring security realms

我具有以下用於兩個單獨的安全領域(管理區域和前端區域)的Spring安全配置類:

@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();
        }
    }
}

啟動應用程序時,管理區域的身份驗證成功處理程序將被前端區域的身份驗證處理程序覆蓋,前端區域的身份驗證處理程序將在第一個之后加載。 登錄到管理區域時,這將導致錯誤的重定向(重定向到前端auth成功處理程序中定義的url)。 如何為單獨的配置分配不同的處理程序?

問題似乎在RequestMatcher模式中。 您的USER應用具有RequestMatcher模式'/ **'(表示/之后的任何內容,也將包含路徑/ admin),它將覆蓋您的ADMIN RequestMatcher模式/ admin / **。將用戶RequestMatcher更改為/ user / **

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM