【Https】Spring RestTemplete支持Https安全请求
实现步骤
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安全请求相关推荐
- 【Spring】Spring Boot 支持 Https
1.美图 2.概述 https 现在已经越来越普及了,特别是做一些小程序或者公众号开发的时候,https 基本上都是刚需了. 不过一个 https 证书还是挺费钱的,个人开发者可以在各个云服务提供商那 ...
- ie8 ajax访问不了https,ie8不支持https协议的api接口么
如图 ie8 并没有你这个问题 ---------补充,别说ie8了,ie5我都跑通了--------- 前端代码如下 var $dom = $("#root"); $.ajax( ...
- 【转】借助第三方支持https协议的存储实现自己网站上扫描二维码安装IOS APP
使用plist安装,一般是企业级开发者账号不需要登录到APP STORE的IOS设备应用发布时所用到的技巧. 准备: *一台运行着OSX的苹果电脑,最新版的XCODE,用于导出ipa和plist 一个 ...
- Spring Boot工程支持HTTP和HTTPS,HTTP重定向HTTPS
本文试图以通俗易通的方式介绍Https的工作原理,不纠结具体的术语,不考证严格的流程.我相信弄懂了原理之后,到了具体操作和实现的时候,方向就不会错,然后条条大路通罗马.阅读文本需要提前大致了解对称加密 ...
- springmvc配置ssl_Spring Mvc和Spring Boot配置Tomcat支持Https
SpringBoot配置支持https spring boot因为是使用内置的tomcat,所以只需要一些简单的配置即可. 1.首先打开命令行工具,比如cmd,输入以下命令 keytool -genk ...
- 微信小程中wx.requert请求外部数据和.NET Core 发布服务支持https
微信小程序的项目中wx.requert请求外部数据 1.注册小程序获取AppId和Appsecret 2.wx.login 获得 code: 3.发送 res.code 到后台换取 openId, s ...
- HTTP请求工具类,支持https
前言 有时候后台需要请求其他的API来获取数据,springboot可以使用RestTemplate,但是在工具类里使用就有点麻烦了.所以就自定义一个http请求的工具类,来处理GET.POST.PU ...
- node服务器支持https请求
node服务器支持https请求 水文一篇 问题 前段时间给自己的域名接入了https,但是前端向后端发起请求的时候报了这么一个错误 net::ERR_SSL_PROTOCOL_ERROR 意思是ht ...
- postman-SSL证书问题-支持HTTPS请求
使用Google接口调试插件postman请求https协议的接口,postman提示: 为此,需要解决这个问题,提示信息已经给出了解决方案!Using self-signed SSL certifi ...
- php重新编译curl扩展,在Linux安装php的curl扩展,并支持https请求,重新编译法
环境:php5.5.5 + nginx 任务:安装php curl扩展,并支持https 血与泪之坑:我先安装了curl,发现不支持https,然后再安装openssl,然而一切都晚了,折腾了半天,还 ...
最新文章
- 进程间通讯,临界区,互斥
- node的包管理工具:yarn和npm
- 怎么用excel表做网页搜索工具_你敢相信?Excel居然可以做搜索引擎
- linux删除具有指定内容的文件,Linux bash删除文件中含“指定内容”的行功能示例...
- HTML页面加载顺序
- 技巧:让Eclipse或Flex Builder 支持自动换行。(转)
- 元启发式算法之一:蝙蝠算法BA
- 物联网感知-高压直流长距离供电在线实时监测(光纤传感技术)
- pyinstaller系列之十一:exe 反编译到 源码 尝试
- uni-app微信小程序短信验证、微信支付
- Java FX小项目国际象棋
- Effective Java笔记(第二章)
- 钉钉开放平台查询宜搭表单实例数据
- JS实现键盘移动光标
- h5跳转微信公众号文章,小程序,任意站跳转链接制作方法?
- 快速了解 Robot Operating System(ROS) 机器人操作系统
- Linux 性能分析命令详解
- 【MySQL】聚合函数详解
- iOS音频篇:AVPlayer的缓存实现
- Angularts将前端数据生成报表