简体   繁体   English

服务器需要客户端证书时如何使用wsimport?

[英]How to use wsimport when server expects client certificate?

I have a web service using mutual SSL authentication. 我有一个使用相互SSL身份验证的Web服务。 I can access it just fine in the browser when I have the client's certificate installed. 当我安装了客户端证书时,我可以在浏览器中正常访问它。

I need to be able to access this using wsimport for generating the Java code to access the service. 我需要能够使用wsimport访问它,以生成访问服务的Java代码。

How can I set up my credentials so that I may access the URL using wsimport ? 如何设置凭据以便我可以使用wsimport访问URL?

Here's an example of what I'm trying, but it times out due to the inability to authenticate. 这是我正在尝试的一个例子,但由于无法进行身份验证而超时。

wsimport ./sample.wsdl -p com.company.ws.sample -Xnocompile -d ./src -extension -keep -XadditionalHeaders

Thanks for any help 谢谢你的帮助

Edit: 编辑:

This is what wsimport prints. 这是wsimport打印的内容。 The WSDL is definitely valid, and at the given location, it's a matter of figuring out how to pass in my credentials for authenticating: WSDL绝对有效,在给定的位置,这是一个确定如何传递我的凭据进行身份验证的问题:

wsimport https://wsdl.location.com?WSDL -p com.company.ws.sample -Xnocompile 
-d ./src -extension -keep -XadditionalHeaders


parsing WSDL...

[ERROR] Received fatal alert: handshake_failure

Failed to read the WSDL document: "https://wsdl.location.com?WSDL", because 1) could 
not find the document; /2) the document could not be read; 3) the root element of 
the document is not <wsdl:definitions>.

[ERROR] failed.noservice=Could not find wsdl:service in the provided WSDL(s): 

 At least one WSDL with at least one service definition needs to be provided.

    Failed to parse the WSDL.

You can call directly the WsImport java class ( source ) and add the JVM necessary args for Java knowing where to look for client certificates. 您可以直接调用WsImport java类( 源代码 )并为Java添加JVM必需的args,知道在哪里查找客户端证书。

Something like 就像是

java -classpath C:\jdk160_29\lib\tools.jar -Djavax.net.ssl.trustStore=c:\jdk160_29\.mykeystore com.sun.tools.internal.ws.WsImport https://host:8443/Webservice?wsdl -p com.test -s ./src"

should do the trick. 应该做的伎俩。

I managed to do this by setting the _JAVA_OPTIONS environment variable with all the extra system properties to pass through; 我设法通过设置_JAVA_OPTIONS环境变量以及要传递的所有额外系统属性来实现此目的; as a Windows batch file it looks like this (putting your password in as appropriate: 作为Windows批处理文件,它看起来像这样(将密码放入适当的位置:

setlocal
set _JAVA_OPTIONS=%_JAVA_OPTIONS% -Djavax.net.ssl.trustStore="%JAVA_HOME%\jre\lib\security\cacerts" -Djavax.net.ssl.keyStoreType=PKCS12 -Djavax.net.ssl.keyStorePassword={...passwordForThePFX...} -Djavax.net.ssl.keyStore=r:\cert.pfx
wsimport -s . -verbose https://your.host.name/path/to/service?wsdl
endlocal

For sanity's sake, the Java options in the long 'set' line are: 为了理智,长'set'行中的Java选项是:

-Djavax.net.ssl.trustStore="%JAVA_HOME%\jre\lib\security\cacerts"
-Djavax.net.ssl.keyStoreType=PKCS12
-Djavax.net.ssl.keyStorePassword={...passwordForThePFX...}   
-Djavax.net.ssl.keyStore=R:\cert.pfx

You may or may not need to specift the trustStore setting; 您可能需要也可能不需要调整trustStore设置; I had to as I've got a couple of installs on and Java was picking up the wrong cacerts file for me. 我必须安装几个,而Java正在为我挑选错误的cacerts文件。

Similarly you won't need the keyStorePassword if the keystore isn't password protected. 同样,如果密钥库没有密码保护,则不需要keyStorePassword As for keyStoreType , you'll need to specify this if you're not accessing a Java keystore. 对于keyStoreType ,如果您没有访问Java密钥库,则需要指定它。

Ultimately, the only "mandatory" option is the keyStore which defines where the client certificate and keys live (and it's only mandatory if the client certificate isn't in any of the core Java certificate stores). 最终,唯一的“强制”选项是keyStore ,它定义客户端证书和密钥所在的位置(并且只有在客户端证书不在任何核心Java证书存储区中时才是必需的)。 As it stands, the example above is for a client certificate in a PFX file generated by exporting it from the Windows certificate store. 就目前而言,上面的示例是通过从Windows证书存储区导出PFX文件生成的客户端证书。

I found much easier (no need to play with -D, classpath, ...) to: 我发现更容易(不需要使用-D,classpath,...)来:
1. download the wsdl via ssl with your browser (install the certificate in your default browser by double-click on the keystore) or even easier with soapUI (install the keystore via config/ssl) that shows the wsdl contents 1.使用浏览器通过ssl下载wsdl(通过双击密钥库在默认浏览器中安装证书),或者使用soapUI(通过config / ssl安装密钥库)更简单地显示wsdl内容
2. run the wsimport against the downloaded wsdl 2.对下载的wsdl运行wsimport
That's all. 就这样。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM