SpringBoot 之 配置 RestTemplate + 跳过https 验证

上截图

目录文件结构

在配置文件下创建下面两个文件

文件内容

HttpsClientHttpRequestFactory.java

java 复制代码
package org.fri.config;

import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.SimpleClientHttpRequestFactory;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.security.KeyStore;

/**
 * 处理无法发送https 请求问题
 * 忽略 https 校验
 */
@Configuration
public class HttpsClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
        try {
            if (connection instanceof HttpsURLConnection) {
                KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                //信任任何链接,忽略对证书的校验
                TrustStrategy trustStrategy = (x509Certificates, s) -> true;
                //自定义sslcontext
                SSLContext ctx = SSLContexts.custom().loadTrustMaterial(trustStore, trustStrategy).build();
                //解决问题
                ((HttpsURLConnection) connection).setSSLSocketFactory(ctx.getSocketFactory());
                //解决No subject alternative names matching IP address xxx.xxx.xxx.xxxx found问题
                ((HttpsURLConnection) connection).setHostnameVerifier((s, sslSession) -> true);
                HttpsURLConnection httpsURLConnection = (HttpsURLConnection) connection;
                super.prepareConnection(httpsURLConnection, httpMethod);
            } else {
                super.prepareConnection(connection, httpMethod);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

ResTemplateConfig.java

java 复制代码
package org.fri.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class ResTemplateConfig {
    @Bean
    RestTemplate restTemplate() {
        return new RestTemplate(new HttpsClientHttpRequestFactory());
    }
}

配置完成

请求示例

java 复制代码
HttpHeaders headers = new HttpHeaders();       
HttpEntity<String> requestEntity = new HttpEntity<>(headers);
ResponseEntity<JSONObject> responseEntity = restTemplate.exchange(reqUri, HttpMethod.GET, requestEntity, JSONObject.class);
if (responseEntity.getStatusCode().equals(HttpStatus.OK)) {
    log.info(responseEntity.getBody().toJSONString());
}

完结撒花!!!

相关推荐
吃饱了得干活12 小时前
缓存与数据库一致性:从理论到实战
java·后端·面试
520拼好饭被践踏12 小时前
JAVA+Agent学习day22
java·开发语言·后端·学习
whi12 小时前
V 编译器 v3 ownership 模式:编译与使用指南
后端·编译器
笨蛋不要掉眼泪12 小时前
Java虚拟机:堆的参数配置
java·开发语言·jvm
swipe12 小时前
05|(前端转后全栈)不手写一堆 SQL,后端怎么操作数据库?MyBatis-Plus 入门
前端·后端·全栈
霸道流氓气质12 小时前
SpringBoot中使用JasperReports 报表引擎 — 介绍、原理与使用实践
java·spring boot·后端
swipe12 小时前
04|(前端转后全栈)前端状态为什么不够用?从页面数据到 MySQL 持久化
前端·后端·全栈
花生了什么事o12 小时前
DDD 分层架构:六层分层架构
java·架构·ddd
苏三说技术12 小时前
为什么越来越多人使用WebFlux?
后端
Y38153266212 小时前
3 种 SERP 数据处理方案对比:流式 / 批处理 / 缓存
java·spring·缓存