Spring boot 2.3.x 和 Spring 5.x 最近添加了对基于WebClient类配置响应式 oauth2 客户端的支持。
我需要客户端凭据授予流程配置
在没有 Mutual TLS/SSL 的情况下进行此调用非常简单。
正常(无 TLS/SSL)配置(@Configuration
)代码提取如下:-
@Bean
public ReactiveOAuth2AuthorizedClientManager authorizedClientManager(
ReactiveClientRegistrationRepository clientRegistrationRepository,
ServerOAuth2AuthorizedClientRepository authorizedClientRepository){
ReactiveOAuth2AuthorizedClientProvider authorizedClientProvider = ReactiveOAuth2AuthorizedClientProviderBuilder.builder()
.clientCredentials()
.build();
DefaultReactiveOAuth2AuthorizedClientManager authorizedClientManager = new DefaultReactiveOAuth2AuthorizedClientManager(clientRegistrationRepository, authorizedClientRepository);
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
return authorizedClientManager;
}
@Bean("testClient")
public WebClient webClient(ReactiveOAuth2AuthorizedClientManager authorizedClientManager,
@Value("${test.client.base.url}") String baseUrl) {
ServerOAuth2AuthorizedClientExchangeFilterFunction oauthFunction = new ServerOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
oauthFunction.setDefaultClientRegistrationId("local");
return WebClient.builder()
.baseUrl(baseUrl)
.filter(oauthFunction)
.build();
}
@Bean
public SecurityWebFilterChain springSecurityFilterChain(ServerHttpSecurity http) {
http.oauth2Client();
return http.build();
}
属性文件
spring.security.oauth2.client.registration.local.authorization-grant-type=client_credentials
spring.security.oauth2.client.registration.local.client-id=client_id
spring.security.oauth2.client.registration.local.client-secret=client_secret
spring.security.oauth2.client.provider.local.token-uri=http://hostname:port/oauth/token
test.client.base.url=http://protected-resource/v1/apis
但是通过 Mutual TLS(客户端证书)调用 oauth2 授权服务器是一件大事。
怎么做?我想与社区分享相同的内容,并在下面自己回答