Skip to content

Instantly share code, notes, and snippets.

@lijie2000
Forked from jurigis/RestProxyTemplate.java
Created February 28, 2020 19:34
Show Gist options
  • Save lijie2000/867a5d23cfa41b0a2fe8ca35feb04dbe to your computer and use it in GitHub Desktop.
Save lijie2000/867a5d23cfa41b0a2fe8ca35feb04dbe to your computer and use it in GitHub Desktop.
Spring RestTemplate with proxy settings and proxy authentication
import org.apache.http.HttpHost;
import org.apache.http.auth.AuthScope;
import org.apache.http.auth.UsernamePasswordCredentials;
import org.apache.http.client.CredentialsProvider;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.ProxyAuthenticationStrategy;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import javax.annotation.PostConstruct;
@Component
public class RestProxyTemplate {
private static final Logger LOGGER = LoggerFactory.getLogger(RestProxyTemplate.class);
private RestTemplate restTemplate;
@Value("${proxy.host}")
private String proxyHost;
@Value("${proxy.port}")
private String proxyPort;
@Value("${proxy.user}")
private String proxyUser;
@Value("${proxy.password}")
private String proxyPassword;
@PostConstruct
public void init() {
this.restTemplate = new RestTemplate();
final int proxyPortNum = Integer.parseInt(proxyPort);
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
credsProvider.setCredentials(new AuthScope(proxyHost, proxyPortNum), new UsernamePasswordCredentials(proxyUser, proxyPassword));
final HttpClientBuilder clientBuilder = HttpClientBuilder.create();
clientBuilder.useSystemProperties();
clientBuilder.setProxy(new HttpHost(proxyHost, proxyPortNum));
clientBuilder.setDefaultCredentialsProvider(credsProvider);
clientBuilder.setProxyAuthenticationStrategy(new ProxyAuthenticationStrategy());
final CloseableHttpClient client = clientBuilder.build();
final HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(client);
restTemplate.setRequestFactory(factory);
}
public RestTemplate getRestTemplate() {
return restTemplate;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment