Get access token using spring-boot-starter-oauth2-client

43 views Asked by At

I have used below code to get the access token from OAuth2 server, but while running the code from Linux machine I need to add the proxy and the proxy configurations which I have added are not working.

    @Bean
    public OAuth2AuthorizedClientManager authorizedClientManager(
            ClientRegistrationRepository clientRegistrationRepository, OAuth2AuthorizedClientService clientService) {
        
        HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
        httpClientBuilder = httpClientBuilder.setProxy(new HttpHost("my.proxy.server.com", 8080));
        CloseableHttpClient httpClient = httpClientBuilder.build();
        HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
        requestFactory.setHttpClient(httpClient);
        
         RestTemplate restTemplate = new RestTemplate( 
                    Arrays.asList(new FormHttpMessageConverter(), new OAuth2AccessTokenResponseHttpMessageConverter())); 
                restTemplate.setErrorHandler(new OAuth2ErrorResponseErrorHandler()); 
                
        DefaultClientCredentialsTokenResponseClient tokenResponseClient = new DefaultClientCredentialsTokenResponseClient(); 
        
        tokenResponseClient.setRestOperations(restTemplate); 
        ClientCredentialsOAuth2AuthorizedClientProvider authorizedClientProvider = new ClientCredentialsOAuth2AuthorizedClientProvider(); 
        authorizedClientProvider.setAccessTokenResponseClient(tokenResponseClient); 

        AuthorizedClientServiceOAuth2AuthorizedClientManager authorizedClientManager = new AuthorizedClientServiceOAuth2AuthorizedClientManager(
                clientRegistrationRepository, clientService);
        authorizedClientManager.setAuthorizedClientProvider(authorizedClientProvider);
        return authorizedClientManager;
    }

Have tried with above code

0

There are 0 answers