I have tried to set network proxy in the following ways, but none of the method is working
1: set jvm variables like -Dhttp.proxyHost= -Dhttp.proxyPort= .......
2: Created the Bean.
@Bean
public RestTemplate restTemplate() {
final String proxyHost = "######"; // host
final int proxyPort = ####; // port
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
factory.setProxy(new Proxy(Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)));
return new RestTemplate(factory);
}
But this configuration is overridden by OAuth2AccessTokenSupport.restTemplate.
So the below method always returns the newly created rest template object.
org.springframework.security.oauth2.client.token.OAuth2AccessTokenSupport
protected RestOperations getRestTemplate() {
if (restTemplate == null) {
synchronized (this) {
if (restTemplate == null) {
RestTemplate restTemplate = new RestTemplate();
restTemplate.setErrorHandler(getResponseErrorHandler());
restTemplate.setRequestFactory(requestFactory);
restTemplate.setInterceptors(interceptors);
this.restTemplate = restTemplate;
}
}
}
if (messageConverters == null) {
setMessageConverters(new RestTemplate().getMessageConverters());
}
return restTemplate;
}
Kindly help me to override or set proxy on the rest template from OAuth Client application.
Another way to do this is by setting a custom AccessTokenProvider to your OAuth2RestTemplate. In the code sample below the SSL validation is bypassed :
This might not be a simple solution. But finally managed to set the proxy on oauth request by the below code.
Register the filter
Auth filter
Request helper code