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 分钟前
解锁AI编程密码:程序员常用的10个AI提示词
后端
九转成圣40 分钟前
Java 性能优化实战:如何将海量扁平数据高效转化为类目字典树?
java·开发语言·json
直奔標竿1 小时前
Java开发者AI转型第二十七课!Spring AI 个人知识库实战(六)——全栈闭环收官,解锁前端流式渲染终极技巧
java·开发语言·前端·人工智能·后端·spring
金銀銅鐵2 小时前
[java] 编译之后的记录类(Record Classes)长什么样子(上)
java·jvm·后端
uzong3 小时前
我研读了 500 个 Spring Boot 生产级代码库,90% 都犯了这 7 个致命错误
后端
野生技术架构师3 小时前
金三银四面试总结篇,汇总 Java 面试突击班后的面试小册
java·面试·职场和发展
xiaobaoyu3 小时前
ssm知识点梳理
后端
小袁拒绝摆烂4 小时前
多表关联大平层转JSON树形结构
java·json
IT_陈寒4 小时前
Vite的public文件夹放静态资源?这坑我替你踩了
前端·人工智能·后端