首页 > 其他分享 >restTemplate请求 跳过SSL认证

restTemplate请求 跳过SSL认证

时间:2023-01-05 16:34:21浏览次数:34  
标签:http restTemplate factory SSL org apache import 跳过

//思路 添加配置类,然后创建的restTemplate 时候引入好配置类

RestTemplate restTemplate = null;
try {
restTemplate = new RestTemplate(RestTemplateConfig.generateHttpRequestFactory());
} catch (Exception e) {
log.error("创建restTemplate失败:" + e.getMessage(), e);
}

配置类

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.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

@Configuration
public class RestTemplateConfig {
@Bean
public RestTemplate restTemplate(ClientHttpRequestFactory factory){
return new RestTemplate(factory);
}

@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory(){
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setConnectTimeout(15000);
factory.setReadTimeout(5000);
return factory;
}

public static HttpComponentsClientHttpRequestFactory generateHttpRequestFactory()
throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException
{
TrustStrategy acceptingTrustStrategy = (x509Certificates, authType) -> true;
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, acceptingTrustStrategy).build();
SSLConnectionSocketFactory connectionSocketFactory = new SSLConnectionSocketFactory(sslContext, new NoopHostnameVerifier());

HttpClientBuilder httpClientBuilder = HttpClients.custom();
httpClientBuilder.setSSLSocketFactory(connectionSocketFactory);
CloseableHttpClient httpClient = httpClientBuilder.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory();
factory.setHttpClient(httpClient);
return factory;
}


}

标签:http,restTemplate,factory,SSL,org,apache,import,跳过
From: https://www.cnblogs.com/liang-shi/p/17027954.html

相关文章

  • NGINX 配置 SSL 双向认证
    NGINX配置SSL双向认证1.1.生成一个CA私钥:ca.keyopensslgenrsa-outca.key40961.2.生成一个CA的数字证书:ca.crt(CommonName随意填写;其它可以填”.”)op......
  • RestTemplate 设置超时时间(转发)
    项目访问量大,频繁调取其他系统接口经常出现项目后台假死现象,发现其他系统掉线重启一段时间必现。查看调用接口,同事直接引用了RestTemplate但是没有设置超时时间->_<-。两......
  • 使用Let's Encrypt 安装配置免费SSL 证书教程
    一、Let'sEncrypt简介Let'sEncrypt是一个由非营利性组织互联网安全研究小组(ISRG)提供的免费、自动化和开放的证书颁发机构(CA)。简单的说,借助Let'sEncrypt颁发的证......
  • 使用Let's Encrypt 安装配置免费SSL 证书教程
    一、Let'sEncrypt简介Let'sEncrypt是一个由非营利性组织互联网安全研究小组(ISRG)提供的免费、自动化和开放的证书颁发机构(CA)。简单的说,借助Let'sEncrypt颁发的证......
  • webdav服务/ddns/域名/ssl证书设置
    1.webdav服务1.1.Windowsiis自带的webdav组件默认未启用,可以启用进行设置,使用了一下,在使用浏览器或Windows资源管理器内右键添加网络位置作为客户端时比较好用,但是......
  • mysql show processlist的使用
    showfullprocesslist用来查看当前线程处理情况,具体信息请参考官网:https://dev.mysql.com/doc/refman/5.7/en/show-processlist.htmlshowfullprocesslist返回的结果......
  • nginx: the "ssl" parameter requires ngx_http_ssl_module in /usr/local/nginx/conf
    Nginx如果未开启SSL模块,配置Https时将提示如题错误原因:nginx缺少http_ssl_module模块,编译安装的时候带上--with-http_ssl_module配置就行了,但是现在的情况是我的nginx已经......
  • 面对Centos7系统的openssl版本升级
    CentOS7的版本系统,默认的OpenSSL的版本为OpenSSL1.0.2k-fips26Jan2017。但是openssl需要的版本需要较高的版本。通过下载最新的openssl版本。对openssl进行升级。1.查......
  • org.nutz.http.Http忽略https SSL证书验证
    访问的是一个httpsget请求,报错需要SSL证书验证,以下方法直接跳过booleancheck=Http.disableJvmHttpsCheck(); //忽略https的证书检查......
  • Spring RestTemplate 专题
    相同的参数(接口的入参json打印在日志了)在PostMan中返回预期的数据,但使用RestTemplate时去提示信息错误(参数中汉字)。这种情况,搞得怀疑对RestTemplate的理解了使用RestTempla......