实现步骤

Step1: 自定义ClientHttpRequestFactory

package com.example.demo.https;

import org.springframework.http.client.SimpleClientHttpRequestFactory;

import javax.net.ssl.*;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.InetAddress;
import java.net.Socket;
import java.security.cert.X509Certificate; /**
* Desc: 使用Spring RestTemplete实现 Https需要自定义ClientHttpRequestFactory;
* <p>
* 参考链接:https://stackoverflow.com/questions/17619871/access-https-rest-service-using-spring-resttemplate
*/
public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory {
@Override
protected void prepareConnection(HttpURLConnection connection, String httpMethod) {
try {
if (!(connection instanceof HttpsURLConnection)) {
throw new RuntimeException("An instance of HttpsURLConnection is expected");
} HttpsURLConnection httpsConnection = (HttpsURLConnection) connection; TrustManager[] trustAllCerts = new TrustManager[]{
new X509TrustManager() {
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
} public void checkClientTrusted(X509Certificate[] certs, String authType) {
} public void checkServerTrusted(X509Certificate[] certs, String authType) {
} }
};
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, trustAllCerts, new java.security.SecureRandom());
httpsConnection.setSSLSocketFactory(new MyCustomSSLSocketFactory(sslContext.getSocketFactory())); httpsConnection.setHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String s, SSLSession sslSession) {
return true;
}
}); super.prepareConnection(httpsConnection, httpMethod);
} catch (Exception e) {
e.printStackTrace();
}
} /**
* We need to invoke sslSocket.setEnabledProtocols(new String[] {"SSLv3"});
* see http://www.oracle.com/technetwork/java/javase/documentation/cve-2014-3566-2342133.html (Java 8 section)
*/
private static class MyCustomSSLSocketFactory extends SSLSocketFactory { private final SSLSocketFactory delegate; public MyCustomSSLSocketFactory(SSLSocketFactory delegate) {
this.delegate = delegate;
} @Override
public String[] getDefaultCipherSuites() {
return delegate.getDefaultCipherSuites();
} @Override
public String[] getSupportedCipherSuites() {
return delegate.getSupportedCipherSuites();
} @Override
public Socket createSocket(final Socket socket, final String host, final int port, final boolean autoClose) throws IOException {
final Socket underlyingSocket = delegate.createSocket(socket, host, port, autoClose);
return overrideProtocol(underlyingSocket);
} @Override
public Socket createSocket(final String host, final int port) throws IOException {
final Socket underlyingSocket = delegate.createSocket(host, port);
return overrideProtocol(underlyingSocket);
} @Override
public Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort) throws
IOException {
final Socket underlyingSocket = delegate.createSocket(host, port, localAddress, localPort);
return overrideProtocol(underlyingSocket);
} @Override
public Socket createSocket(final InetAddress host, final int port) throws IOException {
final Socket underlyingSocket = delegate.createSocket(host, port);
return overrideProtocol(underlyingSocket);
} @Override
public Socket createSocket(final InetAddress host, final int port, final InetAddress localAddress, final int localPort) throws
IOException {
final Socket underlyingSocket = delegate.createSocket(host, port, localAddress, localPort);
return overrideProtocol(underlyingSocket);
} private Socket overrideProtocol(final Socket socket) {
if (!(socket instanceof SSLSocket)) {
throw new RuntimeException("An instance of SSLSocket is expected");
}
((SSLSocket) socket).setEnabledProtocols(new String[]{"TLSv1"});
return socket;
}
}
}

Step2: 设置RestTemplate的RequestFactory

package com.example.demo.https;

import org.springframework.web.client.RestTemplate;

/**
* Desc: 参考链接:https://stackoverflow.com/questions/17619871/access-https-rest-service-using-spring-resttemplate
*/
public class RestTempleteConfig {
private RestTemplate httpRestTemplate;
private RestTemplate httpsRestTemplate; public void init() {
this.httpsRestTemplate = new RestTemplate(new HttpsClientRequestFactory());
this.httpRestTemplate = new RestTemplate();
}
}

参考链接

Access Https Rest Service using Spring RestTemplate

最新文章

  1. html5离线应用和缓存
  2. 编译GCC4.8.2
  3. android之animation
  4. Android 判断当前屏幕是横屏还是竖屏
  5. nyoj 364 田忌赛马(贪心)
  6. cookie相关内容
  7. linux应用于发展(下)
  8. BackgroundCheck – 根据图片亮度智能切换元素样式
  9. schtasks确实可以绕过UAC,简直不可思议啊~~
  10. IOS开发问题录:如何在Swift中引入Head文件?
  11. [反汇编练习] 160个CrackMe之004
  12. apache常用工作模式的区别
  13. python中如何用sys.excepthook来对全局异常进行捕获、显示及输出到error日志中
  14. Fragment与FragmentActivity通信封装
  15. dplyr 数据操作 常用函数(1)
  16. js函数防抖、节流实现
  17. asp.net webapi 的Request如何获取参数
  18. JQ面向对象的放大镜
  19. JS 日期转换,格式化等常用的函数定义
  20. Image Lazy Load:那些延时加载图片的开源插件(jQuery)

热门文章

  1. Angular版本
  2. 从boost到Adaboost再到GBRT-GBDT-MART
  3. css有缝隙
  4. ★ MYSQL隔离级别 通俗理解 + mysql、oracle默认事务隔离级别
  5. Sencha Touch app example -- oreilly app 分析
  6. centos7 添加第三方源
  7. IE浏览器兼容问题
  8. 02基于python玩转人工智能最火框架之TensorFlow人工智能&amp;深度学习介绍
  9. 关于package.json
  10. Windows git 初始设置