Spring oauth2 客户端中的 PKCE 在对 idp 的 /token 端点执行 POST 请求时未发送正确的有效负载

PKCE in spring oauth2 client for is not sending correct payload when doing POST request to /token endpoint of idp

提问人:Nicolò Frongia 提问时间:11/16/2023 最后编辑:durNicolò Frongia 更新时间:11/16/2023 访问量:21

问:

我正在实现一个 Spring OAuth2 登录机制,该机制充当外部身份提供程序的客户端,在没有 PKCE 的情况下使用 OAuth2 时,我的代码运行良好。 要求已更改,现在我必须在授权请求中实现challenge_code,在令牌请求中实现code_verifier。 在spring文档中查找add pkce是强制性的,覆盖了DefaultServerOAuth2AuthorizationRequestResolver,所以我添加了这个bean

    @Bean
public ServerOAuth2AuthorizationRequestResolver pkceResolver(ReactiveClientRegistrationRepository repo) {
    var resolver = new DefaultServerOAuth2AuthorizationRequestResolver(repo);
    resolver.setAuthorizationRequestCustomizer(OAuth2AuthorizationRequestCustomizers.withPkce());
    return resolver;
}

另一件事是在 SecurityFilterChain 配置方法中添加新的解析程序

    @Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http, ServerLogoutSuccessHandler handler, ServerOAuth2AuthorizationRequestResolver resolver, ReactiveClientRegistrationRepository repository) {

    http.oauth2Login(auth -> auth.authorizationRequestResolver(resolver));
    http.oauth2Login().authenticationFailureHandler(failureHandler());
    http.cors().disable().csrf().disable()
            .authorizeExchange()
            .pathMatchers("/actuator/**", "/login/**","/logout.html")
            .permitAll()
        .and()
            .authorizeExchange()
            .anyExchange()
            .authenticated()
            .and()
            .oauth2ResourceServer()
              .jwt();           
    http.logout().logoutSuccessHandler(handler);

    return http.build();
}   

现在,challenge_code位于对外部 IdP 的请求中。 IdP 在应用程序中再次使用authorization_code和状态重定向。 现在,应用应向 idp 提供程序 /token 终结点发送 POST 请求,但失败并显示此错误消息

    Suppressed: org.springframework.security.oauth2.core.OAuth2AuthenticationException: [invalid_client] FBTOAU229E Confidential clients accessing the token endpoint must authenticate using their registered credentials.

日志显示存在错误的请求

HTTP POST https://exampl.it/mga/sps/oauth/oauth20/token, headers={masked}
Writing form fields [grant_type, client_id, code, redirect_uri, code_verifier] (content masked)
Response 400 BAD_REQUEST, headers={masked}
Decoded [{error_description=FBTOAU229E Confidential clients accessing the token endpoint must authenticate using their registered credentials., error=invalid_client}]

如何查看该值?我尝试使用请求拦截器,但没有捕获到任何东西。 我找不到解决问题的方法,我真的不明白哪个是。 请帮忙,我已经 1 周陷入了这个问题:(

spring-webflux spring-boot-starter-oauth2-客户端

评论


答:

0赞 ch4mp 11/16/2023 #1

根据该消息,您遇到了客户端身份验证问题。选中 ,并在应用程序属性中。client-idclient-secretclient-authentication-method

旁注

根据文档,您可以执行更简单的操作:

DefaultServerOAuth2AuthorizationRequestResolver.setAuthorizationRequestCustomizer(OAuth2AuthorizationRequestCustomizers.withPkce())