Proxy Configuration in OAuth2RestTemplate

I need to use an API that is protected by OAuth2. For this I use OAuth2RestTemplate. But I get below the error:

java.net.ConnectException: Connection timed out: connect 

This is due to a proxy problem. I know how to set proxies in RestTemplate:

  SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("Proxy host", 8080)); 

clientHttpRequestFactory.setProxy (proxy); RestTemplate restTemplate = new RestTemplate (clientHttpRequestFactory);

Similarly, I tried setting for OAuth2RestTemplate :

 @Bean public OAuth2RestOperations restTemplate(OAuth2ClientContext oauth2ClientContext) { OAuth2RestTemplate client = new OAuth2RestTemplate(resource(), oauth2ClientContext); SimpleClientHttpRequestFactory clientHttpRequestFactory = new SimpleClientHttpRequestFactory(); Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT)); clientHttpRequestFactory.setProxy(proxy); client.setRequestFactory(clientHttpRequestFactory); return client; } 

But it does not work and throws a Connection timed out exception. This is due to this first line OAuth2RestTemplate client = new OAuth2RestTemplate(resource(), oauth2ClientContext); , which is trying to get an access token, which means that proxy settings are also required. if i add below line then it works:

 System.setProperty("https.proxyHost", "urproxy.com"); System.setProperty("https.proxyPort", "8080"); 

But I can not use the System.setProperties ("," ") parameter, because we do not have permission to install on the tomcat server.

I researched but could not find a way to set the proxy in OAuth2RestTemplate when creating this object.

Any help would be greatly appreciated. Thanks

+5
source share
1 answer

OAuth2RestTemplate simply creates an AccessTokenProvider to retrieve the token from the authorization server according to the different types of grant types. For example, AuthorizationCodeAccessTokenProvider used to retrieve an access token with an authorization type of type grant. Token providers themselves initiate some RestTemplate to send the request, but do not use only OAuth2RestTemplate . One way can solve the problem. That is, to create your own AccessTokenProvider and set the factory request.

 SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory(); Proxy proxy= new Proxy(Type.HTTP, new InetSocketAddress(PROXY_HOST, PROXY_PORT)); requestFactory.setProxy(proxy); AuthorizationCodeAccessTokenProvider authorizationCodeAccessTokenProvider = new AuthorizationCodeAccessTokenProvider(); authorizationCodeAccessTokenProvider.setRequestFactory(requestFactory); ImplicitAccessTokenProvider implicitAccessTokenProvider = new ImplicitAccessTokenProvider(); implicitAccessTokenProvider.setRequestFactory(requestFactory); AccessTokenProvider accessTokenProvider = new AccessTokenProviderChain( Arrays.<AccessTokenProvider> asList(authorizationCodeAccessTokenProvider, implicitAccessTokenProvider)); OAuth2RestTemplate client = new OAuth2RestTemplate(github(), oauth2ClientContext); client.setAccessTokenProvider(accessTokenProvider); 

You can also add ResourceOwnerPasswordAccessTokenProvider and ClientCredentialsAccessTokenProvider to OAuth2RestTemplate.

+7
source

All Articles