【问题标题】:Configuring Multiple Spring Security [duplicate]配置多个 Spring Security [重复]
【发布时间】:2021-12-30 10:44:54
【问题描述】:

我有以下配置:

@Configuration
@EnableWebSecurity
public class SecurityConfig {

    @Configuration
    @Order(1)
    public static class SamlConfig extends WebSecurityConfigurerAdapter {
        
        @Value("${enable_csrf}")
        private Boolean enableCsrf;

        @Autowired
        private SamlUserService samlUserService;

        public SamlWebSecurityConfig() {
            super();
        }

        @Override
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .antMatchers("/secure/sso").permitAll()
                    .antMatchers("/saml/**").permitAll()
                    .anyRequest().authenticated()
                    .and()
                .apply(saml())
                    .userDetailsService(samlUserService)
                    .serviceProvider()
                    .keyStore()
                    .storeFilePath("path")
                    .password("password")
                    .keyname("alias")
                    .keyPassword("password")
                    .and()
                    .protocol("https")
                    .hostname(String.format("%s:%s","localhost", "8080"))
                    .basePath("/")
                    .and()
                    .identityProvider()
                    .metadataFilePath("metadata");
            if (!enableCsrf) {
                http.csrf().disable();
            }
        }
    }

    @Configuration
    @Order(2)
    public static class BasicConfig extends WebSecurityConfigurerAdapter {
        
        public BasicWebSecurityConfig() {
            super();
        }
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .antMatchers("/secure/basic").permitAll()
                    .anyRequest().authenticated();
            if (!enableCsrf) {
                http.csrf().disable();
            }
        }
    }

这适用于 SAML,但基本登录返回错误:403 禁止

我用这个修改了BasicConfig,SAML 不再工作,但基本身份验证工作。所有端点都用于 SAML 和基本身份验证,只是登录页面不同。

public static class BasicConfig extends WebSecurityConfigurerAdapter {
        
        public BasicWebSecurityConfig() {
            super();
        }
        protected void configure(HttpSecurity http) throws Exception {
            http
                .authorizeRequests()
                    .antMatchers("/secure/basic").permitAll()
                    .antMatchers("/**").permitAll()
                    .anyRequest().authenticated();
            if (!enableCsrf) {
                http.csrf().disable();
            }
        }
    }

由于某些原因,它有时有效,有时无效。我也试过修改@Order,还是不行。

【问题讨论】:

    标签: spring-security spring-saml


    【解决方案1】:

    在 Spring Security 中,有两个相似但做事完全不同的东西,requestMatchers().antMatchers()authorizeRequests().antMatchers()

    requestMatchers 告诉HttpSecurity 仅在提供的RequestMatcher 匹配时才调用SecurityFilterChain

    authorizeRequests 允许基于 HttpServletRequest 使用 RequestMatcher 实现来限制访问。

    在您的情况下,您有两个 SecurityFilterChains。但是只有最高优先级的那个被调用,这是因为你没有给它任何requestMatchers,因此它会匹配每个请求。并且每个请求只调用一个SecurityFilterChain,因此它不会调用下一个。

    因此,您应该通知requestMatchers 进行配置,如下所示:

    http
            .requestMatchers((requests) -> requests
                    .antMatchers("/secure/sso", "/saml/**")
            )
            .authorizeRequests()
            .antMatchers("/secure/sso").permitAll()
            .antMatchers("/saml/**").permitAll()
            .anyRequest().authenticated()
            ...
    
    
    http
             .requestMatchers((requests) -> requests
                    .antMatchers("/secure/basic", "/**")
             )
             .authorizeRequests()
             .antMatchers("/secure/basic").permitAll()
             .anyRequest().authenticated();
    

    【讨论】:

    • 它不起作用,500 - 内部服务器错误。我添加了 .requestMatchers().antMatchers("...","....").and().authorizeRequests()......
    • 提高你的日志级别来跟踪和查看堆栈跟踪
    猜你喜欢
    • 2012-06-04
    • 1970-01-01
    • 2020-08-24
    • 2015-02-18
    • 1970-01-01
    • 2019-06-13
    相关资源
    最近更新 更多