java - 使用 Axis2/Java 创建 SSL 客户端

标签 java ssl apache-axis

我正在尝试连接到使用 SSL 但未成功的 Web 服务。我使用 Axis2,我发现了一些有用的文章:http://people.apache.org/~dumindu/docs/HowToConfigureSSL.html ,但它适用于 C。在本文中,他们使用 axis2.xml 或 C 编码将路径设置为 SERVER_CERT、KEY_FILE 和 SSL_PASSPHRASE。我试图更改配置文件,但这对我不起作用。如果有人知道如何从 Java 代码中设置此参数,请告诉我。

最佳答案

我初始化了EasySSLProtocolSocketFactory和不同端点的协议(protocol)实例,并使用唯一 key 注册协议(protocol),如下所示:

/**
 * This method does the following:
 * 1. Creates a new and unique protocol for each SSL URL that is secured by client certificate
 * 2. Bind keyStore related information to this protocol
 * 3. Registers it with HTTP Protocol object 
 * 4. Stores the local reference for this custom protocol for use during furture collect calls
 * 
 *  @throws Exception
 */
public void registerProtocolCertificate() throws Exception {
    EasySSLProtocolSocketFactory easySSLPSFactory = new EasySSLProtocolSocketFactory();
    easySSLPSFactory.setKeyMaterial(createKeyMaterial());
    myProtocolPrefix = (HTTPS_PROTOCOL + uniqueCounter.incrementAndGet());
    Protocol httpsProtocol = new Protocol(myProtocolPrefix,(ProtocolSocketFactory) easySSLPSFactory, port);
    Protocol.registerProtocol(myProtocolPrefix, httpsProtocol);
    log.trace("Protocol [ "+myProtocolPrefix+" ] registered for the first time");
}

/**
 * Load keystore for CLIENT-CERT protected endpoints
 */
private KeyMaterial createKeyMaterial() throws GeneralSecurityException, Exception  {
    KeyMaterial km = null;
    char[] password = keyStorePassphrase.toCharArray();
    File f = new File(keyStoreLocation);
    if (f.exists()) {
        try {
            km = new KeyMaterial(keyStoreLocation, password);
            log.trace("Keystore location is: " + keyStoreLocation + "");
        } catch (GeneralSecurityException gse) {
            if (logErrors){
                log.error("Exception occured while loading keystore from the following location: "+keyStoreLocation, gse);
                throw gse;
            }
        }
    } else {
        log.error("Unable to load Keystore from the following location: " + keyStoreLocation );
        throw new CollectorInitException("Unable to load Keystore from the following location: " + keyStoreLocation);
    }
    return km;
}   

当我必须调用 Web 服务时,我会这样做(基本上将 URL 中的“https”替换为 https1、https2 或其他内容,具体取决于您为该特定端点初始化的协议(protocol)):

httpClient.getHostConfiguration().setHost(host, port,Protocol.getProtocol(myProtocolPrefix));
initializeHttpMethod(this.url.toString().replace(HTTPS_PROTOCOL, myProtocolPrefix));

它就像一个魅力!

关于java - 使用 Axis2/Java 创建 SSL 客户端,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6465907/

相关文章:

java - 在jboss 7中为Activemq配置池连接jndi

java - 通过 WSO2ESB 访问 NTLM 安全 WS

java - 生成随机的简单数学除法问题

java - 当服务器拥有来自 CA 的有效证书时,Web 服务 (https) 的客户端 JVM 会抛出 SSL 异常吗?

java - Http URLConnection 等待内部请求

java - RMI over SSL - 通信速度慢

python - 让 Ansible 信任我在 macOS 和 CentOS 上的自定义 CA

java - Apache Axis 2 启动时加载类方法

java - 如何使用 matlabcontrol.jar 从 Java 调用用户定义的 Matlab

java - 找不到spring-boot-2.3.0.BUILD-SNAPSHOT.jar错误