Spring RestTemplate and Proxy Auth

I am trying to make REST calls using Spring. As far as I understand, the correct way is to use RestTemplate (?). The problem is what kind of proxy I am.

This is my code right now:

  SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); InetSocketAddress address = new InetSocketAddress(host, 3128); Proxy proxy = new Proxy(Proxy.Type.HTTP, address); factory.setProxy(proxy); RestTemplate restTemplate = new RestTemplate(); restTemplate.setRequestFactory(factory); 

It seems to work, but I need to authenticate with the proxy, but how do I do this? The Proxy type, as well as the SimpleClientHttpRequestFactory type, do not seem to handle credentials. Without credentials, I get only 407 ...

+19
java spring spring-mvc proxy
source share
3 answers

After several different options, I settled on the code below because of the ability to set proxies for RestTemplate at creation so that I could reorganize it into a separate method. It’s just to note that it also has an additional dependency, so keep that in mind.

  private RestTemplate createRestTemplate() throws Exception { final String username = "myusername"; final String password = "myPass"; final String proxyUrl = "proxy.nyc.bigtower.com"; final int port = 8080; CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope(proxyUrl, port), new UsernamePasswordCredentials(username, password)); HttpHost myProxy = new HttpHost(proxyUrl, port); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.setProxy(myProxy).setDefaultCredentialsProvider(credsProvider).disableCookieManagement(); HttpClient httpClient = clientBuilder.build(); HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); factory.setHttpClient(httpClient); return new RestTemplate(factory); } 

// Dependencies I used.

  <dependency> <groupId>org.apache.httpcomponents</groupId> <artifactId>httpclient</artifactId> <version>4.5.2</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>4.2.5.RELEASE</version> </dependency> 
+22
source share

the code below worked for me.

 RestTemplate restTemplate = new RestTemplate(); CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials( new AuthScope("proxyHost", "proxyPort"), new UsernamePasswordCredentials("proxyUser", "proxyPass") ); HttpClientBuilder clientBuilder = HttpClientBuilder.create(); clientBuilder.useSystemProperties(); clientBuilder.setProxy(new HttpHost("proxyHost", "proxyPort")); clientBuilder.setDefaultCredentialsProvider(credsProvider); clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy()); CloseableHttpClient client = clientBuilder.build(); HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(); factory.setHttpClient(client); restTemplate.setRequestFactory(factory); 
+6
source share

I used the same code, but getting 407.proxy credentials works fine with the curl command, but using the code above doesn't work. any idea?

0
source share

All Articles