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());
}

完结撒花!!!

相关推荐
大大杰哥3 分钟前
SSeEmitter的基本使用和介绍
java·sse·通信
道友可好5 分钟前
写给 AI 的入职手册,AGENTS.md
前端·人工智能·后端
闪电悠米5 分钟前
黑马点评-Redis 消息队列-02_list_pubsub_limits
java·数据库·ide·redis·缓存·list·intellij-idea
海梨花7 分钟前
字节面试高频算法题
java·算法·面试·职场和发展
野生技术架构师9 分钟前
Java 23 种设计模式:从踩坑到精通 —— 开篇及系列介绍
java·开发语言·设计模式
折哥的程序人生 · 物流技术专研13 分钟前
《Java 100 天进阶之路》第93篇:Redis实战应用:缓存策略与分布式锁(2026版)
java·redis·缓存·面试·架构·求职招聘
瓦特what?13 分钟前
位运算核心技巧与应用
java·jvm·算法
人道领域16 分钟前
【LeetCode刷题日记】90.子集Ⅱ--- 归纳题解
java·开发语言·leetcode
sandnes20 分钟前
把ToolUse循环做到生产级-错误处理与可靠性五件套
后端
ch.ju22 分钟前
Java Programming Chapter 4——Characteristics of inheritance
java·开发语言