如何在 Webflux 功能端点中访问 OAuth2AuthorizedClient?
How to access OAuth2AuthorizedClient in Webflux Functional Endpoints?
使用经典的 MVC 风格的 RestController 很容易获得 OAuth2AuthorizedClient,我需要做的就是:
@GetMapping("/foo")
public Foo getFoo(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient client){
return new Foo(client.getPrincipalName());
}
但是,对于 Webflux 的功能端点,我如何才能以类似的方式访问 OAuth2AuthorizedClient?
编辑:
为了说得更清楚,我知道反应式方法与非反应式方法的工作原理相同。我很好奇的是如何从 Webflux 的功能端点访问 OAuth2AuthorizedClient:
PersonRepository repository = ...
PersonHandler handler = new PersonHandler(repository);
RouterFunction<ServerResponse> route = route()
.GET("/person/{id}", accept(APPLICATION_JSON), handler::getPerson)
.GET("/person", accept(APPLICATION_JSON), handler::listPeople)
.POST("/person", handler::createPerson)
.build();
public class PersonHandler {
// ...
public Mono<ServerResponse> listPeople(ServerRequest request) {
// ...
}
public Mono<ServerResponse> createPerson(ServerRequest request) {
// ...
}
public Mono<ServerResponse> getPerson(ServerRequest request) {
// ...
}
}
比如createPerson方法中,如何访问OAuth2AuthorizedClient?
看起来它的工作原理与非反应式方法相同。
请看:
找到解决办法了!
@Component
@RequiredArgsConstructor
public class OAuth2Utils {
private final ServerOAuth2AuthorizedClientRepository authorizedClientRepository;
public Mono<OAuth2AuthorizedClient> extractOAuth2AuthorizedClient(ServerRequest request) {
return request.principal()
.filter(principal -> principal instanceof OAuth2AuthenticationToken)
.cast(OAuth2AuthenticationToken.class)
.flatMap(auth -> authorizedClientRepository.loadAuthorizedClient(auth.getAuthorizedClientRegistrationId(), auth, request.exchange()));
}
}
使用经典的 MVC 风格的 RestController 很容易获得 OAuth2AuthorizedClient,我需要做的就是:
@GetMapping("/foo")
public Foo getFoo(@RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient client){
return new Foo(client.getPrincipalName());
}
但是,对于 Webflux 的功能端点,我如何才能以类似的方式访问 OAuth2AuthorizedClient?
编辑:
为了说得更清楚,我知道反应式方法与非反应式方法的工作原理相同。我很好奇的是如何从 Webflux 的功能端点访问 OAuth2AuthorizedClient:
PersonRepository repository = ...
PersonHandler handler = new PersonHandler(repository);
RouterFunction<ServerResponse> route = route()
.GET("/person/{id}", accept(APPLICATION_JSON), handler::getPerson)
.GET("/person", accept(APPLICATION_JSON), handler::listPeople)
.POST("/person", handler::createPerson)
.build();
public class PersonHandler {
// ...
public Mono<ServerResponse> listPeople(ServerRequest request) {
// ...
}
public Mono<ServerResponse> createPerson(ServerRequest request) {
// ...
}
public Mono<ServerResponse> getPerson(ServerRequest request) {
// ...
}
}
比如createPerson方法中,如何访问OAuth2AuthorizedClient?
看起来它的工作原理与非反应式方法相同。
请看:
找到解决办法了!
@Component
@RequiredArgsConstructor
public class OAuth2Utils {
private final ServerOAuth2AuthorizedClientRepository authorizedClientRepository;
public Mono<OAuth2AuthorizedClient> extractOAuth2AuthorizedClient(ServerRequest request) {
return request.principal()
.filter(principal -> principal instanceof OAuth2AuthenticationToken)
.cast(OAuth2AuthenticationToken.class)
.flatMap(auth -> authorizedClientRepository.loadAuthorizedClient(auth.getAuthorizedClientRegistrationId(), auth, request.exchange()));
}
}