I'm trying to do REST calls with Spring. As I understand, the right way to go is using RestTemplate
(?). Problem is, I'm behind a proxy.
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 at the proxy, but how is this done? The Proxy
type as well as the SimpleClientHttpRequestFactory
type don't seem to handle credentials. Without credentials, I'm getting just 407...
Sending a request to a proxy using RestTemplate is pretty simple. All we need to do is to call the setProxy(java. net. Proxy) from SimpleClientHttpRequestFactory before building the RestTemplate object.
RestTemplate provides a synchronous way of consuming Rest services, which means it will block the thread until it receives a response. RestTemplate is deprecated since Spring 5 which means it's not really that future proof.
Setting bearer token for a GET request RestTemplate restTemplate = new RestTemplate(); String customerAPIUrl = "http://localhost:9080/api/customer"; HttpHeaders headers = new HttpHeaders(); headers. set("Authorization", "Bearer " + accessToken); //accessToken can be the secret key you generate.
After quite a few different options I settled on The below code due to the ability to set the proxy for the RestTemplate at creation so I could refactor it into a separate method. Just to note 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>
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With