gpt4 book ai didi

java - 如何正确创建 2 路 TLS 的 SSLSocketFactory? (适用于苹果支付)

转载 作者:行者123 更新时间:2023-12-01 17:32:28 26 4
gpt4 key购买 nike

我正在尝试向 ApplePay 发出请求,但无法使用 Java 来执行此操作。

这是 curl 请求:

$ curl -X POST --cert-type P12 --cert cert.p12 https://apple-pay-gateway-pr-pod1.apple.com/paymentservices/startSession -d '{}'
{
"statusMessage": "Payment Services Exception Invalid session request",
"statusCode": "400"
}

只要能够与苹果服务器通信就正常。

这是我生成新 Feign 客户端 SSLContextFactory 的代码(用 Kotlin 编写):

   @Bean
fun client(): Client { // a Feign Client
val keystoreFile = File("/path/to/cert.p12")

val keyStore = KeyStore.getInstance("PKCS12")
keyStore.load(keystoreFile.inputStream(), null)
val keyFactory = KeyManagerFactory.getInstance(KeyManagerFactory.getDefaultAlgorithm());
keyFactory.init(keyStore, null)
val trustManagerFactory = TrustManagerFactory.getInstance(
TrustManagerFactory.getDefaultAlgorithm()
)
trustManagerFactory.init(keyStore)
val trustManagers = trustManagerFactory.trustManagers
val trustManager = trustManagers[0] as X509TrustManager
val sslContext: SSLContext = SSLContext.getInstance("TLS")
sslContext.init(keyFactory.keyManagers, trustManagers, null)

return feign.okhttp.OkHttpClient(
FeignConfiguration.enrichOkHttpClientBuilder(logbook)
.sslSocketFactory(sslContext.socketFactory, trustManager)
.connectionSpecs(
arrayListOf(
ConnectionSpec.Builder(ConnectionSpec.MODERN_TLS)
.tlsVersions(TlsVersion.TLS_1_2)
.cipherSuites(
CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
, CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA256
, CipherSuite.TLS_ECDHE_RSA_WITH_AES_128_CBC_SHA
, CipherSuite.TLS_DHE_RSA_WITH_AES_128_GCM_SHA256
, CipherSuite.TLS_DHE_RSA_WITH_AES_128_CBC_SHA256
, CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256
, CipherSuite.TLS_RSA_WITH_AES_128_GCM_SHA256
)
.build()
)
)
.build()
)
}

但是当我尝试向同一 URL 发出请求时,出现以下错误:

feign.RetryableException: java.lang.RuntimeException: Unexpected error: java.security.InvalidAlgorithmParameterException: the trustAnchors parameter must be non-empty executing POST https://apple-pay-gateway-pr-pod1.apple.com/paymentservices/startSession/paymentSession

有人知道我可能做错了什么吗?

最佳答案

我的技术主管能够帮助解决此问题,但@dave_thompson_085 的回答也应该有所帮助。

所以我基本上用以下内容替换了假客户端之前的部分:

        val keystoreStream = <stream>

val keyStore = KeyStore.getInstance("PKCS12")
keyStore.load(keystoreStream, keystorePassword.toCharArray())

val sslContext: SSLContext = SSLContexts.custom()
.loadTrustMaterial(null, TrustSelfSignedStrategy())
.loadKeyMaterial(keyStore, keystorePassword.toCharArray())
.build()

val tmf = TrustManagerFactory
.getInstance(TrustManagerFactory.getDefaultAlgorithm())
tmf.init(keyStore) // can use tmf to get individual trustmanager

关于java - 如何正确创建 2 路 TLS 的 SSLSocketFactory? (适用于苹果支付),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61093306/

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