【问题标题】:How to have access to the oauth2 opaque token in the controller如何访问控制器中的 oauth2 不透明令牌
【发布时间】:2021-09-08 03:01:03
【问题描述】:

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

【问题讨论】:

    标签: spring spring-security spring-security-oauth2 okta


    【解决方案1】:

    我找到了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);
            }
        };
    }
    

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 1970-01-01
      • 1970-01-01
      • 2015-01-02
      • 2020-12-12
      • 2021-08-15
      • 1970-01-01
      • 2015-04-05
      • 2018-07-17
      相关资源
      最近更新 更多