业务:本系统接口都是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. 【拥抱鸿蒙】Flutter+Cursor轻松打造HarmonyOS应用(二)

    这是[Flutter+Cursor轻松打造HarmonyOS应用]系列的第二篇.前一篇已经介绍了如何搭建Flutter鸿蒙应用开发环境,就让我们一起来看看如何借助Cursor让鸿蒙App开发更快更简单 ...

  2. 乒乓球测距(K210)

    测距 说明 识别的物体是乒乓球(规格应该是统一的吧), 硬件是K210,测距的函数经过拟合,在50cm范围内是准确的 如果使用起来不准确,可以打印代码中的LM, 然后去测数据,自己再拟合一个函数,代替 ...

  3. java实现linux文件的解压缩(确保md5sum一致)

    package com.xlkh.device.utils; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import java. ...

  4. 如何在FastAPI中实现权限隔离并让用户乖乖听话?

    title: 如何在FastAPI中实现权限隔离并让用户乖乖听话? date: 2025/06/18 17:24:12 updated: 2025/06/18 17:24:12 author: cmd ...

  5. 【前端AI实践】DeepSeek:开源大模型的使用让开发过程不再抓头发

    有时候你可能正对着屏幕发呆,不知道怎么下手一个 Vue 的流式请求功能.这时候,DeepSeek 就像是你的"编程外挂",帮你把模糊的需求变成清晰的代码. 下面我们就以几个常见的开 ...

  6. DRF之异常捕获源码分析

    DRF之异常捕获源码分析 [一]异常捕获介绍 Django Rest Framework(DRF)是一个用于构建Web API的强大框架,它提供了一种处理异常的机制,使开发人员能够捕获和处理各种异常情 ...

  7. 如何基于three.js(webgl)引擎架构,实现3D机房园区,数据中心消防系统

    前言前面的文章我们已经详细介绍了数据中心机房的关键知识点,以及消防领域的基础知识.在此基础上,本文将深入探讨展示消防在数据中心这一特殊场景中的应用,特别是气体消防系统的应用模拟及发生火灾时逃生路径规划 ...

  8. DotTrace系列:3. 时间度量之墙钟时间和线程时间

    一:背景 1. 讲故事 在用 dotTrace 对程序进行性能评测的时候,有一个非常重要的概念需要使用者明白,那就是 时间度量 (Time measurement),主要分为两种. 墙钟时间 线程时间 ...

  9. STL:迭代器与常用算法

    迭代器 C++ STL(Standard Template Library,标准模板库)中迭代器与常用算法是泛型编程的核心组成部分.它们配合使用,可以对容器进行高效.统一的操作.下面是对它们的系统性总 ...

  10. sublime text 2 snippet 设置

    1.标准文件写法 <snippet> <content><![CDATA[ 你需要插入的代码片段${1:name} ]]></content> < ...