实现步骤

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

转载请标明出处:http://www.cnblogs.com/ssslinppp/
 
标签: Https, RestTemplete

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

  1. 【Https】Spring RestTemplete支持Https安全请求

    实现步骤 Step1: 自定义ClientHttpRequestFactory package com.example.demo.https; import org.springframework.h ...

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

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

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

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

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

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

  5. Spring Boot 支持https

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

  6. spring mvc支持跨域请求

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

  7. 支持https请求以及https请求的抓包

    iOS9推出的时候,苹果希望大家使用https协议,来提高数据传输之间的安全性.下面我就从最简单的代码介绍,如何在工程中设置,来支持https的请求. 一.证书准备篇 1.证书转换 在服务器人员,给你 ...

  8. iOS开发 支持https请求以及ssl证书配置(转)

    原文地址:http://blog.5ibc.net/p/100221.html 众所周知,苹果有言,从2017年开始,将屏蔽http的资源,强推https 楼主正好近日将http转为https,给还没 ...

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

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

  10. Volley框架支持HTTPS请求。

    第一次写帖子,嘿嘿. 最近了解到google2013IO大会出了个网络框架,正好项目也需要用到,就看了下. 最后发现接口都是HTTPS的,但是Volley默认是不支持HTTPS,网上找了好久,都没有对 ...

随机推荐

  1. 安装nvm管理node版本(npm、yarn)

    安装nvm管理node版本(npm.yarn) 一.下载安装nvm nvm网址:https://nvm.uihtm.com/ 1.点击下载链接下载nvm 2.将下载的压缩包解压,解压后双击安装包,然后 ...

  2. KubeSphere 社区双周报|2024.02.01-02.29

    KubeSphere 社区双周报主要整理展示新增的贡献者名单和证书.新增的讲师证书以及两周内提交过 commit 的贡献者,并对近期重要的 PR 进行解析,同时还包含了线上/线下活动和布道推广等一系列 ...

  3. insufficient permission for adding an object to repository database .git/objects

    1.出错截图: 有时候使用软件项目管理系统github时候,会出现一些问题截图如下: 2.出错原因 从出错的地方就知道是因为权限不足导致,回想一下,在链接远程服务器时候,不小心切换为管理员权限进行了g ...

  4. oracle下拼同比环比查询sql方法

    拼接方法: /// <summary> /// 生成计算同比环比查询语句 /// table:表名称:statColumns:要统计的值字段;yearColumn:年份字段名:monthC ...

  5. css动画效果(边框线条流动效果)

    1.整体效果 https://mmbiz.qpic.cn/sz_mmbiz_gif/EGZdlrTDJa6FxrVbiamfvb7b0H4qcDzZRwq3PqvXfuMDaPZ44VUic1h2WR ...

  6. C++中的各种锁

    在多线程开发中,经常会遇到数据同步,很多情况下用锁都是一个很好的选择.C++中常用的锁主要有下面几种: 互斥锁(std::mutex) 这是最基本的一种锁.它用于保护共享资源,在任意时刻,最多只有一个 ...

  7. ATC:多快好省,无参数token reduction方法 | ECCV'24

    来源:晓飞的算法工程笔记 公众号,转载请注明出处 论文: Agglomerative Token Clustering 论文地址:https://arxiv.org/abs/2409.11923 论文 ...

  8. sql注入--学习笔记_1

    实验室 sql sql可以对数据库进行访问和处理:取回数据,删除数据.web页面会使用这些. SQL 能做什么? SQL 面向数据库执行查询 SQL 可从数据库取回数据 SQL 可在数据库中插入新的记 ...

  9. An expression evaluator

    An expression evaluator  Download source code (17 kb) Two weeks ago, I saw an article on codeproject ...

  10. golang之copier

    今天我们要介绍的copier库就能处理不同类型之间的赋值.除此之外,copier还能: 调用同名方法为字段赋值: 以源对象字段为参数调用目标对象的方法,从而为目标对象赋值(当然也可以做其它的任何事情) ...