RestTemplate发送https请求配置
程序员文章站
2022-06-25 19:17:36
...
使用RestTemplate发送Https请求
创建RestTemplateConfig
package com.example.zrf.config;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.ssl.TrustStrategy;
import org.springframework.cloud.client.loadbalancer.LoadBalanced;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate() throws NoSuchAlgorithmException, KeyStoreException {
TrustStrategy trustStrategy = ((x509Certificates, aythType) -> true);
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, trustStrategy).build();
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());
HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setSSLSocketFactory(sslConnectionSocketFactory);
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient);
return new RestTemplate(factory);
}
}
推荐阅读
-
IOS开发 支持https请求以及ssl证书配置详解
-
c#使用Socket发送HTTP/HTTPS请求的实现代码
-
java 发送http和https请求的实例
-
c#使用Socket发送HTTP/HTTPS请求的实现代码
-
前端笔记之微信小程序(三)GET请求案例&文件上传和相册API&配置https
-
Java发送https请求代码实例
-
前端笔记之微信小程序(三)GET请求案例&文件上传和相册API&配置https
-
RestTemplate发送带请求头的Json的Post请求Utils工具类
-
RestTemplate的请求参数传递问题 RestTemplate发送Get请求通过body传参问题
-
RestTemplate实现发送带headers的GET请求