Spring Security .permitAll() 不适用于 OAuth2ResourceServer

问题描述 投票:0回答:1

我已经设置了一个Spring Boot应用程序,并且刚刚开始添加身份验证。我正在使用 JWT 令牌(来自 Amazon Cognito)进行验证。

我希望某些端点(例如来自登录页面的请求)不需要有效的 JWT 令牌。当我拥有有效的 JWT 令牌时,身份验证就会成功,因此我知道令牌验证正在工作。但是,对于某些端点(例如“启动登录身份验证”),不应需要令牌,但它仍然需要。当向此端点发出请求时,我收到 401 错误,错误如下:

May  3 13:47:37 ip-172-31-30-116 web[1423242]: 2024-05-03T13:47:37.061Z DEBUG 1423242 --- [nio-5000-exec-5] o.s.s.oauth2.jwt.JwtTimestampValidator   : Jwt expired at 2024-05-03T12:35:46Z
May  3 13:47:37 ip-172-31-30-116 web[1423242]: 2024-05-03T13:47:37.062Z DEBUG 1423242 --- [nio-5000-exec-5] o.s.s.o.s.r.a.JwtAuthenticationProvider  : Failed to authenticate since the JWT was invalid

虽然这正确地使令牌无效,但由于在 SecurityConfig 中使用了 .permitAll(),因此不应检查此端点的令牌。

我在网上读到身份验证先于授权,因此在检查authorizeHttpRequests之前检查JWT令牌。这可能是真的,但我不知道如何将 .permitAll() 添加到 oauth2ResourceServer。

我添加了以下安全配置:

@EnableWebSecurity
@Configuration
public class SecurityConfig {
    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        System.out.println("entering the SecurityChainFilter");
        http
                .authorizeHttpRequests((authorize) -> authorize
                        .requestMatchers("/initiate-sign-in-auth").permitAll()
                        .anyRequest().permitAll()
                )
                .csrf(AbstractHttpConfigurer::disable)
                .oauth2ResourceServer(oauth2 -> oauth2.jwt(Customizer.withDefaults()));

        return http.build();
    }
}

以下是我添加到 pom.xml 文件中的安全依赖项:

<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-resource-server</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-oauth2-jose</artifactId>
        </dependency>

这是我尝试调用的端点:

@PostMapping("/initiate-sign-in-auth")
    public InitiateAuthRequestResponse initiateSignInAuth(@RequestBody InitiateAuthRequestRequest request) throws JsonProcessingException, StripeException {
        System.out.printf("Starting initiateSignInAuth request: %s%n", om.writeValueAsString(request));
        InitiateAuthRequestResponse response = initiateSignInAuthExecutor.execute(request);
        System.out.printf("InitiateSignInAuthResponse: %s%n", om.writeValueAsString(response));

        return response;
    }

我已经被困在这个问题上几天了,所以任何帮助将不胜感激!谢谢你

我尝试删除 oauth2ResourceServer,当我执行 .permitAll() 按预期工作时。所以我知道问题与验证 JWT 令牌的 oauth2ResourceServer 有关。我也尝试过禁用 CSRF 但问题仍然存在。

我还尝试将 .permitAll() 添加到 .anyRequest() 以防端点配置出现问题,但这也无法解决问题。所以我知道这不是因为我正在使用的端点字符串。

spring spring-boot spring-security oauth-2.0
1个回答
0
投票

资源服务器过滤器链将始终拒绝无效令牌(错误的发行者、过期、错误的签名、错误的受众或令牌验证器配置为检查的任何内容)。在评估访问控制之前会抛出 esception,因此 不要将令牌发送到配置有

permitAll()
的端点。

如果您确实想接受某些端点的垃圾令牌,请将其从资源服务器安全过滤器链中排除。您可以创建另一个具有更高优先级的过滤器链和一个仅拦截此不安全端点的安全匹配器。

© www.soinside.com 2019 - 2024. All rights reserved.