RestTemplate请求https忽略证书认证
RestTemplate是Spring提供的用于访问Rest服务的客户端,提供了多种便捷访问远程Http服务的方法,能够大大提高客户端的编写效率。RestTemplate 默认使用J2SE提供的方式(既java.net包提供的方式)创建底层的Http请求连接(SimpleClientHttpRequestFactory),不需要配置证书信息,但如果调用https请求时的证书不合法,会报”unable to find valid certification path to requested target“错误。这时可以使用HttpComponentsClientHttpRequestFactory方式,底层使用HttpClient访问远程的Http服务来配置证书。
代码如下:
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate; import javax.net.ssl.SSLContext; import org.apache.http.client.config.RequestConfig;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContexts;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; @Component("httpClientFactory")
public class HttpClientFactory { @Value("${restclient.readTimeout}")
private int readTimeout; @Value("${restclient.connectTimeout}")
private int connectTimeout; public CloseableHttpClient acceptsUntrustedCertsHttpClient() throws NoSuchAlgorithmException,
KeyManagementException, KeyStoreException {
SSLContext sslcontext = SSLContexts.custom().setSecureRandom(new SecureRandom())
.loadTrustMaterial(null, new TrustStrategy() { public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1" }, null,
SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(readTimeout)
.setConnectTimeout(connectTimeout).setStaleConnectionCheckEnabled(true).build(); return HttpClients.custom().setSSLSocketFactory(sslsf).setDefaultRequestConfig(defaultRequestConfig).build();
}
}
初始化httpClient
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:p="http://www.springframework.org/schema/p" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd"> <bean id="httpClient" factory-bean="httpClientFactory" factory-method="acceptsUntrustedCertsHttpClient" /> <bean id="clientHttpRequestFactory" class="org.springframework.http.client.HttpComponentsClientHttpRequestFactory">
<constructor-arg ref="httpClient" />
</bean> <bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
<constructor-arg index="0">
<list>
<bean id="byteArrayHttpMessageConverter" class="org.springframework.http.converter.ByteArrayHttpMessageConverter"></bean>
<bean id="stringHttpMessageConverter" class="org.springframework.http.converter.StringHttpMessageConverter">
<constructor-arg value="UTF-8"></constructor-arg>
</bean>
<bean id="resourceHttpMessageConverter" class="org.springframework.http.converter.ResourceHttpMessageConverter"></bean>
<bean id="sourceHttpMessageConverter" class="org.springframework.http.converter.xml.SourceHttpMessageConverter"></bean>
<bean id="allEncompassingFormHttpMessageConverter" class="org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter"></bean>
</list>
</constructor-arg>
<property name="requestFactory" ref="clientHttpRequestFactory" />
</bean>
</beans>
配置resttemplate
RestTemplate请求https忽略证书认证的更多相关文章
- svnkit https 忽略证书认证
直接上代码 解决jdk版本问题:Security.setProperty("jdk.tls.disabledAlgorithms", ""); import j ...
- java实现https免证书认证
java实现https免证书认证 解决方法: 1.下载两个包,httpclient-4.2.jar和httpcore-4.2.jar,复制以下代码就可使用. 2.调用类代码: String htt ...
- SSL通信-忽略证书认证错误
.NET的SSL通信过程中,使用的证书可能存在各种问题,某种情况下可以忽略证书的错误继续访问.可以用下面的方式跳过服务器证书验证,完成正常通信. 1.设置回调属性ServicePointManager ...
- https绕过证书认证请求 Get或Post请求(证书过期,忽略证书)
报错信息 解决: postman方式 java请求 报错信息 javax.net.ssl.SSLHandshakeException: sun.security.validator.Validator ...
- java https post请求并忽略证书,参数放在body中
1 新建java类,作用是绕过证书用 package cn.smartercampus.core.util; import java.security.cert.CertificateExceptio ...
- https的证书认证 iOS版
一.证书链 SecTrustRef: SecTrustRef trust = challenge.protectionSpace.serverTrust; 需要先拿出一个 SecTrustRef 对象 ...
- Https 忽略证书\使用自定义证书的java代码实现
public SSLContext createIgnoreVerifySSL() throws KeyManagementException, NoSuchAlgorithmException, K ...
- 各种编程语言忽略http的SSL证书认证
目录 前言 代码 go语言 Python语言 Ruby语言 Java语言 PHP语言 C#语言 前言 我们内部测试的http服务器很多时候证书都是没有经过第三方认证的,我们发送http请求基本上都是忽 ...
- HTTP-java访问https资源时,忽略证书信任问题,代码栗子
java程序在访问https资源时,出现报错 sun.security.validator.ValidatorException: PKIX path building failed: sun.sec ...
随机推荐
- Unity3D优化技巧系列七
笔者介绍:姜雪伟,IT公司技术合伙人.IT高级讲师,CSDN社区专家,特邀编辑.畅销书作者,国家专利发明人;已出版书籍:<手把手教你架构3D游戏引擎>电子工业出版社和<Unity3D ...
- java组件学习15天
Linuxdocker redminegitlabnginxmavenkibanakafkaspringspring cloudLucene 搭建基本使用
- Linux cp命令
cp命令(copy),用来对一个或多个文件,目录进行拷贝 1.语法 cp [选项] [参数] 2.命令选项 -b 当文件存在时,覆盖前,为其创建一个备份-d 当复制软连接时,把目标文件或目录也建立为软 ...
- abap编辑器中代码不可修改
当出现abap编辑器中代码不能修改的情况,可以按如下设置: edit——> 修改操作——>关闭助手
- MBR和GPT(分区)
MBR:Master Boot Record GPT:Guid Partition Table (全局唯一标识符分区表) GPT is the new standard and is graduall ...
- beego——发行部署
开发模式 通过bee创建的项目,beego默认情况下是开发模式. 我们可以通过如下的方式改变我们的模式: beego.RunMode = "prod" 或者我们在conf/app. ...
- python全栈开发从入门到放弃之socket并发编程多线程GIL
一 介绍 ''' 定义: In CPython, the global interpreter lock, or GIL, is a mutex that prevents multiple nati ...
- javascript 类型 内存
ecmscript中包含两种类型 基本类型值 引用类型值(对象) 按值传递和按引用传递 function test ($num) { //按值传递,JavaScript中没有按引用 ...
- SQL注入漏洞有哪些
SQL注入攻击是当今最危险.最普遍的基于Web的攻击之一.所谓注入攻击,是攻击者把SQL命令插入到Web表单的输入域页面请求的查询字符串中,如果要对一个网站进行SQL注入攻击,首先需要找到存在SQL注 ...
- FileOutputStream写出数据实现换行和追加写入
FileOutputStream fos = fos = new FileOutputStream(Utils.getData(bizCtx,"strcat(getenv(HWORKDIR) ...