业务:本系统接口都是http的,调用第三方接口,因为做了安全性校验,所以不能通过RestTemplate调用

方法:重写覆盖SimpleClientHttpRequestFactory抽象类的prepareConnection方法

package com.common.utils.http.rest;

import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
import org.springframework.http.client.SimpleClientHttpRequestFactory; import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.net.HttpURLConnection;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; /**
* 兼容调Https接口
* @Date 2020/06/04 17:16
* @Param
* @return
*/
public class HttpsClientRequestFactory extends SimpleClientHttpRequestFactory { @Override
protected void prepareConnection(HttpURLConnection connection, String httpMethod) {
try {
if (!(connection instanceof HttpsURLConnection)) {// http协议
//throw new RuntimeException("An instance of HttpsURLConnection is expected");
super.prepareConnection(connection, httpMethod);
}
if (connection instanceof HttpsURLConnection) {// https协议,修改协议版本
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
// 信任任何链接
TrustStrategy anyTrustStrategy = new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
return true;
}
};
SSLContext ctx = SSLContexts.custom().useTLS().loadTrustMaterial(trustStore, anyTrustStrategy).build();
((HttpsURLConnection) connection).setSSLSocketFactory(ctx.getSocketFactory());
HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
super.prepareConnection(httpsConnection, httpMethod);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}

关键代码,new RestTemplate(new HttpsClientRequestFactory());,对应工具类参考:

package com.common.utils.http.rest;

import common.utils.web.WebUtil;
import org.springframework.http.*;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate; import java.util.Map; /**
* <pre>
* RestTemplate 远程调用工具类
* </pre>
*
* <pre>
* 修改记录
* 修改后版本: 修改人: 修改日期: 2020/06/01 11:38 修改内容:
* </pre>
*/
@Component
public class RestTemplateUtils { public static RestTemplate geTemplate(){
return new RestTemplate(new HttpsClientRequestFactory());
} /**
* GET请求调用方式
* @Date 2020/06/01 13:47
* @Param [url, responseType, uriVariables]
* @return org.springframework.http.ResponseEntity<T>
*/
public static <T> ResponseEntity<T> getForEntity(String url, Class<T> responseType, Map<String, ?> uriVariables) {
return geTemplate().getForEntity(url, responseType, uriVariables);
} /**
* POST请求调用方式
* @Date 2020/06/01 13:47
* @Param [url, headers, body, responseType]
* @return org.springframework.http.ResponseEntity<T>
*/
public static <T> ResponseEntity<T> postForEntity(String url,HttpHeaders headers, Object requestBody , Class<T> responseType ){ HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return geTemplate().postForEntity(url, requestEntity, responseType);
} /**
* PUT请求调用方式
* @Date 2020/06/01 13:35
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,与Map中的key对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> put(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Map<String, ?> uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return geTemplate().exchange(url, HttpMethod.PUT, requestEntity, responseType, uriVariables);
} /**
* DELETE请求调用方式
* @Date 2020/06/01 13:37
* @param url 请求URL
* @param headers 请求头参数
* @param requestBody 请求参数体
* @param responseType 返回对象类型
* @param uriVariables URL中的变量,按顺序依次对应
* @return ResponseEntity 响应对象封装类
*/
public static <T> ResponseEntity<T> delete(String url, HttpHeaders headers, Object requestBody, Class<T> responseType, Object... uriVariables) {
HttpEntity<Object> requestEntity = new HttpEntity<Object>(requestBody, headers);
return geTemplate().exchange(url, HttpMethod.DELETE, requestEntity, responseType, uriVariables);
} /**
* 通用调用方式
* @Date 2020/06/01 13:37
* @Param [url, method, requestEntity, responseType, uriVariables]
* @return org.springframework.http.ResponseEntity<T>
*/
public static <T> ResponseEntity<T> exchange(String url, HttpMethod method, HttpEntity<?> requestEntity, Class<T> responseType, Map<String, ?> uriVariables) {
return geTemplate().exchange(url, method, requestEntity, responseType, uriVariables);
}
}

SpringBoot系列之RestTemplate调https接口的更多相关文章

  1. [Java] 绕过证书验证调 HTTPS 接口时报 “SSLHandshakeException: DHPublicKey does not comply to algorithm constraints”的解决办法

