gpt4 book ai didi

java - MasterCard API - 添加证书请求后出错

转载 作者:行者123 更新时间:2023-12-01 05:05:15 44 4
gpt4 key购买 nike

MasterCard 使用 2 足 OAuth。准备好 OAuth 有效负载后,我将其添加到 Authorization 键中的 header 中。然后请求需要与证书一起发送。我在这一步遇到错误。

我已经根据以下位置提供的示例代码编写了我的代码<强> https://developer.mastercard.com/portal/display/api/Locations+-+Sample+Code

示例 key 位于 https://developer.mastercard.com/portal/display/api/OAuth+Validation ,

生成 SSL 证书的步骤:

1. Open the Firefox browser and enter the URL : https://sandbox.api.mastercard.com
2. Click on the lock icon on the bottom of the browser.
3. Click on View Certificate
4. Click on the Details tab
5. Under "Certificate Hierarchy", click on the issuing CA (beside the arrow)
6. Click on Export..
7. Save the .PEM file (suppose with "openapi-sandbox.pem").
8. Then using Java's keytool, Load this into a JKS keystore using a command like this:

keytool -import -alias openapi-samplecode-ssl-cert -file openapi-sandbox.pem -keystore openapi-samplecode.jks

准备请求的代码

  URL url = new URL(httpsURL);
con = (HttpsURLConnection) url.openConnection();
con.setRequestMethod(method);
con.setSSLSocketFactory(getSocketFactory());
con.setDoOutput(true);
con.setDoInput(true);
con.addRequestProperty("Authorization", buildAuthHeaderString(params));

以及处理Cert文件的函数

  private SSLSocketFactory getSocketFactory() 
{
KeyStore ks = KeyStore.getInstance("JKS");
// get user password and file input stream
char[] password = "prince".toCharArray();
ClassLoader cl = this.getClass().getClassLoader();
String location = "D:\\DEV_HOME\\openapi-samplecode6.jks";
// String location = "openapi-samplecode1.jks";
System.out.println("location =" + location );
InputStream stream = cl.getResourceAsStream(location);
ks.load(stream, password);
// stream.close();

SSLContext sc = SSLContext.getInstance("TLS");
KeyManagerFactory kmf = KeyManagerFactory.getInstance("SunX509");
TrustManagerFactory tmf = TrustManagerFactory.getInstance("SunX509");

kmf.init(ks, password);
tmf.init(ks);

sc.init(kmf.getKeyManagers(), tmf.getTrustManagers(),null);

return sc.getSocketFactory();
}

预先感谢您的宝贵时间。 :)

错误消息

javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: No trusted certificate found at sun.security.ssl.Alerts.getSSLException(Unknown Source) at sun.security.ssl.SSLSocketImpl.fatal(Unknown Source) at sun.security.ssl.Handshaker.fatalSE(Unknown Source) at sun.security.ssl.Handshaker.fatalSE(Unknown Source) at sun.security.ssl.ClientHandshaker.serverCertificate(Unknown Source) at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source) at sun.security.ssl.Handshaker.processLoop(Unknown Source) at sun.security.ssl.Handshaker.process_record(Unknown Source) at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source) at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source) at sun.net.www.protocol.https.HttpsClient.afterConnect(Unknown Source) at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(Unknown Source) at sun.net.www.protocol.https.HttpsURLConnectionImpl.connect(Unknown Source) at mypack.MasterCardDemo.createOpenAPIConnection(MasterCardDemo.java:183) at mypack.MasterCardDemo.main(MasterCardDemo.java:64) Caused by: sun.security.validator.ValidatorException: No trusted certificate found at sun.security.validator.SimpleValidator.buildTrustedChain(Unknown Source) at sun.security.validator.SimpleValidator.engineValidate(Unknown Source) at sun.security.validator.Validator.validate(Unknown Source) at sun.security.ssl.X509TrustManagerImpl.validate(Unknown Source) at sun.security.ssl.X509TrustManagerImpl.checkTrusted(Unknown Source) at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(Unknown Source)

最佳答案

这实际上是一个 SSL 问题,与 MasterCard 的 OAuth 关系不大。您需要说服 Java 信任 https://api.mastercard.com 上的 SSL 证书。您将 OAuth 证书放入信任存储区,而不是 MasterCard 证书。比将 key 放入信任存储中更好的解决方案是信任根 CA。在 Java 中,您可以通过使用默认套接字因子来完成此操作。替换此代码:

con.setSSLSocketFactory(getSocketFactory());

这样:

con.setSSLSocketFactory((SSLSocketFactory)SSLSocketFactory.getDefault());

关于java - MasterCard API - 添加证书请求后出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12813732/

44 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com