贝隆

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

如果您需要修改OAuth2请求中的参数名称,您需要配置一个 OAuth2AuthorizedClientProvider bean。您可以使用 DefaultOAuth2AuthorizedClientProvider 类作为基础,并在其上修改所需的参数名称。例如,以下代码片段将 authorizationCode 的参数名称更改为 custom_code

@Configuration
@EnableOAuth2Client
public class OAuth2Configuration {

    @Bean
    public OAuth2AuthorizedClientManager authorizedClientManager(
            ClientRegistrationRepository clientRegistrationRepository,
            OAuth2AuthorizedClientRepository authorizedClientRepository) {

        DefaultOAuth2AuthorizedClientManager authorizedClientManager =
                new DefaultOAuth2AuthorizedClientManager(
                        clientRegistrationRepository, authorizedClientRepository);

        DefaultOAuth2AuthorizedClientProvider authorizedClientProvider =
                new DefaultOAuth2AuthorizedClientProvider();

        authorizedClientProvider.setAuthorizationCodeTokenResponseClient(
                new OAuth2AuthorizationCodeGrantRequestEntityConverter() {
                    @Override
                    public RequestEntity<?> convert(OAuth2AuthorizationCodeGrantRequest request) {
                        HttpHeaders headers = new HttpHeaders();
                        headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
                        MultiValueMap<String, String> parameters = new LinkedMultiValueMap<>();
                        parameters.add("client_id", request.getClientId());
                        parameters.add("custom_code", request.getAuthorizationCode());
                        ...
                        // 添加其它参数
                        ...
                        return new RequestEntity<>(parameters, headers, HttpMethod.POST, request.getAccessTokenUri());
                    }
                });

        authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);

        return authorizedClientManager;
    }
}

 

这个例子使用 DefaultOAuth2AuthorizedClientProviderDefaultOAuth2AuthorizedClientManager 类。然后,它使用匿名内部类重写了 OAuth2AuthorizationCodeGrantRequestEntityConverter 类。在 convert() 方法中,我们可以设置任何要发送到授权服务器的参数,包括更改 authorizationCode 参数的名称为 custom_code

 

https://blog.csdn.net/friendlytkyj/article/details/130439868

 

https://zhuanlan.zhihu.com/p/620506340

https://zhuanlan.zhihu.com/p/620499977

 

 

https://mp.weixin.qq.com/s?__biz=MzUzMzQ2MDIyMA==&mid=2247487544&idx=1&sn=0152c6700c18d765a6cc4183b18d3b2e&chksm=faa2f5abcdd57cbde4dfd65bfacfd945ae70ca93838b0e41b34debd386a1b1f63e23258caa31&cur_album_id=1319904585363980289&scene=189#wechat_redirect

posted on 2023-06-04 19:51  贝隆  阅读(175)  评论(0编辑  收藏  举报