java - 对于同一路径,JWT 身份验证可回退到 SAML2

标签 java spring-boot authentication spring-security spring-security-saml2

我正在使用 spring-security-saml2-service-provider 在我的一个 Spring Boot 应用程序中进行身份验证,并且我正在使用自定义 JwtAuthorizationFilter (通过http 身份验证 header )在不同的 Spring Boot 应用程序中。

它们各自都能完美工作。

现在我需要编写一个使用它们的 Spring Boot 应用程序。如果 JWT token 可用(身份验证 header ),则使用 JwtAuthorizationFilter,否则使用 saml2Login

SAML2 配置如下所示:(没有过滤器,只有 saml2Login)

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf()
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
            .antMatcher("/**").authorizeRequests()
            .antMatchers("/saml2/service-provider-metadata/**").permitAll()
            .antMatchers("/**").authenticated().and()

            // use SAML2
            .saml2Login()
            .addObjectPostProcessor(new ObjectPostProcessor<OpenSamlAuthenticationProvider>() {
                public <O extends OpenSamlAuthenticationProvider> O postProcess(O samlAuthProvider) {
                    samlAuthProvider.setAuthoritiesExtractor(authoritiesExtractor());
                    samlAuthProvider.setAuthoritiesMapper(authoritiesMapper());
                    return samlAuthProvider;
                }
            })
        ;
    }

JWT 配置如下所示:

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf()
            .csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
            .antMatcher("/**").authorizeRequests()
            .antMatchers("/**").authenticated().and()

            // use JWT
            .addFilter(new JwtAuthorizationFilter(authenticationManager(), jwtUtil))
        ;
    }

我想我需要类似 JwtOrSaml2AuthenticationFilter 的东西,但不知道该怎么做。

最佳答案

解决办法是

  1. 使用 @Order 复制配置并
  2. 在 addFilter 之前设置基于 header 的 requestMatcher

    @EnableWebSecurity
    public class SecurityConfiguration {
        @Order(100) // lower number = higher priority
        @Configuration
        @RequiredArgsConstructor
        public static class AppSecurityJWT extends WebSecurityConfigurerAdapter {
            final JWTUtil jwtUtil;
    
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http.csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
                    .antMatcher("/**").authorizeRequests()
                    .antMatchers("/saml2/service-provider-metadata/**", "/idm-app/**").permitAll()
                    .antMatchers("/**").authenticated().and()
    
                    // This configuration will only be active if the Authorization header is present in the request
                    .requestMatcher(new RequestHeaderRequestMatcher("Authorization")).addFilter(new JwtAuthorizationFilter(authenticationManager(), jwtUtil))
                ;
            }
        }
    
        @Order(101)
        @Configuration
        @RequiredArgsConstructor
        public static class AppSecuritySAML2 extends WebSecurityConfigurerAdapter {
            @Override
            protected void configure(HttpSecurity http) throws Exception {
                http
                    .csrf().csrfTokenRepository(CookieCsrfTokenRepository.withHttpOnlyFalse()).and()
                    .antMatcher("/**").authorizeRequests()
                    .antMatchers("/saml2/service-provider-metadata/**", "/idm-app/**").permitAll()
                    .antMatchers("/**").authenticated().and()
    
                    // This whole configuration will only be active, if the previous (100) didn't match
                    .saml2Login()
                    //...
            ;
        }
    }
    

关于java - 对于同一路径,JWT 身份验证可回退到 SAML2,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61411208/

相关文章:

java - 如何使用 Selenium 或 Selenium 选择 md-autocomplete 选项?

asp.net-mvc - 使用 ServiceStack.MVC 进行身份验证和授权

java - jlist get 和 set 选择损坏?

java - Java套接字客户端未检测到服务器消息

spring - Spring Data REST 中具有多对多关系的 POST 请求

java - Spring Boot 数据库只初始化一次

java - Log4j 似乎不适用于 Spring Boot

javascript - 对 Spotify Web API 的身份验证请求被拒绝

Python-手动浏览器登录后从urllib2恢复网络 session

java - 在 Eclipse 中创建 Web 服务