RestTemplate发送https请求
程序员文章站
2022-03-04 19:07:52
...
package com.sunsy.websocket_demo.configuration;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import javax.net.ssl.SSLSession;
import javax.net.ssl.SSLSocketFactory;
import javax.net.ssl.TrustManager;
import javax.net.ssl.X509TrustManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfiguretion {
@Bean
public RestTemplate restTemplate(ClientHttpsRequestFactory factory) {
return new RestTemplate(factory);
}
@Bean
public ClientHttpsRequestFactory simpleClientHttpRequestFactory() {
ClientHttpsRequestFactory factory = new ClientHttpsRequestFactory();
factory.setReadTimeout(5000);
factory.setConnectTimeout(5000);
return factory;
}
class ClientHttpsRequestFactory extends SimpleClientHttpRequestFactory {
@Override
protected void prepareConnection(HttpURLConnection connection, String httpMethod)
throws IOException {
if (connection instanceof HttpsURLConnection) {
prepareHttpsConnection((HttpsURLConnection) connection);
}
super.prepareConnection(connection, httpMethod);
}
private void prepareHttpsConnection(HttpsURLConnection connection) {
connection.setHostnameVerifier(new SkipHostnameVerifier());
try {
connection.setSSLSocketFactory(createSslSocketFactory());
} catch (Exception ex) {
// Ignore
}
}
private SSLSocketFactory createSslSocketFactory() throws Exception {
SSLContext context = SSLContext.getInstance("TLSv1.2");
context.init(null, new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
if(true) {}
}
@Override
public void checkServerTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
if(true) {}
}
@Override
public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; }
}}, new java.security.SecureRandom());
return context.getSocketFactory();
}
private class SkipHostnameVerifier implements HostnameVerifier {
@Override
public boolean verify(String requestedHost, SSLSession remoteServerSession) {
System.out.println(requestedHost);
System.out.println("+++++++++++++++++++++++++++");
System.out.println(remoteServerSession.getPeerHost());
System.out.println("+++++++++++++++++++++++++++++");
System.out.println(requestedHost.equalsIgnoreCase(remoteServerSession.getPeerHost()));
return requestedHost.equalsIgnoreCase(remoteServerSession.getPeerHost()); // Compliant
}
}
// private class SkipX509TrustManager implements X509TrustManager {
//
// @Override
// public X509Certificate[] getAcceptedIssuers() {
// return new X509Certificate[0];
// }
//
// @Override
// public void checkClientTrusted(X509Certificate[] chain, String authType) {
// }
//
// @Override
// public void checkServerTrusted(X509Certificate[] chain, String authType) {
// }
//
// }
}
}
上一篇: Regsvr32 在64位机器上的用法
下一篇: DLL详解(下)---MFC下的DLL
推荐阅读
-
IE6通过get发送奇数个汉字请求会乱码的解决方法_PHP教程
-
php curl 获取https请求的2种方法,curlhttps
-
PHP模拟发送POST请求之三、加强file_get_contents发送POST请求
-
php之curl实现http与https请求的方法,phpcurlhttps请求_PHP教程
-
java发送http get请求的两种方式
-
java发送http get请求的两种方法(总结)
-
IOS开发 支持https请求以及ssl证书配置详解
-
Java 发送http请求上传文件功能实例
-
iOS之Https自签名证书认证及数据请求的封装原理
-
Python 使用requests模块发送GET和POST请求的实现代码