当前位置: 首页 > 知识库问答 >
问题:

如何在spring Boot中使用客户端私钥和服务器提供的证书调用服务器?

况嘉运
2023-03-14

我是java中ssl的新手,需要帮助。我的应用程序需要调用支付提供商服务器,使用他们提供的证书和我的公钥。

final String password = "password";
    SSLContext sslContext = SSLContextBuilder
            .create()
            .loadTrustMaterial(ResourceUtils.getFile("/home/workspace/gop/javaclient.jks"), password.toCharArray())
            .build();

    CloseableHttpClient client = HttpClients.custom()
            .setSSLContext(sslContext)
            .build();

    HttpComponentsClientHttpRequestFactory requestFactory
            = new HttpComponentsClientHttpRequestFactory();
    requestFactory.setHttpClient(client);

    RestTemplate restTemplate = new RestTemplate(requestFactory);

    String url = "https://someurl.com/rndpoint"; // Web Service endpoint that requires SSL

    ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.POST, HttpEntity.EMPTY, String.class);
    ResponseEntity<String> response2 = restTemplate.exchange(url, HttpMethod.GET, HttpEntity.EMPTY, String.class);

    System.out.println("Result = " + response.getBody());
    return response.getBody() + response2.getBody();

我的输出:

{
    "timestamp": "2020-04-19T08:28:18.871+0000",
    "status": 500,
    "error": "Internal Server Error",
    "message": "I/O error on POST request for \"https://nabiltest.compassplus.com:8444/Exec\": 
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",
    "path": "/nabil-payment"
}

共有1个答案

冯阳成
2023-03-14

我终于设法解决了这个问题。下面是我的代码片段。

private RestTemplate getRestTemplateClientAuthentication()
 throws IOException, UnrecoverableKeyException, CertificateException, NoSuchAlgorithmException,
 KeyStoreException, KeyManagementException {
final String allPassword = "123456";
 TrustStrategy acceptingTrustStrategy = (X509Certificate[] chain, String authType) -> true;
 SSLContext sslContext = SSLContextBuilder
 .create()
//if you use keystore
 .loadKeyMaterial(ResourceUtils.getFile("classpath:keystore.jks"),
 allPassword.toCharArray(), allPassword.toCharArray())
//if you want to use truststore instead
//.loadTrustMaterial(ResourceUtils.getFile("classpath:truststore.jks"), allPassword.toCharArray())
 .loadTrustMaterial(null, acceptingTrustStrategy)
 .build();
HttpClient client = HttpClients.custom()
 .setSSLContext(sslContext)
 .build();
HttpComponentsClientHttpRequestFactory requestFactory =
 new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(client);
RestTemplate restTemplate = new RestTemplate(requestFactory);
return restTemplate;
}

现在只需使用此函数调用您的endpoint

// url ->  endpoint url
getRestTemplateClientAuthentication().exchange(url, HttpMethod.POST, HttpEntity.EMPTY, String.class);
 类似资料:
  • websocket客户端(使用Autobahn/Python和Twisted)需要连接到websocket服务器:客户端需要向服务器提供其客户端证书,客户端需要检查服务器的证书。例如,这些证书是在Kubernetes minikube安装过程中创建的。特别地: 服务器证书(据我所知为X509格式) 客户端证书~/。minikube/客户。按键 我已经检查过,我可以成功地使用这些证书密钥使用发出库伯

  • 我需要用RSA-2048服务器公钥加密客户端私钥。我知道私钥明显比公钥长,我不确定是否可能。。。但我看到类似的任务是用Python完成的,所以我想知道您的看法。 crypto/rsa致命错误:对于rsa公钥大小,消息太长

  • 验证 SSH 服务器证书(客户端) 和 SSH 用户证书 类似,使用客户端机构认证来验证主机。需要一个 SSH 服务器数字证书认证机构签发服务器证书,客户端只需要保存 CA 的公钥。 使用一台处于气隙系统下的计算机来生成服务器数字证书认证机构的根证书: ❯ ssh-keygen -C "SSH Server Certificate Authority" -f sshserver.root.ca 使

  • 我必须使用证书调用服务器API,所以我已经将cer文件导入到密钥库中并完成编码,但仍然得到了javax.net.ssl.SSLHandShakeException:Sun.Security.Validator.ValidatoreXception:PKIX路径构建失败:Sun.Security.Provider.CertPath.SunCertPathBuilderException:找不到请求目

  • 我想使用远程过程调用(RPC)开发一个Qt5/C客户机-服务器应用程序。 想法:服务器监听多个客户端的传入连接。客户机提供了一组服务器可以调用的过程/服务,以便从客户机收集数据并将更改通知其他客户机。 这里有一个问题:到目前为止,我看到的RPC库似乎期望服务器提供客户端可能调用的服务。但我想做相反的事情。客户端应该提供服务器可能调用的服务。方向很重要,因为我想只在服务器端启用端口转发,而不是在客户

  • 我将创建一个身份验证服务器,它本身与一组不同的Oauth2.0服务器交互。Netty似乎是在这里实现网络部分的一个很好的候选者。但在开始之前,我需要澄清一些关于netty的细节,因为我是新手。例行程序如下: > < li> 服务器接受来自客户端的HTTPS连接。 然后,不关闭第一个连接,它通过HTTPS与远程OAuth2.0服务器建立另一个连接并获取数据 毕竟,服务器将结果发送回客户端,客户端应该