如何访问控制器中的 oauth2 不透明令牌

How to have access to the oauth2 opaque token in the controller

我有一个 API 使用 OKTA 进行身份验证。我需要 opaque token 以便我可以代表用户访问 OKTA API。我怎样才能访问我的控制器中的不透明令牌?

我找到了 this

我创建了这个 ExchangeFilterFunction:

private ExchangeFilterFunction myExchangeFilterFunction(OAuth2AuthorizedClientService clientService) {
    return new ExchangeFilterFunction() {
        @Override
        public Mono<ClientResponse> filter(ClientRequest request, ExchangeFunction next) {
            Authentication authentication = SecurityContextHolder.getContext().getAuthentication();

            OAuth2AuthenticationToken oauthToken = (OAuth2AuthenticationToken) authentication;

            OAuth2AuthorizedClient client = clientService.loadAuthorizedClient(
                    oauthToken.getAuthorizedClientRegistrationId(),
                    oauthToken.getName());

            String accessToken = client.getAccessToken().getTokenValue();

            ClientRequest newRequest = ClientRequest.from(request)
                    .headers((headers) -> headers.setBearerAuth(accessToken))
                    .build();

            return next.exchange(newRequest);
        }
    };
}