有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

JavaSpringWebFlux和KeyClope JWTRESTAPI

我正在Webflux rest api上构建一个userinfo端点,如何访问通过rest调用中的授权头传入的access_令牌。还需要一个类似的端点来更新用户

我在最新的Spring5/Boot2中找到的所有示例都是关于保护webapp的

@GetMapping("/api/user-info")
    public Map userInfo(OAuth2AuthenticationToken authentication) {
        OAuth2AuthorizedClient authorizedClient = this.getAuthorizedClient(authentication);
        Map userAttributes = Collections.emptyMap();

        String userInfoEndpointUri = authorizedClient
                                    .getClientRegistration()
                                    .getProviderDetails()
                                    .getUserInfoEndpoint()
                                    .getUri();

        if (!StringUtils.isEmpty(userInfoEndpointUri)) {
            // userInfoEndpointUri is optional for OIDC Clients
            userAttributes = WebClient.builder()
                    .filter(oauth2Credentials(authorizedClient))
                    .build()
                    .get()
                    .uri(userInfoEndpointUri)
                    .retrieve()
                    .bodyToMono(Map.class)
                    .block();
        }

        return userAttributes;
    }

    private OAuth2AuthorizedClient getAuthorizedClient(OAuth2AuthenticationToken authentication) {
        return this.authorizedClientService.loadAuthorizedClient(
                authentication.getAuthorizedClientRegistrationId(), authentication.getName());
    }

    private ExchangeFilterFunction oauth2Credentials(OAuth2AuthorizedClient authorizedClient) {
        return ExchangeFilterFunction.ofRequestProcessor(
                clientRequest -> {
                    ClientRequest authorizedRequest = ClientRequest.from(clientRequest)
                            .header(HttpHeaders.AUTHORIZATION, "Bearer " + authorizedClient.getAccessToken().getTokenValue())
                            .build();
                    return Mono.just(authorizedRequest);
                });
    }

方法中定义的OAuth2AuthenticationToken对象为null,这是可以理解的,但不确定还需要配置什么

谢谢你的帮助


共 (0) 个答案