使用HTTPS和PEM证书Java客户端

问题描述 投票:1回答:2

我需要创建一个Java客户端消耗使用HTTPS肥皂web服务。

我可以得到授权的工作,我可以通过在命令行中执行以下命令下载的WSDL文件:

wget的--certificate = undisclosed.crt.pem https://service.an-organization.com/foo/bar?wsdl

(不是真的,例子已经模糊了一下)

我的问题是,我有一个很难找到一个Java的例子,我可以用它来获得从Java客户端的工作是一样的。我目前还不能确定该证书应如何处理。我相信,我不想与密钥库拨弄,而是编程提供的证书。

最终目标是使用扩展javax.xml.ws.Service一些生成的存根。这样的例子将是美好的。不过,我会很乐意与香草Java客户端,这是他们只下载wsdl文件像我能够使用wget做多。

请包括任何进口,以及如果你使用一个库中的任何Maven的坐标。

java ssl soap https pem
2个回答
0
投票

我在这里有最好的成功与实际创建一个密钥文件来验证我的客户端类(和/或信任到信任的服务器),但随后加载程序,而不是作为一个系统属性为JVM。

所以,第一步,导入你的私钥和证书到密钥库(叹气......痛苦,但只能做一次,至少),然后第二步,做这样的事情(这使用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);
    }
}

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;
}
© www.soinside.com 2019 - 2024. All rights reserved.