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

完结撒花!!!

相关推荐
No0d1es11 分钟前
电子学会青少年软件编程(C/C++)1级等级考试真题试卷(2025年9月)
java·c语言·c++·青少年编程·电子学会·真题·一级
我是天龙_绍21 分钟前
java 比对两对象大小 重写 comparator
后端
IT_陈寒25 分钟前
Python 3.12新特性实测:10个让你的代码提速30%的隐藏技巧 🚀
前端·人工智能·后端
BingoGo26 分钟前
从零开始打造 Laravel 扩展包:开发、测试到发布完整指南
后端·php
9号达人32 分钟前
普通公司对账系统的现实困境与解决方案
java·后端·面试
golang学习记33 分钟前
Go 1.26 新特性:netip.Prefix.Compare —— 标准化 IP 子网排序能力
后端
超级苦力怕35 分钟前
Java 为何 long a = 999999999 能过;long a = 9999999999 报错?一文讲透“宽化转换”
java
佐杰40 分钟前
Jenkins使用指南1
java·运维·jenkins
花落已飘40 分钟前
openEuler容器化实践:从Docker入门到生产部署
后端