栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

RestTemplate发起HTTPS请求

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

RestTemplate发起HTTPS请求

因为发起HTTPS请求时需要验证服务端SSL证书,所以在此有两种解决办法,一是导入证书,二是忽略证书的校验。

在此我采用的是忽略证书的校验。

首先使用 RestTemplateBuilder来构建一个 RestTemplate,而非使用默认。requestFactory()方法用来设置 ClientHttpRequestFactory。SimpleClientHttpRequestFactory是Spring内置的默认实现,实现了 ClientHttpRequestFactory接口,我们需要重写其 prepareConnection()方法,在此方法里实现对 HttpURLConnection的重新处理,忽略对证书的校验。

代码:

public class HttpsClientHttpRequestFactory extends SimpleClientHttpRequestFactory {
    @Override
    protected void prepareConnection(HttpURLConnection connection, String httpMethod) throws IOException {
        try {
            if (connection instanceof HttpsURLConnection) {// https协议,修改协议版本
                KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
                // 信任任何链接,忽略对证书的校验
                TrustStrategy anyTrustStrategy = (x509Certificates, s) -> true;
                //自定义SSLContext
                SSLContext ctx = SSLContexts.custom().loadTrustMaterial(trustStore, anyTrustStrategy).build();
                // ssl问题
                ((HttpsURLConnection) connection).setSSLSocketFactory(ctx.getSocketFactory());
                //解决No subject alternative names matching IP address xxx.xxx.xxx.xxx found问题
                ((HttpsURLConnection) connection).setHostnameVerifier((s, sslSession) -> true);
                HttpsURLConnection httpsConnection = (HttpsURLConnection) connection;
                super.prepareConnection(httpsConnection, httpMethod);
            } else { // http协议
                super.prepareConnection(connection, httpMethod);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
    
String url = "https://xxx.xxx.xxx";
RestTemplate restTemplate = new RestTemplateBuilder()
		.requestFactory(HttpsClientHttpRequestFactory::new)
		//basic认证
		.basicAuthentication("username", "password")
		.build();
HttpHeaders headers = new HttpHeaders();
HttpEntity entity = new HttpEntity<>(headers);
ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
String str = response.getBody();
System.out.println(str);
    

注:

    RestTemplateBuilder 需要直接构建成 RestTemplate对象,而不能中间生成 RestTemplateBuilder对象。
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/737433.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号