import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.http.converter.HttpMessageConverter; import org.springframework.http.converter.StringHttpMessageConverter; import org.springframework.web.client.RestTemplate; import javax.net.ssl.HostnameVerifier; import javax.net.ssl.HttpsURLConnection; import java.io.IOException; import java.net.HttpURLConnection; import java.net.InetSocketAddress; import java.net.Proxy; import java.nio.charset.StandardCharsets; import java.util.List; @Configuration public class RestConfig { @Bean public RestTemplate normal() { return new RestTemplate(); } // 解决中文乱码 @Bean public RestTemplate noConfusionChinese() { RestTemplate restTemplate = new RestTemplate(); List> list = restTemplate.getMessageConverters(); for (HttpMessageConverter> httpMessageConverter : list) { if(httpMessageConverter instanceof StringHttpMessageConverter) { ((StringHttpMessageConverter) httpMessageConverter).setDefaultCharset(StandardCharsets.UTF_8); break; } } return restTemplate; } // 添加代理 @Bean public RestTemplate proxy() { RestTemplate restTemplate = new RestTemplate(); SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory(); simpleClientHttpRequestFactory.setProxy(new Proxy(Proxy.Type.HTTP,new InetSocketAddress("代理IP",端口))); restTemplate.setRequestFactory(simpleClientHttpRequestFactory); return restTemplate; } // 请求绕过证书认证 @Bean public RestTemplate disableSsl() { HostnameVerifier PROMISCUOUS_VERIFIER = (s, sslSession ) -> true; RestTemplate restTemplate = new RestTemplate(); restTemplate.setRequestFactory( new SimpleClientHttpRequestFactory() { @Override protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { if(connection instanceof HttpsURLConnection){ ((HttpsURLConnection) connection).setHostnameVerifier(PROMISCUOUS_VERIFIER); } super.prepareConnection(connection, httpMethod); } }); return restTemplate; } // 使用代理并绕过证书认证 @Bean public RestTemplate proxyDisableSsl() { HostnameVerifier PROMISCUOUS_VERIFIER = (s, sslSession ) -> true; RestTemplate restTemplate = new RestTemplate(); SimpleClientHttpRequestFactory simpleClientHttpRequestFactory = new SimpleClientHttpRequestFactory(){ @Override protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException { if(connection instanceof HttpsURLConnection){ ((HttpsURLConnection) connection).setHostnameVerifier(PROMISCUOUS_VERIFIER); } super.prepareConnection(connection, httpMethod); } }; simpleClientHttpRequestFactory.setProxy(new Proxy(Proxy.Type.HTTP,new InetSocketAddress("代理IP",端口))); restTemplate.setRequestFactory(simpleClientHttpRequestFactory); return restTemplate; } }
使用时 @Autowired 引入,@Qualifier 标明用的是哪个构造方法即可。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)