使用Maven的Web服务客户端 - SSL证书

时间:2014-07-16 08:53:48

标签: java web-services maven ssl https

我正在尝试使用带有wsimport目标的jaxws-maven-plugin创建Web服务客户端,使用此pom:

<plugin>
        <groupId>org.jvnet.jax-ws-commons</groupId>
        <artifactId>jaxws-maven-plugin</artifactId>
        <version>2.2</version>
        <executions>
            <execution>
                <goals>
                    <goal>wsimport</goal>
                </goals>
            </execution>
        </executions>
        <configuration>
            <wsdlUrls>
                <wsdlUrl>
                    https://test.test/WSTtest?wsdl
                </wsdlUrl>
            </wsdlUrls>
            <verbose>true</verbose>
        </configuration>

        <dependencies>
            <dependency>
                <groupId>javax.xml</groupId>
                <artifactId>webservices-api</artifactId>
                <version>1.4</version>
            </dependency>
        </dependencies>
    </plugin>

但是我收到了错误

[错误] java.security.cert.CertificateException:找不到与找到的IP地址93.92.169.114匹配的主题替代名称

因为我需要证书。当我在SSL client certificate in Maven阅读时,我在POM中添加了与认证文件位置相关的属性:

<plugin> 
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>properties-maven-plugin</artifactId>
    <version>1.0-alpha-2</version>
    <executions>
      <execution>
        <goals>
          <goal>set-system-properties</goal>
        </goals>
        <configuration>
          <properties>
            <property>
              <name>javax.net.ssl.trustStore</name>
              <value>c:\certificate.crt</value>
            </property>
          </properties>
        </configuration>
      </execution>
    </executions>
    </plugin>

但现在我得到了其他错误:

[ERROR] java.security.NoSuchAlgorithmException:构造实现时出错(算法:默认,提供者:SunJSSE,类:sun.security.ssl.SSLContextImpl $ DefaultSSLContext)

我在Java Exception on SSLSocket creation读到的内容似乎与认证文件的格式有关。我试图通过在POM中添加具有相同结果的属性来指示文件的类型。

<properties>
             <property>
              <name>javax.net.ssl.trustStoreType</name>
              <value>JCEKS</value>
            </property>
            <property>
              <name>javax.net.ssl.trustStore</name>
              <value>c:\certificate.crt</value>
            </property>

          </properties>

我知道如何解决这个问题?我做错了什么?这是指示证书文件在哪里的正确方法吗?

谢谢

1 个答案:

答案 0 :(得分:0)

好吧,您可以尝试禁用所有SSL信任检查:

import java.security.AccessController;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.PrivilegedAction;
import java.security.Security;
import java.security.cert.X509Certificate;  
import javax.net.ssl.ManagerFactoryParameters;
import javax.net.ssl.TrustManager;
import javax.net.ssl.TrustManagerFactorySpi;
import javax.net.ssl.X509TrustManager;

public final class XTrustProvider extends java.security.Provider {

        private final static String NAME = "XTrustJSSE";
        private final static String INFO = "XTrust JSSE Provider (implements trust factory with truststore validation disabled)";
        private final static double VERSION = 1.0D;

        public XTrustProvider() {
                super(NAME, VERSION, INFO);

                AccessController.doPrivileged(new PrivilegedAction() {
                        public Object run() {
                                put("TrustManagerFactory." + TrustManagerFactoryImpl.getAlgorithm(), TrustManagerFactoryImpl.class.getName());
                                return null;
                        }
                });
        }

        public static void install() {
                if(Security.getProvider(NAME) == null) {
                        Security.insertProviderAt(new XTrustProvider(), 2);
                        Security.setProperty("ssl.TrustManagerFactory.algorithm", TrustManagerFactoryImpl.getAlgorithm());
                }
        }

        public final static class TrustManagerFactoryImpl extends TrustManagerFactorySpi {
                public TrustManagerFactoryImpl() { }
                public static String getAlgorithm() { return "XTrust509"; }
                protected void engineInit(KeyStore keystore) throws KeyStoreException { }
                protected void engineInit(ManagerFactoryParameters mgrparams) throws InvalidAlgorithmParameterException {
                        throw new InvalidAlgorithmParameterException( XTrustProvider.NAME + " does not use ManagerFactoryParameters");
                }

                protected TrustManager[] engineGetTrustManagers() {
                        return new TrustManager[] {
                                new X509TrustManager() {
                                        public X509Certificate[] getAcceptedIssuers() { return null; }
                                        public void checkClientTrusted(X509Certificate[] certs, String authType) { }
                                        public void checkServerTrusted(X509Certificate[] certs, String authType) { }
                                }
                        };
                }
        }
}

简单调用XTrustProvider.install();以禁用所有证书检查。也许这种解决方法也解决了您的问题。但请记住,这只是一种解决方法。