简体   繁体   English

使用https和pem证书的Java客户端

[英]Java client using https and a pem certificate

I need to create a Java client that consumes a soap webservice using https. 我需要创建一个使用https使用肥皂网络服务的Java客户端。

I can get authorization to work and I can download the wsdl file by issuing this command from a command line: 我可以得到授权才能工作,并且可以通过从命令行发出以下命令来下载wsdl文件:

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

(Not really, the example has been obfuscated a bit) (不是,这个例子有点混乱了)

My problem is that I have a hard time to find a Java example I can use to get the same thing working from a Java client. 我的问题是,我很难找到一个Java示例,可以用来从Java客户端获得相同的功能。 I'm currently not certain how the certificate should be handled. 我目前不确定该如何处理证书。 I am sure that I don't want to fiddle with the keystore and instead supply the certificate programatically. 我确定我不想摆弄密钥库,而是以编程方式提供证书。

The ultimate goal is to use some generated stubs that extends javax.xml.ws.Service. 最终目标是使用一些扩展了javax.xml.ws.Service的生成的存根。 Such an example would be wonderful. 这样的例子将是很棒的。 But I would be more than happy with a vanilla Java client that was able just to download the wsdl file like I'm able to do using wget. 但是,我对能够像我使用wget一样能够下载wsdl文件的香草Java客户端感到非常满意。

Please include any imports as well as any Maven coordinates if you use a library. 如果使用库,请包括所有导入以及任何Maven坐标。

I've had best success here with actually creating a keystore file to authenticate my client class (and/or truststore to trust the server) but then load it programmatically, rather than as a system property for the JVM. 在这里,我通过实际创建一个密钥库文件来认证我的客户端类(和/或信任库来信任服务器),然后以编程方式加载它,而不是将其作为JVM的系统属性来获得了最大的成功。

So, step one, import your private key and certificate into a keystore (sigh... painful, but only done once, at least), and then step two, do something like this (this uses Spring for some parts): 因此,第一步,将您的私钥和证书导入密钥库(叹气……很痛苦,但至少只做一次),然后第二步,做类似的事情(这在某些部分使用了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);
    }
}
//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;
}

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

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