我正在实现一个 spring oauth2 登录机制,该机制充当外部身份提供者的客户端,在没有 pkce 的情况下使用 oauth2 时,我的代码运行良好。 要求已更改,现在我必须在授权请求中实现challenge_code,在令牌请求中实现code_verifier。 查看 Spring 文档以添加 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 使用授权代码和状态在应用程序中再次重定向。 现在应用程序应该向 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}]
我怎样才能看到这个值?我尝试使用请求拦截器,但没有捕获任何内容。 我找不到解决问题的方法,而且我也不明白到底是什么。 请帮助我,我被这个问题困扰了一个星期了:(
根据消息,您遇到客户端身份验证问题。检查应用程序属性中的
client-id
、client-secret
和 client-authentication-method
。
根据文档,你可以做得更简单:
DefaultServerOAuth2AuthorizationRequestResolver.setAuthorizationRequestCustomizer(OAuth2AuthorizationRequestCustomizers.withPkce())