实现步骤

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 {@Overrideprotected 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() {@Overridepublic 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;}@Overridepublic String[] getDefaultCipherSuites() {return delegate.getDefaultCipherSuites();}@Overridepublic String[] getSupportedCipherSuites() {return delegate.getSupportedCipherSuites();}@Overridepublic 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);}@Overridepublic Socket createSocket(final String host, final int port) throws IOException {final Socket underlyingSocket = delegate.createSocket(host, port);return overrideProtocol(underlyingSocket);}@Overridepublic Socket createSocket(final String host, final int port, final InetAddress localAddress, final int localPort) throwsIOException {final Socket underlyingSocket = delegate.createSocket(host, port, localAddress, localPort);return overrideProtocol(underlyingSocket);}@Overridepublic Socket createSocket(final InetAddress host, final int port) throws IOException {final Socket underlyingSocket = delegate.createSocket(host, port);return overrideProtocol(underlyingSocket);}@Overridepublic Socket createSocket(final InetAddress host, final int port, final InetAddress localAddress, final int localPort) throwsIOException {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

【Https】Spring RestTemplete支持Https安全请求相关推荐

  1. 【Spring】Spring Boot 支持 Https

    1.美图 2.概述 https 现在已经越来越普及了,特别是做一些小程序或者公众号开发的时候,https 基本上都是刚需了. 不过一个 https 证书还是挺费钱的,个人开发者可以在各个云服务提供商那 ...

  2. ie8 ajax访问不了https,ie8不支持https协议的api接口么

    如图 ie8 并没有你这个问题 ---------补充,别说ie8了,ie5我都跑通了--------- 前端代码如下 var $dom = $("#root"); $.ajax( ...

  3. 【转】借助第三方支持https协议的存储实现自己网站上扫描二维码安装IOS APP

    使用plist安装,一般是企业级开发者账号不需要登录到APP STORE的IOS设备应用发布时所用到的技巧. 准备: *一台运行着OSX的苹果电脑,最新版的XCODE,用于导出ipa和plist 一个 ...

  4. Spring Boot工程支持HTTP和HTTPS,HTTP重定向HTTPS

    本文试图以通俗易通的方式介绍Https的工作原理,不纠结具体的术语,不考证严格的流程.我相信弄懂了原理之后,到了具体操作和实现的时候,方向就不会错,然后条条大路通罗马.阅读文本需要提前大致了解对称加密 ...

  5. springmvc配置ssl_Spring Mvc和Spring Boot配置Tomcat支持Https

    SpringBoot配置支持https spring boot因为是使用内置的tomcat,所以只需要一些简单的配置即可. 1.首先打开命令行工具,比如cmd,输入以下命令 keytool -genk ...

  6. 微信小程中wx.requert请求外部数据和.NET Core 发布服务支持https

    微信小程序的项目中wx.requert请求外部数据 1.注册小程序获取AppId和Appsecret 2.wx.login 获得 code: 3.发送 res.code 到后台换取 openId, s ...

  7. HTTP请求工具类,支持https

    前言 有时候后台需要请求其他的API来获取数据,springboot可以使用RestTemplate,但是在工具类里使用就有点麻烦了.所以就自定义一个http请求的工具类,来处理GET.POST.PU ...

  8. node服务器支持https请求

    node服务器支持https请求 水文一篇 问题 前段时间给自己的域名接入了https,但是前端向后端发起请求的时候报了这么一个错误 net::ERR_SSL_PROTOCOL_ERROR 意思是ht ...

  9. postman-SSL证书问题-支持HTTPS请求

    使用Google接口调试插件postman请求https协议的接口,postman提示: 为此,需要解决这个问题,提示信息已经给出了解决方案!Using self-signed SSL certifi ...

  10. php重新编译curl扩展,在Linux安装php的curl扩展,并支持https请求,重新编译法

    环境:php5.5.5 + nginx 任务:安装php curl扩展,并支持https 血与泪之坑:我先安装了curl,发现不支持https,然后再安装openssl,然而一切都晚了,折腾了半天,还 ...

最新文章

  1. 进程间通讯,临界区,互斥
  2. node的包管理工具:yarn和npm
  3. 怎么用excel表做网页搜索工具_你敢相信?Excel居然可以做搜索引擎
  4. linux删除具有指定内容的文件,Linux bash删除文件中含“指定内容”的行功能示例...
  5. HTML页面加载顺序
  6. 技巧:让Eclipse或Flex Builder 支持自动换行。(转)
  7. 元启发式算法之一:蝙蝠算法BA
  8. 物联网感知-高压直流长距离供电在线实时监测(光纤传感技术)
  9. pyinstaller系列之十一:exe 反编译到 源码 尝试
  10. uni-app微信小程序短信验证、微信支付
  11. Java FX小项目国际象棋
  12. Effective Java笔记(第二章)
  13. 钉钉开放平台查询宜搭表单实例数据
  14. JS实现键盘移动光标
  15. h5跳转微信公众号文章,小程序,任意站跳转链接制作方法?
  16. 快速了解 Robot Operating System(ROS) 机器人操作系统
  17. Linux 性能分析命令详解
  18. 【MySQL】聚合函数详解
  19. iOS音频篇:AVPlayer的缓存实现
  20. Angularts将前端数据生成报表

热门文章

  1. WebLogic(12C)——windows下安装教程
  2. python 实现的huffman 编码压缩,解码解压缩
  3. Linux O(n)调度器
  4. php课设报告致谢_奇安信CERT发布1月安全监测报告:需警惕这19个高危漏洞
  5. netlink 011 -- generic netlink 编程入门
  6. 如何直观的长时间统计Android应用的动态内存消耗
  7. Android环境下的GDB调试
  8. Shell脚本 批量修改目录下若干文件名
  9. 阻塞和非阻塞(串口自环测试失败原因定位)
  10. Windows下用VS2015+MSYS编译OpenH264