Java 如何使用SpringSecurity5动态地将OAuth客户端凭据传递给WebClient?
我正在开发一个通用的web客户机包装器,它应该向OAuth2保护的资源发送请求。我的想法是实现一个ClientWrapper,它包含一个Java 如何使用SpringSecurity5动态地将OAuth客户端凭据传递给WebClient?,java,spring,spring-security,Java,Spring,Spring Security,我正在开发一个通用的web客户机包装器,它应该向OAuth2保护的资源发送请求。我的想法是实现一个ClientWrapper,它包含一个WebClient。ClientWrapper提供了访问资源的方法,如getFile(String registrationId)。我想我可以在运行时注册不同的ClientRegistration,并使用WebClient获取它们 到目前为止,我已经注册了一个WebClient,如下所示: ClienRegistration Bean: @Bean pu
WebClient
。ClientWrapper提供了访问资源的方法,如getFile(String registrationId)。我想我可以在运行时注册不同的ClientRegistration
,并使用WebClient
获取它们
到目前为止,我已经注册了一个WebClient
,如下所示:
ClienRegistration Bean:
@Bean
public ClientRegistration clientRegistration() {
ClientRegistration.Builder clientRegestrationBuilder = ClientRegistration
.withRegistrationId(CLIENT_REGISTRATION_ID);
clientRegestrationBuilder.clientId(CLIENT_ID);
clientRegestrationBuilder.clientSecret(CLIENT_SECRET);
clientRegestrationBuilder.tokenUri(TOKEN_ENDPOINT);
clientRegestrationBuilder.authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS);
return clientRegestrationBuilder.build();
}
内存保持器:
@Bean
public ReactiveClientRegistrationRepository clientRegistrations(List<ClientRegistration> clientRegistrations) {
return new InMemoryReactiveClientRegistrationRepository(clientRegistrations);
}
我已将其注入我的客户包装中:
public ClientWrapper(WebClient webClient) {
this.webClient = webClient;
}
@Override
public File getFile(String regId) {
Mono<File> result = webClient.get().uri("https://somepath") //
.attributes(ServerOAuth2AuthorizedClientExchangeFilterFunction
.clientRegistrationId(regId))
.retrieve() //
.bodyToMono(File.class);
return result.block();
}
publicclientwrapper(WebClient-WebClient){
this.webClient=webClient;
}
@凌驾
公共文件getFile(字符串regId){
Mono result=webClient.get().uri(“https://somepath") //
.attributes(ServerOAuth2AuthorizedClient ChangeFilterFunction
.clientRegistrationId(regId))
.retrieve()//
.bodyToMono(File.class);
返回result.block();
}
这样做是非常静态的,我只能使用一个OAuth2客户机(clientid/client secret和token端点)。有了提供注册id的想法,就需要在运行时注册n
ClientRegistration
对象,我认为这是不可能的,而且感觉也不对。。。是否有一种很好的方法可以动态地将“OAuthCredentials”对象传递给用于获取令牌的WebClient
?我有同样的想法…不,我没有进一步调查,仍然坚持OAuth2 RESTTemplate。你能找到解决方案吗?我有同样的想法…不,我没有进一步调查,仍然坚持OAuth2 REST模板
public ClientWrapper(WebClient webClient) {
this.webClient = webClient;
}
@Override
public File getFile(String regId) {
Mono<File> result = webClient.get().uri("https://somepath") //
.attributes(ServerOAuth2AuthorizedClientExchangeFilterFunction
.clientRegistrationId(regId))
.retrieve() //
.bodyToMono(File.class);
return result.block();
}