我需要使用https创建一个使用soap webservice的Java客户端。
我可以获得授权工作,我可以通过从命令行发出此命令来下载wsdl文件:
wget --certificate = undisclosed.crt.pem https://service.an-organization.com/foo/bar?wsdl
(不是真的,这个例子已被混淆了一点)
我的问题是我很难找到一个Java示例,我可以用来从Java客户端获取相同的东西。我目前还不确定如何处理证书。我确信我不想摆弄密钥库,而是以编程方式提供证书。
最终目标是使用一些扩展javax.xml.ws.Service的生成存根。这样的例子会很精彩。但我会对一个能够下载wsdl文件的vanilla Java客户端感到非常满意,就像我能够使用wget一样。
如果您使用图书馆,请包含所有导入以及任何Maven坐标。
答案 0 :(得分:0)
我在这里取得了最大的成功,实际创建了一个密钥库文件来验证我的客户端类(和/或信任库以信任服务器),然后以编程方式加载它,而不是作为JVM的系统属性。 / p>
所以,第一步,将你的私钥和证书导入密钥库(叹息......痛苦,但至少只做一次),然后是第二步,做这样的事情(这对某些部分使用Spring) :
/**
* Loads an SSL context given the specified properties.
*
* @return An SSL context created using the given keystore and truststore properties
* @throws KeyManagementException
*/
@Bean
public SSLContext getSSLContext() throws KeyManagementException{
SslConfigurator sslConfig = SslConfigurator.newInstance();
if(!this.trustStore.isEmpty()){
sslConfig
.trustStore(loadKeyStore(this.trustStore, this.trustStoreType, this.truststorePassword))
.trustStorePassword(this.truststorePassword);
}
if(!this.keyStore.isEmpty()){
sslConfig
.keyStore(loadKeyStore(this.keyStore, this.keystoreType, this.keystorePassword))
.keyStorePassword(this.keystorePassword);
}
return sslConfig.createSSLContext();
}
/**
* Loads a keystore from the classpath
* @param name the name of the keystore resource
* @param type the type of the keystore
* @param password the password of the keystore
* @return the keystore
*/
private KeyStore loadKeyStore(String name, String type, String password) {
try {
KeyStore keyStore = KeyStore.getInstance(type);
keyStore.load(this.applicationContext.getResource(name).getInputStream(), password.toCharArray());
return keyStore;
} catch (Exception e) {
throw new InvalidValueException("Could not read keystore", e);
}
}
答案 1 :(得分:0)
//import cert into jdk using cmd key tool command (google it)
import javax.xml.soap.SOAPConnection;
import javax.xml.soap.SOAPConnectionFactory;
import javax.xml.soap.SOAPMessage;
public SOAPMessage send(SOAPMessage requestMessage, String url) throws Exception {
System.setProperty("javax.net.ssl.keyStore", "C:\\cert.jks");
System.setProperty("javax.net.ssl.keyStorePassword", "password");
System.setProperty("javax.net.ssl.trustStore", "cacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "password");
// CreateApplication SOAP Connection
SOAPConnectionFactory soapConnectionFactory = SOAPConnectionFactory.newInstance();
SOAPConnection soapConnection = soapConnectionFactory.createConnection();
// Send SOAP Message to SOAP Server
SOAPMessage soapResponse = soapConnection.call(requestMessage, url);
// print SOAP Response
System.out.print("Response SOAP Message:");
soapResponse.writeTo(System.out);
soapConnection.close();
return soapResponse;
}