如何在Spring Webflow中实例化webClient时设置一次访问令牌? [英] How to set the access token once during the instanciation of the webClient in spring webflux?
本文介绍了如何在Spring Webflow中实例化webClient时设置一次访问令牌?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我尝试在Spring WebFlux中使用带有OAuth2的WebClient。我从URL访问令牌中获取一个令牌,并将其设置到Web客户端。但我不喜欢在其他安全端点的每次调用中获取此访问令牌。意味着我只想在Web客户端实例化期间和访问令牌过期时第一次获取它。
以下是我使用的代码:
@Configuration
public class OauthEmployeConfig{
/**
** ... String baseUrl, String accessUrl for the access token url
**/
@Bean
public WebClient webClient(UserRegistration userRegistr) {
ClientRequest clientRequest = ClientRequest
.create(HttpMethod.POST, URI.create(accessUrl))
.header(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_FORM_URLENCODED_VALUE)
.headers(headers -> headers.setBasicAuth(userRegistr.getClientId(), userRegistr.getClientSecret()))
.body(BodyInserters.fromFormData("grant_type", userRegistr.getAuthorizGrantType())
.with("scope", userRegistr.getScope().replaceAll(",", "")))
.build();
return WebClient.builder()
.baseUrl(baseUrl)
.defaultHeader(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_JSON_VALUE)
.defaultHeader(HttpHeaders.ACCEPT, MediaType.APPLICATION_JSON_VALUE)
.filter((request, next) -> next.exchange(clientRequest)
.flatMap(response -> response.body(org.springframework.security.oauth2.core.web.reactive.function.OAuth2BodyExtractors.oauth2AccessTokenResponse()))
.map(accessToken -> accessToken.getAccessToken().getTokenValue())
.map(token -> setBearer(request, token))
.flatMap(next::exchange))
.filter(logRequest())
.filter(handleResponseError())
.build();
}
private ClientRequest setBearer(ClientRequest request, String token) {
return ClientRequest.from(request)
.header("Authorization", "Bearer " + token).build();
}
private static ExchangeFilterFunction handleResponseError() {
return ExchangeFilterFunction.ofResponseProcessor(
response -> response.statusCode().isError()
? response.bodyToMono(String.class)
.flatMap(errorBody -> Mono.error(new RuntimeException(errorBody, response.statusCode())))
: Mono.just(response));
}
private static ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
clientRequest.headers().forEach((name, values) -> values.forEach(value -> LOG.info("{}={}", name, value)));
return Mono.just(clientRequest);
});
}
}
推荐答案
我遵循了this toturial和spring doc,并且我必须更改我的代码。
所以我的代码如下:
application.properties
spring.security.oauth2.client.registration.chris.authorization-grant-type=client_credentials
spring.security.oauth2.client.registration.chris.client-id=chris-client-id
spring.security.oauth2.client.registration.chris.client-secret=chris-secret
spring.security.oauth2.client.provider.chris.token-uri=http://localhost:8085/oauth/token
配置类:
@Configuration
public OauthEmployeConfig {
@Bean
WebClient webClient(ReactiveClientRegistrationRepository clientRegistrations) {
ServerOAuth2AuthorizedClientExchangeFilterFunction oauth =
new ServerOAuth2AuthorizedClientExchangeFilterFunction(
clientRegistrations,
new UnAuthenticatedServerOAuth2AuthorizedClientRepository());
oauth.setDefaultClientRegistrationId("chris");
oauth.setDefaultOAuth2AuthorizedClient(true);
return WebClient.builder()
.filter(oauth)
.filter(logRequest())
.filter(handleResponseError())
.build();
}
private static ExchangeFilterFunction handleResponseError() {
return ExchangeFilterFunction.ofResponseProcessor(
response -> response.statusCode().isError() ?
response.bodyToMono(String.class)
.flatMap(errorBody -> Mono.error(new RunTimeException(errorBody, response.statusCode()))) :
Mono.just(response));
}
private static ExchangeFilterFunction logRequest() {
return ExchangeFilterFunction.ofRequestProcessor(clientRequest -> {
// To log the headers details like Token ...
clientRequest.headers().forEach((name, values) -> values.forEach(value -> LOGGER.info("{}={}", name, value)));
return Mono.just(clientRequest);
});
}
}
通过WebClient进行REST调用:
...
webClient.get()
.uri("http://localhost:8084/retrieve-resource")
.retrieve()
...
此方法当然会在访问令牌到期后通过刷新令牌更新访问令牌。
这篇关于如何在Spring Webflow中实例化webClient时设置一次访问令牌?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文