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

完结撒花!!!

相关推荐
码语智行5 分钟前
基于word模板导出人员信息
java
西凉的悲伤13 分钟前
redis和数据库实现分布式锁
java·数据库·redis·分布式
Csvn14 分钟前
Linux 文件与目录操作命令(通关版)
后端
weixin_5231853218 分钟前
Java内存模型详解:栈、堆、方法区、本地方法栈与程序计数器
java·开发语言
ywl47081208718 分钟前
泛型extends和super的区别
java
惜缘破军29 分钟前
基于 Spring Boot 4 和 Spring Cloud 2025 的微服务基础框架 hdfk7-boot
java
cjp56029 分钟前
007. ASP.NET WEB API配置JWT令牌身份认证
后端·asp.net
程序员mine35 分钟前
Web服务密码存储安全详解:从哈希到密钥派生的演进
前端·后端
小白起 v35 分钟前
从零搭建一个现代化的验证码登录系统:Spring Boot + 阿里云短信实战教程
java·阿里云