    作者: zyl910 一.缘由 最近有在对接一个无证书的HTTPS接口时,总是收到"SSLHandshakeException: DHPublicKey does not comply to ...

  2. Springboot系列(七) 集成接口文档swagger,使用,测试

    Springboot 配置接口文档swagger 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配 ...

  3. Java调用Http/Https接口(6)--RestTemplate调用Http/Https接口

    RestTemplate是Spring提供的用于访问Http接口的客户端,提供同步的API:在将来的Spring版本中可能会过时,将逐渐被WebClient替代.文中所使用到的软件版本:Java 1. ...

  4. SpringBoot系列(八)分分钟学会Springboot多种解决跨域方式

    SpringBoot系列(八) 分分钟学会SpringBoot多种跨域解决方式 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 s ...

  5. SpringBoot系列(十四)集成邮件发送服务及邮件发送的几种方式

    往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)web静 ...

  6. SpringBoot系列(九)单,多文件上传的正确姿势

    SpringBoot系列(九)分分钟解决文件上传 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配 ...

  7. SpringBoot系列(十)优雅的处理统一异常处理与统一结果返回

    SpringBoot系列(十)统一异常处理与统一结果返回 往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列 ...

  8. SpringBoot系列(十一)拦截器与拦截器链的配置与使用详解,你知道多少?

    往期推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)web静 ...

  9. SpringBoot系列(十二)过滤器配置详解

    SpringBoot(十二)过滤器详解 往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件 ...

  10. SpringBoot系列(十三)统一日志处理,logback+slf4j AOP+自定义注解,走起!

    往期精彩推荐 SpringBoot系列(一)idea新建Springboot项目 SpringBoot系列(二)入门知识 springBoot系列(三)配置文件详解 SpringBoot系列(四)we ...

随机推荐

  1. C#实现MergeSort算法

    public class MergeSortLearn { /// <summary> /// 分治递归 /// </summary> /// <param name=& ...

  2. XSSpecter - Blind XSS 检测与管理工具

    XSSpecter 是一个模块化的盲测跨站脚本(XSS)漏洞管理工具包,包含服务端回调处理和客户端自动化测试工具. 项目概述 XSSpecter 提供两大核心组件: 服务端 - 处理XSS回调.数据持 ...

  3. 记录一下 nas 当本地网络存储,玩大型游戏再也不用担心硬盘不够用了

    最近由于家里的PC 在玩steam的游戏,原来1T的固态,安装了几个大型的3A游戏,一个黑神话悟空就用了200多G,硬盘就不够用了 查看了京东的2T的固态,还是很贵,要大几百块钱.去年配了一台极空间的 ...

  4. Jenkins 起服务包后自动退出

    今天使用Jenkins来做一个定时更新并启java服务包的任务,搞了挺久. 比较坑爹的就是,我的jar包有十几个,各个包也比较大,每次启动都要好久. 但启完最后一个包之后,我去,Jenkins就结束了 ...

  5. odoo14里面开发一个简单的action.client 的tag 模板例子

    1.js模板  web_template.js odoo.define('web', function (require) { "use strict"; var core = r ...

  6. AI大模型应用开发入门-LangChain开发聊天机器人ChatBot

    在大模型应用开发中,状态管理 和 对话追踪 是不可忽视的重要能力,尤其在需要保存上下文.重放对话或进行异步处理时尤为关键. 今天我们来演示如何用 LangChain + OpenAI 的 GPT 模型 ...

  7. Go 重构案例分享:订单创建逻辑重构

    背景:从 PHP (Laravel) 到 Go 的模式迁移 •原 PHP (Laravel) 实现思路:核心模式: "行为管道" (Behavior Pipeline).如何工作: ...

  8. C# 托盘图标缓存清除

    https://blog.csdn.net/weixin_42953003/article/details/119676004 using System; using System.Collectio ...

  9. wifi转串口

    wifi转串口 ZLAN7146是一款wifi转串口的wifi串口服务器.该串口服务器可以方便地使得串口设备连接到WIFI无线网络,实现串口设备的无线化网络升级.RS232接口支持全双工.不间断通信: ...

  10. centos8 yum替换阿里源

    解决centos7使用yum安装mysql 下载速度慢的问题 挺好用的,之前用腾讯云安装了半天,太慢了,改过之后速度快多了. 1.首先备份系统自带yum源配置文件/etc/yum.repos.d/Ce ...