实现步骤

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

【Https】Spring RestTemplete支持Https安全请求的更多相关文章

  1. Spring Boot 支持 HTTPS 如此简单,So easy!

    这里讲的是 Spring Boot 内嵌式 Server 打 jar 包运行的方式,打 WAR 包部署的就不存在要 Spring Boot 支持 HTTPS 了,需要去外部对应的 Server 配置. ...

  2. Spring Boot 支持 HTTPS 如此简单,So easy!

    这里讲的是 Spring Boot 内嵌式 Server 打 jar 包运行的方式,打 WAR 包部署的就不存在要 Spring Boot 支持 HTTPS 了,需要去外部对应的 Server 配置. ...

  3. https封装类,支持get/post请求

    所需jar:commons-logging-1.1.3.jar.httpclient-4.3.1.jar.httpcore-4.3.jar package com.onlyou.microfinanc ...

  4. Spring Boot 支持https

    1. 生成key JDK下 keytool -genkeypair -alias mySSL -keyalg RSA -keystore E:\tomcat.key 其中-alias是证书的别名,RS ...

  5. Spring Boot 支持 Https 有那么难吗?

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

  6. spring mvc支持跨域请求

    @WebFilter(urlPatterns = "/*", filterName = "corsFilter") public class CorsFilte ...

  7. php中curl不支持https的解决办法

    在php程序中使用curl去访问https站点时,报错:Protocol https not supported or disabled in libcurl 该错误信息表示php当时编译时使用的cu ...

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

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

  9. Spring Mvc和Spring Boot配置Tomcat支持Https

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

随机推荐

  1. 【bug-劫持】深信服劫持

    深信服的 HTTP 劫持有办法解决嘛 劫持js 当前页面打开下载地址,chrome是弹出保存窗口不刷新页面的,但劫持了就刷新页面了 连wifi(电信)和4g(联通)都一样

  2. Unity 3D与Android Studio安卓交互之-导出jar包

    u3d与安卓 jar 包交互 本文提供全流程,中文翻译. Chinar 坚持将简单的生活方式,带给世人!(拥有更好的阅读体验 -- 高分辨率用户请根据需求调整网页缩放比例) Chinar -- 心分享 ...

  3. java-SimpleDateFormat类

    1.SimpleDateFormat类实现日期和字符串的相互转换 * A:DateFormat类的概述 * DateFormat 是日期/时间格式化子类的抽象类,它以与语言无关的方式格式化并解析日期或 ...

  4. R49 A-D D图有向有环图

    A. Palindromic Twist 给一个字符串(小写字母)   每个字符+1,-1:变成其他字符  a只能变b  z只能变y 看能否变成回文字符串 #include<bits/stdc+ ...

  5. 【HAOI2011】 向量

    数论好劲啊 原题: 给你一对数a,b,你可以任意使用(a,b), (a,-b), (-a,b), (-a,-b), (b,a), (b,-a), (-b,a), (-b,-a)这些向量,问你能不能拼出 ...

  6. prufer数列

    涨姿势---prufer数列 一. 简介 Prufer数列是无根树的一种数列.在组合数学中,Prufer数列由有一个对于顶点标过号的树转化来的数列,点数为n的树转化来的Prufer数列长度为n-2.它 ...

  7. xenserver开启虚拟机时提示找不到存储介质,强制关闭和重启都没用

    具体报错内容为: this vm needs storage that cannot be seen from that host 该错误的原因是该虚拟机使用了外部存储介质,例如,我就是因为当初规划x ...

  8. malloc,calloc,alloca和free函数

    void *malloc(size_t size)因为返回类型为空,所以可以赋值到任何类型指针,其分配的空间大小为size,返回新分配内存地址的起始处的指针,其所分配的内存未经初始化,若分配失败返回N ...

  9. 01炼数成金TensorFlow基本概念

    一.Tensorflow基本概念 1.使用图(graphs)来表示计算任务,用于搭建神经网络的计算过程,但其只搭建网络,不计算 2.在被称之为会话(Session)的上下文(context)中执行图 ...

  10. 设置 sideload Outlook Add-ins

    上期,我们讲到了用前端技术去建立一个outlook add-ins 我们今天来讲解一下怎样测试一个sideload outlook add-ins. 1. 我们需要登录Outlook在Office 3 ...