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 {
@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();
}
}
参考链接
Spring RestTemplete支持Https安全请求的更多相关文章
- 【Https】Spring RestTemplete支持Https安全请求
实现步骤 Step1: 自定义ClientHttpRequestFactory package com.example.demo.https; import org.springframework.h ...
- Spring Boot 支持 HTTPS 如此简单,So easy!
这里讲的是 Spring Boot 内嵌式 Server 打 jar 包运行的方式,打 WAR 包部署的就不存在要 Spring Boot 支持 HTTPS 了,需要去外部对应的 Server 配置. ...
- Spring Boot 支持 HTTPS 如此简单,So easy!
这里讲的是 Spring Boot 内嵌式 Server 打 jar 包运行的方式,打 WAR 包部署的就不存在要 Spring Boot 支持 HTTPS 了,需要去外部对应的 Server 配置. ...
- Spring Boot 支持 Https 有那么难吗?
https 现在已经越来越普及了,特别是做一些小程序或者公众号开发的时候,https 基本上都是刚需了. 不过一个 https 证书还是挺费钱的,个人开发者可以在各个云服务提供商那里申请一个免费的证书 ...
- Spring Boot 支持https
1. 生成key JDK下 keytool -genkeypair -alias mySSL -keyalg RSA -keystore E:\tomcat.key 其中-alias是证书的别名,RS ...
- spring mvc支持跨域请求
@WebFilter(urlPatterns = "/*", filterName = "corsFilter") public class CorsFilte ...
- 支持https请求以及https请求的抓包
iOS9推出的时候,苹果希望大家使用https协议,来提高数据传输之间的安全性.下面我就从最简单的代码介绍,如何在工程中设置,来支持https的请求. 一.证书准备篇 1.证书转换 在服务器人员,给你 ...
- iOS开发 支持https请求以及ssl证书配置(转)
原文地址:http://blog.5ibc.net/p/100221.html 众所周知,苹果有言,从2017年开始,将屏蔽http的资源,强推https 楼主正好近日将http转为https,给还没 ...
- Spring Mvc和Spring Boot配置Tomcat支持Https
SpringBoot配置支持https spring boot因为是使用内置的tomcat,所以只需要一些简单的配置即可. 1.首先打开命令行工具,比如cmd,输入以下命令 keytool -genk ...
- Volley框架支持HTTPS请求。
第一次写帖子,嘿嘿. 最近了解到google2013IO大会出了个网络框架,正好项目也需要用到,就看了下. 最后发现接口都是HTTPS的,但是Volley默认是不支持HTTPS,网上找了好久,都没有对 ...
随机推荐
- work中模板、主题、样式集、样式的作用和使用方法
[收藏]Word样式.样式集.主题.模版怎么区分?进来围观学习了~ 我们先来按照层次关系从小到大排序:样式<样式集<主题<模板 接下来,我们按照层次关系从小到大开始了解它们之间的的区 ...
- Java解析HJ212环保报文
Java解析HJ212环保报文 toMap方法对报文进行基础的解析 /** * HJ212报文转换为标准化的Map * @param str HJ212报文 * @return */ public s ...
- OpenFunction 0.6.0 发布: FaaS 可观测性、HTTP 同步函数能力增强及更多特性
OpenFunction 是一个开源的云原生 FaaS(Function as a Service,函数即服务)平台,旨在帮助开发者专注于业务逻辑的研发.在过去的几个月里,OpenFunction 社 ...
- 开源之夏 2023|欢迎报名 Apache RocketMQ 社区项目!
开源之夏是由中科院软件所"开源软件供应链点亮计划"发起并长期支持的一项暑期开源活动,旨在鼓励在校学生积极参与开源软件的开发维护,培养和发掘更多优秀的开发者,促进优秀开源软件社区的蓬 ...
- Tomcat线程池详解,为什么SpringBoot最大支持200并发?
Q:经典面试题,SpringBoot 应用可以同时并发处理多少请求? A:SpringBoot 应用并发处理请求数主要由两个因素影响,使用的 Servlet容器(默认使用 Tomcat,常用的还有 j ...
- AI五子棋_01 Python的网络通信
AI五子棋 第一步 第一步 服务器是交战的战场,我们的AI大脑想要参战,先得找到去战场的路. 任务 1 写程序从以下网址取得下一步的指示 http://2**.2**.**.1**:9012/step ...
- 记录EntityFramework增删改产生的SQL语句
项目里遇到一个数据交换的需求,申报端和审批端的系统和数据库都不在同一个网段:甲方提供一个msmq队列:我们把申报和审批产生的变化数据用sql记录到xml报文中,通过交换xml文件再解析出sql语句来实 ...
- NES 模拟器中音画同步问题
背景 模拟器是与游戏和播放器都有相似之处的系统.模拟器与游戏的相似之处,在于都需要一个采集输入--执行逻辑--然后按一定帧率(通常是 60 FPS)把画面显示出来的循环.但是模拟器又需要模拟音频设备, ...
- Nginx 安全配置
server { listen 8089; server_name 10.5.210.203:8089; #charset koi8-r; #access_log logs/host.access.l ...
- python 中的[:-1]和[::-1]的具体使用
案例 a='python' b=a[::-1] print(b) #nohtyp c=a[::-2] print(c) #nhy #从后往前数的话,最后一个位置为-1 d=a[:-1] #从位置0到位 ...