resttemplate 调用https使用下面代码:

@Bean
@Primary
public RestTemplate restTemplate(ClientHttpRequestFactory httpRequestFactory) {
return new RestTemplate(httpRequestFactory);
}

@Bean
public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
//单位为ms
factory.setReadTimeout(10 * 1000);
//单位为ms
factory.setConnectTimeout(30 * 1000);
return factory;
}
调用没有证书的https出现的错误

org.springframework.web.client.ResourceAccessException: I/O error on POST request for "https://gateway.fat.demo.com/service-commodity/providerInventory/queryInventory": sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target; nested exception is javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

修改为下面的java代码后,一切OK:

@Primary
@Bean
public RestTemplate restTemplate() {
return new RestTemplate(generateHttpsRequestFactory());
}

public HttpComponentsClientHttpRequestFactory generateHttpsRequestFactory() {
try {
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);
factory.setConnectTimeout(10 * 1000);
factory.setReadTimeout(30 * 1000);
return factory;
} catch (Exception e) {
log.error("创建HttpsRestTemplate失败", e);
throw new RuntimeException("创建HttpsRestTemplate失败", e);
}

}

resttemplate 调用https 出错 unable to find valid certification path to requested target的更多相关文章

  1. https编程遇到PKIX:unable to find valid certification path to requested target 的问题

    https编程遇到PKIX:unable to find valid certification path to requested target 的问题 2016-12-01 解决方案见:解决PKI ...

  2. java程序中访问https时,报 PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    在java中使用https访问数据时报异常: Caused by: sun.security.validator.ValidatorException: PKIX path building fail ...

  3. PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    注:网上搜来的快照,暂未验证 在java代码中请求https链接的时候,可能会报下面这个错误javax.net.ssl.SSLHandshakeException: sun.security.vali ...

  4. 解决 java 使用ssl过程中出现"PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target"

    今天,封装HttpClient使用ssl时报一下错误: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorExc ...

  5. 解决PKIX(PKIX path building failed) 问题 unable to find valid certification path to requested target

    最近在写java的一个服务,需要给远程服务器发送post请求,认证方式为Basic Authentication,在请求过程中出现了 PKIX path building failed: sun.se ...

  6. 解决flutter:unable to find valid certification path to requested target 的问题

    1.问题 周末在家想搞搞flutter,家里电脑是windows的,按照官网教程一步步安装好以后,创建flutter工程,点击运行,一片红色弹出来,WTF? PKIX path building fa ...

  7. 工作日志,证书无效 unable to find valid certification path to requested target

    工作日志,证书无效 unable to find valid certification path to requested target 最近被这个问题弄得头大.导致所有用到 se.transmod ...

  8. IDEA unable to find valid certification path to requested target

    一.报错 Could not transfer artifact org.apache.maven.plugins:maven-install-plugin:pom:2.4 from/to alima ...

  9. Maven:sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target

    还是记录使用 maven 时遇到的问题. 一.maven报错 maven package 进行打包时出现了以下报错: Non-resolvable parent POM for com.wpbxin: ...

  10. Android Studio出现:Cause: unable to find valid certification path to requested target问题解决

    Android Studio , Flutter , IDEA 工程报错 unable to find valid certification path to requested target 最新解 ...

随机推荐

  1. JDBC 和 Mybatis

    使用JDBC连接操作数据库 Mybatis是JDBC的二次封装 使用更加简单了

  2. day14-Scanner

    Scanner对象 之前我们学的基本语法中我们并没有实现程序和人的交互,但是Java给我们提供了这样一个工具类,我们可以获取用户的输入.Java.util.Scanner是Java5的新特征,我们可以 ...

  3. 我被 .NET8 JIT 的一个BUG反复折磨了半年之久

    很久很久没有写过博客了, 正好最近园子又挣得一线生机, 必须得凑个热闹水一篇. 事情是这样的, 在今年的早些时候, 把公司的一部分api服务器的.net版本从6升级到了8, 毕竟6马上就是EOL了(. ...

  4. 华为IPv6 GRE隧道

    IPv6 over IPv4 GRE封装隧道 实验目标: 该实验参考了华为官网案例配置 https://support.huawei.com/enterprise/zh/doc/EDOC1000097 ...

  5. 【FAQ】HarmonyOS SDK 闭源开放能力 —Map Kit(3)

    1.问题描述: compatibleSdkVersion升级到5.0.0(12)之后,调用坐标系转换API:map.convertCoordinate(mapCommon.CoordinateType ...

  6. for-each循环陷阱

    for-each删除元素报错 public static void main(String[] args) { List<String> list = new ArrayList<& ...

  7. 使用 Cursor 和 Devbox 快速开发基于 Rust 的 WASM 智能合约

    本教程以一个智能合约(使用 NEAR 的一个官方 Fungible Tokens 来实现)的例子来介绍一下 Devbox 的强大功能,轻松构建环境,轻松发布. NEAR 是一个去中心化的应用平台,使用 ...

  8. 反射+特性实现 类和XML文档的序列化反序列化

    1.作用:将实体类的属性(字符串.日期.数值.布而.类)生成为xml文档中的结点,将xml文档的结点值或者属性值填充到实体类的属性值中2.思路:特性.反射.泛型:特性记录xml结点与实体属性的对应关系 ...

  9. Python 中的抽象类和接口类

    [抽象类] 抽象类是一个特殊的类,只能被继承,不能被实例化.它主要用于作为其他类的基类或模板. 抽象类可以包含抽象方法和具体方法.在抽象类中定义的抽象方法必须在子类中进行实现. from abc im ...

  10. MySQL查询BLOB类型的字段

    1.MySQL有四种BLOB类型: 1.TinyBlob 最大能容纳255B的数据 2.Blob 最大能容纳65KB的 3.MediumBlob 最大能容纳16MB的数据 4.LongBlob 最大能 ...