gpt4 book ai didi

Java TLS 客户端在访问 soap 服务时找不到证书

转载 作者:行者123 更新时间:2023-11-28 22:20:44 27 4
gpt4 key购买 nike

我有一个在 Tomcat 中运行的 Java 应用程序。它正在尝试访问经过 TLS 客户端证书验证的 SOAP 服务。所有使用的证书都是自签名的。我通过 Tomcat 中的 Java 选项指定了 trustStore 和 keyStore,并且在创建我的服务之前,我还尝试在代码中为所有需要的属性执行此操作。 keyStore 和 trustStore 都是带有 RSA 证书的 JKS。我已验证服务器的 trustStore 中存在证书,客户端 keystore 中存在自客户端证书,并且哈希值相同。

客户端:

c:\Program Files\Java\jre7\bin>keytool -list -keystore c:\tomcat\certs\tomcat.keystore
Enter keystore password:

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

tomcat, Jan 20, 2014, PrivateKeyEntry,
Certificate fingerprint (SHA1): 78:F3:30:A0:40:B0:CC:8D:86:1F:99:FF:7C:3B:85:7C:6D:C7:F2:D2

服务器端:

C:\Program Files (x86)\Java\jre7\bin>keytool -list -keystore c:\tomcat\certs\tomcat.truststore
Enter keystore password:

Keystore type: JKS
Keystore provider: SUN

Your keystore contains 1 entry

clientcert, Jan 23, 2014, trustedCertEntry,
Certificate fingerprint (SHA1): 78:F3:30:A0:40:B0:CC:8D:86:1F:99:FF:7C:3B:85:7C:6D:C7:F2:D2

当我启动 Tomcat 时,我看到像这样加载 keystore :

keyStore is : c:\tomcat\certs\tomcat.keystore
keyStore type is : JKS
keyStore provider is :
init keystore
init keymanager of type SunX509
***
found key for : tomcat
chain [0] = [
[
Version: V3
Subject: CN=Name, OU=Engineering, O=MyOrg, ST=NJ, C=US

...

在服务器问候结束时,我看到了我期望的证书 DN,它是从上面的 keystore 加载的:

*** CertificateRequest
Cert Types: RSA, DSS, ECDSA
Cert Authorities:
<CN=Name, OU=Engineering, O=MyOrg, ST=NJ, C=US>
[read] MD5 and SHA1 hashes: len = 115

...

然后在 ServerHelloDone 之后我立即看到一个空的证书链,没有像我期望看到的那样记录“匹配的别名:mycert”。

*** ServerHelloDone
[read] MD5 and SHA1 hashes: len = 4
0000: 0E 00 00 00 ....
*** Certificate chain
***

服务器端我得到这个:

http-nio-8443-exec-2, READ: TLSv1 Handshake, length = 269
*** Certificate chain
***
http-nio-8443-exec-2, fatal error: 42: null cert chain
javax.net.ssl.SSLHandshakeException: null cert chain
%% Invalidated: [Session-1, TLS_RSA_WITH_AES_128_CBC_SHA]
http-nio-8443-exec-2, SEND TLSv1 ALERT: fatal, description = bad_certificate
http-nio-8443-exec-2, WRITE: TLSv1 Alert, length = 2
http-nio-8443-exec-2, fatal: engine already closed. Rethrowing javax.net.ssl.SSLHandshakeException: null cert chain
http-nio-8443-exec-2, called closeOutbound()
http-nio-8443-exec-2, closeOutboundInternal()

当我获取客户端的证书并将其加载到我的浏览器并导航到该站点时,系统会提示我选择证书并能够看到 wsdl 等。所以我相信证书没问题。

Java 客户端使用它在 Java 选项中指定的 trustStore 来验证服务器,这让我相信 trustStore 已加载并使用得很好。最后,这是我认为负责创建服务的客户端代码:

System.setProperty("javax.net.ssl.keyStore","C:\\tomcat\\certs\\tomcat.keystore");
System.setProperty("javax.net.ssl.keyStorePassword", "password");
System.setProperty("javax.net.ssl.keyStoreType", "JKS");
System.setProperty("javax.net.ssl.trustStore","C:\\tomcat\\certs\tomcat.truststore");
System.setProperty("javax.net.ssl.trustStorePassword", "password");
System.setProperty("javax.net.ssl.trustStoreType", "JKS");
URL url = DocumentRepositoryProxy.class.getClassLoader().getResource("XDS.b_DocumentRepositoryWSDLSynchMTOM.wsdl");

QName qname = new QName("urn:ihe:iti:xds-b:2007", "DocumentRepository_Service");
DocumentRepositoryService service = new DocumentRepositoryService(url, qname);

if (handlerResolver != null)
service.setHandlerResolver(handlerResolver);

proxy = service.getDocumentRepositoryPortSoap12(new MTOMFeature(true, 1));

BindingProvider bp = (BindingProvider) proxy;
SOAPBinding binding = (SOAPBinding) bp.getBinding();
binding.setMTOMEnabled(true);

bp.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpoint);

我花了大半个星期的时间在这个兔子洞里摸索,如果有人能帮忙的话,我会省点事的。

最佳答案

答案在这里。在我的 WebApp 的 WEB-INF/cxf-servlet.xml 文件中,我需要添加这个 XML block 和关联的命名空间。代码中的 Java 选项被完全忽略,所以我删除了它们。一旦我添加它并重新启动 Tomcat,第一次尝试一切正常。

命名空间:

xmlns:sec="http://cxf.apache.org/configuration/security"
xmlns:http="http://cxf.apache.org/transports/http/configuration"

元素:

<http:conduit name="*.http-conduit">
<http:tlsClientParameters>
<sec:keyManagers keyPassword="XXX">
<sec:keyStore type="JKS"
password="password"
file="C:/tomcat/certs/tomcat.keystore"/>
</sec:keyManagers>
<sec:trustManagers>
<sec:keyStore type="JKS"
password="password"
file="C:\tomcat\certs\tomcat.truststore"/>
</sec:trustManagers>
</http:tlsClientParameters>
</http:conduit>

关于Java TLS 客户端在访问 soap 服务时找不到证书,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21323708/

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