如何在java中从webclient获取访问令牌

qvtsj1bj  于 2021-06-30  发布在  Java
关注(0)|答案(0)|浏览(223)

我们正在尝试从oauth2resttemplate升级到webclient,并尝试从webclient获取访问令牌,但无法获取。getaccesstoken()方法与oauth2resttemplate一起提供,我们使用此方法获取访问令牌。
有人能告诉我有没有办法从webclient获取访问令牌?

private ClientRegistrationRepository getClientRegistrationRepository() {
    return new InMemoryClientRegistrationRepository(ClientRegistration.withRegistrationId("myId")
            .tokenUri(getAuthenticationBaseUri())
            .clientSecret(getClientSecret())
            .clientId(getClientId())
            .clientAuthenticationMethod(ClientAuthenticationMethod.POST)
            .authorizationGrantType(AuthorizationGrantType.CLIENT_CREDENTIALS).build());
}

public OAuth2AuthorizedClientRepository getOAuth2AuthorizedClientRepository(
        ClientRegistrationRepository clientRegistrationRepository) {
    return new AuthenticatedPrincipalOAuth2AuthorizedClientRepository(
            new InMemoryOAuth2AuthorizedClientService(clientRegistrationRepository));
}

@Bean
public OAuth2AuthorizedClientManager authorizedClientManager() {

    OAuth2AuthorizedClientProvider authorizedClientProvider = OAuth2AuthorizedClientProviderBuilder.builder()
            .clientCredentials().build();
    ClientRegistrationRepository clientRegistrationRepository = getClientRegistrationRepository();
    DefaultOAuth2AuthorizedClientManager authorizedClientManager = new DefaultOAuth2AuthorizedClientManager(
            clientRegistrationRepository, getOAuth2AuthorizedClientRepository(clientRegistrationRepository));
    authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
    return authorizedClientManager;
}
@Bean
public WebClient webClient(OAuth2AuthorizedClientManager authorizedClientManager) {
    ServletOAuth2AuthorizedClientExchangeFilterFunction oauth2Client =
            new ServletOAuth2AuthorizedClientExchangeFilterFunction(authorizedClientManager);
    oauth2Client.setDefaultClientRegistrationId("myId");
    return WebClient.builder()
            .apply(oauth2Client.oauth2Configuration())
            .build();

}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题