我创建了一个rest-application,它接收来自Client的请求并转到资源服务器,同时使用client_Credentials通过OAuth2进行授权。但是,授权服务器(而不是默认请求)也会等待头部中的UserId,该头部必须填充来自Client请求的数据。
我使用这个bean实现:
@Configuration
public class WebClientConfig {
@Bean
OAuth2AuthorizedClientManager authorizedClientManager(
ClientRegistrationRepository clientRegistrationRepository,
OAuth2AuthorizedClientRepository authorizedClientRepository) {
OAuth2AuthorizedClientProvider authorizedClientProvider =
OAuth2AuthorizedClientProviderBuilder.builder()
.clientCredentials()
.build();
DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(
clientRegistrationRepository, authorizedClientRepository);
authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
return authorizedClientManager;
}
}
我通过OAuth2 AutorizeRequest请求访问令牌:
OAuth2AutorizeRequest oAuth2AutorizeRequest = OAuth2AutorizeRequest.withClientRegistrationId("test").principal("test").build();
OAuth2AccessToken token = authorizedClientManager.authorize(oAuth2AutorizeRequest).getAccessToken;
如何在发送POST请求时将UserId头添加到Authorization头,该请求将从Client请求中填充?
示例:
POST /oauth2/token HTTP/1.1
Host: auth-server:1111
Content-Type: application/x-www-form-urlencoded
Authorization: Basic bWVzc2FnaW5nLWNsaWVudDpzZWNyZXQ=
UserId : naW5nLbWVzcVudDpzZ
grant_type=client_credentials&scope=message.read
1条答案
按热度按时间fdbelqdn1#
您可以尝试使用这样的
@Service
(前提是您在Sping Boot 属性中有一个标记为“machin”的OAuth2客户端注册):用法如下: