Java 和 Tomcat Web 服务中的 SSL 握手问题
我必须使用我的 Java Web 应用程序(在 Tomcat 上运行)在 Axis 中完成的 Web 服务。制作 Web 服务的公司使用 HTTPS 和自签名证书进行测试。
我已经运行 Netbeans 向导来生成基于 WSDL 的 Web 服务,并且操作正确。如果我使用浏览器进入 Web 服务的网站,我会因为 SSL 证书而收到警告,并且我必须创建例外。
当尝试运行我的代码时,在建立 SSL 连接时出现异常。例外情况是:
1.
com.sun.xml.ws.client.ClientTransportException:HTTP 传输错误:javax.net.ssl.SSLHandshakeException:收到致命警报:handshake_failure
和有时(不更改代码)
2。
com.sun.xml.ws.client.ClientTransportException:HTTP 传输错误:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX 路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效认证路径
我想我必须将证书合并到 Java VM 和/或 Tomcat 中,并且还告诉忽略该证书不是受信任的来源。
如何做到这一点?如何正确使用这个安全的网络服务?
如果我提供的信息不够,请询问更多。
谢谢
Ezequiel
更新:
我已经尝试过这两件事,但都没有成功,例外情况是相同的。
选项 1)
System.setProperty("javax.net.ssl.trustStore","/home/serverapp/BSS-cert.p12");
System.setProperty("javax.net.ssl.trustStorePassword","password");
System.setProperty("javax.net.ssl.trustStoreType","PKCS12");
选项 2) KeyStore ks = KeyStore.getInstance( "pkcs12" ); ks.load( new FileInputStream("/home/serverapp/BSS-cert.p12"), "密码".toCharArray() );
KeyStore jks = KeyStore.getInstance( "JKS" );
jks.load( null );
KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
kmf.init( ks, "f0p6k9n2".toCharArray() );
TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" );
tmf.init( jks );
SSLContext ctx = SSLContext.getInstance( "TLS" );
ctx.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null );
另外,我认为问题可能出在 Web 服务上,因此我尝试建立 HTTPS 连接,但在打开输入流时失败并出现相同的错误。
String httpsURL = "https://serverurl:443/theservice?wsdl";
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
InputStream ins = con.getInputStream(); //Exception here!
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader in = new BufferedReader(isr);
I have to consume a webservice done in Axis with my Java web application (that runs on Tomcat). The company that made the webservice uses HTTPS and a certificate self signed for testing.
I have run a Netbeans wizzard to generate a Webservice based on the WSDL, and that is done correctly. If I enter to the website of the webservice using a browser, I get a warning because of the SSL certificate, and I have to create an exception.
When trying to run my code, I get exceptions when the SSL connection is made. The exceptions are:
1.
com.sun.xml.ws.client.ClientTransportException: HTTP transport error: javax.net.ssl.SSLHandshakeException: Received fatal alert: handshake_failure
and some times (without changing the code)
2.
com.sun.xml.ws.client.ClientTransportException: HTTP transport error: 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
I imagine that I have to incorporate a certificate into the Java VM and/or Tomcat, and also tells to ignore that is not a trusted source.
How to do this? How to consume this secure webservice correctly?
If the information I provide is not enough, please ask for more.
Thanks
Ezequiel
UPDATE:
I have tried this two things, both without success, the exceptions are the same.
Option 1)
System.setProperty("javax.net.ssl.trustStore","/home/serverapp/BSS-cert.p12");
System.setProperty("javax.net.ssl.trustStorePassword","password");
System.setProperty("javax.net.ssl.trustStoreType","PKCS12");
Option 2)
KeyStore ks = KeyStore.getInstance( "pkcs12" );
ks.load( new FileInputStream("/home/serverapp/BSS-cert.p12"), "password".toCharArray() );
KeyStore jks = KeyStore.getInstance( "JKS" );
jks.load( null );
KeyManagerFactory kmf = KeyManagerFactory.getInstance( "SunX509" );
kmf.init( ks, "f0p6k9n2".toCharArray() );
TrustManagerFactory tmf = TrustManagerFactory.getInstance( "SunX509" );
tmf.init( jks );
SSLContext ctx = SSLContext.getInstance( "TLS" );
ctx.init( kmf.getKeyManagers(), tmf.getTrustManagers(), null );
Also, as I thought that may be the problem was the web service, I tried to stablish an HTTPS connection, and it fails with the same error when openning an input stream.
String httpsURL = "https://serverurl:443/theservice?wsdl";
URL myurl = new URL(httpsURL);
HttpsURLConnection con = (HttpsURLConnection)myurl.openConnection();
InputStream ins = con.getInputStream(); //Exception here!
InputStreamReader isr = new InputStreamReader(ins);
BufferedReader in = new BufferedReader(isr);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
这里有两种常见的方法:
http://ws.apache.org/xmlrpc/ssl.html
WebLogic 有自己的东西:
http://download.oracle.com/docs /cd/E12840_01/wls/docs103/security/SSL_client.html#wp1029670
Two common approaches here:
http://ws.apache.org/xmlrpc/ssl.html
WebLogic has its own stuff:
http://download.oracle.com/docs/cd/E12840_01/wls/docs103/security/SSL_client.html#wp1029670
我认为您必须进行配置,使未签名的证书可以被信任。也许
I think you have to configure, that the unsigned certificate can be trusted. Maybe this will help you.
为了信任证书,您必须告诉 java 您信任签署该证书的证书颁发机构。对于自签名证书,即是证书本身。您需要将 javax.net.ssl.trustStore 系统属性设置为包含您要连接的服务器的证书的 CA 的密钥库。
当您获取证书以解决不受信任的连接错误时,您也许能够从浏览器获取证书。尝试找到一个选项以 PKCS12 格式导出证书,保存该文件。运行代码时,将 javax.net.ssl.trustStore 属性设置为刚刚保存的文件,并将 javax.net.ssl.trustStoreType 设置为 PKCS12。
In order to trust a certificate, you have to tell java that you trust the certificate authority that signed the cert. In the case of a self-signed cert, that is the cert itself. You need to set the javax.net.ssl.trustStore system property to a keystore that contains the CA for the cert of the server you're connecting to.
You may be able to get the cert from your browser when you fetch it to get through the untrusted connection error. Try finding an option to export the cert in PKCS12 format, save that file. When you run your code, set the javax.net.ssl.trustStore property to the file you just saved and also set the javax.net.ssl.trustStoreType to PKCS12.
可能缺少正确的 SSL 认证。阅读此博客文章:http://blogs.oracle.com/andreas/entry/no_more_unable_to_find 这里给出的程序曾经帮助过我一次。
perhaps missing proper SSL certification. Read this blog entry: http://blogs.oracle.com/andreas/entry/no_more_unable_to_find the program given here helped me once.