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. ChallengeMobile

    解题思路 获取到输入的字符串保存到s,调用Jformat方法对s进行验证,返回true则代表输入字符串正确反之错误. Jformat方法分析: 首先看到使用了LoadData加载了"ming ...

  2. .NET高级调试 - 3.7对象检查

    简介 在大多数调试会话中,首先需要检查的项目就是分析应用程序的状态.在确认程序的问题是某种无效状态造成的,我们便需要分析程序是如何变成无效状态的.那么在分析过程中,需要为我们深入了解对象的各种审查方法 ...

  3. tekton初次安装报错“containers with incomplete status: [place-tools]”

    报错内容 在按照官方部署方式部署完毕以后,执行第一个taskrun的时候就报错了,报错如下 Status: Conditions: Last Transition Time: 2022-08-08T0 ...

  4. [第一章 web入门]SQL注入-1

    启动靶机 很明显注入点为id值,单引号闭合影响语句,说明为单引号闭合 构造注入语句 ?id=1 ' and 1 =1 --+ 发现没报错,说明没有其他过滤 ,开始sql注入 ?id=1 ' order ...

  5. 云原生周刊:CNCF 宣布 Falco 毕业|2024.3.4

    开源项目推荐 ldap-operator 用于部署和管理 LDAP 目录的 Kubernetes Operator. Updatecli Updatecli 是一个用于应用文件更新策略的工具.每个应用 ...

  6. KubeSphere 社区双周报 | OpenFunction 发布 v1.1.0 | 2023.5.26-6.8

    KubeSphere 社区双周报主要整理展示新增的贡献者名单和证书.新增的讲师证书以及两周内提交过 commit 的贡献者,并对近期重要的 PR 进行解析,同时还包含了线上/线下活动和布道推广等一系列 ...

  7. 云原生爱好者周刊:使用 GitOps 来动态管理 Grafana 的数据源

    文章推荐 使用 GitOps 来动态管理 Grafana 的数据源 通过 Grafana 的 Provisioning 特性,可以在 provisioning/datasources 目录下添加多个 ...

  8. SQLSEVER 实现货币数字转中文汉字

    SQLSEVER 实现数字转换成中文(货币) -- ============================================= -- Author: LearnerPing -- Cr ...

  9. Sentinel简单使用(1)

    使用场景 在微服务架构中,服务之间会进行大量的调用.为了防止某个服务被过多的请求压垮,导致整个系统崩溃,就需要对流量进行控制.同时,当某个服务出现故障时,为了防止故障扩散到整个系统,需要进行熔断操作. ...

  10. vue之JavaScript封装

    导入js文件有很多种方式,我喜欢使用这种,感觉跟框架导入很相似.没有多余的沉湎. 定义一个js文件,比如util.js import Vue from "vue"; //===== ...