使用jaxb从wsdl生成java代码,需要pkcs11 auth over https

时间:2016-12-02 00:41:01

标签: soap https jaxb wsdl pkcs#11

我在从需要pkcs11(smatcard)身份验证的端点生成wsdl2java代码时遇到问题。我已经配置了像这样的jaxb maven插件:

   <plugin>
            <groupId>org.jvnet.jaxb2.maven2</groupId>
            <artifactId>maven-jaxb2-plugin</artifactId>
            <version>0.13.1</version>
            <executions>
                <execution>
                    <goals>
                        <goal>generate</goal>
                    </goals>
                </execution>
            </executions>
            <configuration>
                <schemaLanguage>WSDL</schemaLanguage>
                <generatePackage>hello.wsdl</generatePackage>
                <schemas>
                    <schema>
                        <url>https://mysecuredwsdl.com?wsdl</url>
                    </schema>
                </schemas>
            </configuration>
 </plugin>

当我执行生成任务时,我收到此错误:

  

[ERROR]解析模式时出错.Location []。   com.sun.istack.SAXParseException2;处理&#34; https://...?wsdl&#34;时抛出IOException。异常:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到所请求目标的有效证书路径。       at com.sun.tools.xjc.ErrorReceiver.error(ErrorReceiver.java:94)       在com.sun.tools.xjc.reader.internalizer.DOMForest.parse(DOMForest.java:402)       在com.sun.tools.xjc.reader.internalizer.DOMForest.parse(DOMForest.java:274)       在com.sun.tools.xjc.ModelLoader.buildDOMForest(ModelLoader.java:324)       at com.sun.tools.xjc.ModelLoader.loadWSDL(ModelLoader.java:391)       在com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:170)       在com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:119)       在org.jvnet.mjiip.v_2_2.XJC22Mojo.loadModel(XJC22Mojo.java:50)       在org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:40)       在org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:28)       at org.jvnet.jaxb2.maven2.RawXJC2Mojo.doExecute(RawXJC2Mojo.java:505)       在org.jvnet.jaxb2.maven2.RawXJC2Mojo.execute(RawXJC2Mojo.java:328)       在org.apache.maven.plugin.DefaultBuildPluginManager.executeMojo(DefaultBuildPluginManager.java:101)       在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:209)       在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:153)       在org.apache.maven.lifecycle.internal.MojoExecutor.execute(MojoExecutor.java:145)       在org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:84)       在org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject(LifecycleModuleBuilder.java:59)       在org.apache.maven.lifecycle.internal.LifecycleStarter.singleThreadedBuild(LifecycleStarter.java:183)       在org.apache.maven.lifecycle.internal.LifecycleStarter.execute(LifecycleStarter.java:161)       在org.apache.maven.DefaultMaven.doExecute(DefaultMaven.java:320)       在org.apache.maven.DefaultMaven.execute(DefaultMaven.java:156)       在org.apache.maven.cli.MavenCli.execute(MavenCli.java:537)       在org.apache.maven.cli.MavenCli.doMain(MavenCli.java:196)       在org.apache.maven.cli.MavenCli.main(MavenCli.java:141)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke(Method.java:498)       在org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced(Launcher.java:290)       在org.codehaus.plexus.classworlds.launcher.Launcher.launch(Launcher.java:230)       在org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode(Launcher.java:409)       在org.codehaus.plexus.classworlds.launcher.Launcher.main(Launcher.java:352)       在org.codehaus.classworlds.Launcher.main(Launcher.java:47)       at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)       at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)       at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)       at java.lang.reflect.Method.invoke(Method.java:498)       在com.intellij.rt.execution.application.AppMain.main(AppMain.java:147)   引起:javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到所请求目标的有效证书路径       at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)       at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1949)       在sun.security.ssl.Handshaker.fatalSE(Handshaker.java:302)       在sun.security.ssl.Handshaker.fatalSE(Handshaker.java:296)       at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1509)       在sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:216)       at sun.security.ssl.Handshaker.processLoop(Handshaker.java:979)       在sun.security.ssl.Handshaker.process_record(Handshaker.java:914)       at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1062)       at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1375)       at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1403)       at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1387)       在sun.net.www.protocol.https.HttpsClient.afterConnect(HttpsClient.java:559)       at sun.net.www.protocol.https.AbstractDelegateHttpsURLConnection.connect(AbstractDelegateHttpsURLConnection.java:185)       at sun.net.www.protocol.http.HttpURLConnection.getInputStream0(HttpURLConnection.java:1513)       at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1441)       at sun.net.www.protocol.https.HttpsURLConnectionImpl.getInputStream(HttpsURLConnectionImpl.java:254)       at com.sun.org.apache.xerces.internal.impl.XMLEntityManager.setupCurrentEntity(XMLEntityManager.java:647)       at com.sun.org.apache.xerces.internal.impl.XMLVersionDetector.determineDocVersion(XMLVersionDetector.java:148)       at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:805)       在com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:770)       在com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)       在com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)       at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl $ JAXPSAXParser.parse(SAXParserImpl.java:643)       在com.sun.tools.xjc.reader.internalizer.DOMForest.parse(DOMForest.java:394)       ......还有37个   引发者:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到所请求目标的有效证书路径       at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:387)       at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)       at sun.security.validator.Validator.validate(Validator.java:260)       at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:324)       at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:229)       at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:124)       at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1491)       ......还有57个   引起:sun.security.provider.certpath.SunCertPathBuilderException:无法找到请求目标的有效证书路径       在sun.security.provider.certpath.SunCertPathBuilder.build(SunCertPathBuilder.java:141)       at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:126)       在java.security.cert.CertPathBuilder.build(CertPathBuilder.java:280)       在sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:382)

现在,我尝试使用以下参数运行maven任务:

-Djavax.net.ssl.keyStore=NONE -Djavax.net.ssl.keyStoreType=PKCS11 -Djavax.net.ssl.keyStorePassword=1111 -Djavax.net.ssl.trustStore=NONE -Djavax.net.ssl.trustStoreType=PKCS11 -Djavax.net.ssl.trustStorePassword=1111 -Djavax.net.ssl.trustStoreProvider=SunPKCS11 -Djavax.net.ssl.keyStoreProvider=SunPKCS11

我收到了这个错误:

  

com.sun.istack.SAXParseException2;处理&#34; https://...?wsdl&#34;时抛出IOException。异常:java.net.SocketException:java.security.NoSuchAlgorithmException:构造实现时出错(算法:默认,提供者:SunJSSE,类:sun.security.ssl.SSLContextImpl $ DefaultSSLContext)。   引起:java.security.NoSuchProviderException:没有这样的提供者:SunPKCS11

之后我试图消除keyStoreProvider和trustedStoreProvider属性:

-Djavax.net.ssl.keyStore=NONE -Djavax.net.ssl.keyStoreType=PKCS11 -Djavax.net.ssl.keyStorePassword=1111 -Djavax.net.ssl.trustStore=NONE -Djavax.net.ssl.trustStoreType=PKCS11 -Djavax.net.ssl.trustStorePassword=1111

我收到了这个错误:

  

[ERROR]解析模式时出错.Location []。   com.sun.istack.SAXParseException2;处理&#34; https://....?wsdl&#34;时抛出IOException。异常:java.net.SocketException:java.security.NoSuchAlgorithmException:构造实现时出错(算法:默认,提供者:SunJSSE,类:sun.security.ssl.SSLContextImpl $ DefaultSSLContext)。       at com.sun.tools.xjc.ErrorReceiver.error(ErrorReceiver.java:94)       在com.sun.tools.xjc.reader.internalizer.DOMForest.parse(DOMForest.java:402)       在com.sun.tools.xjc.reader.internalizer.DOMForest.parse(DOMForest.java:274)       在com.sun.tools.xjc.ModelLoader.buildDOMForest(ModelLoader.java:324)       at com.sun.tools.xjc.ModelLoader.loadWSDL(ModelLoader.java:391)       在com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:170)       在com.sun.tools.xjc.ModelLoader.load(ModelLoader.java:119)       在org.jvnet.mjiip.v_2_2.XJC22Mojo.loadModel(XJC22Mojo.java:50)       在org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:40)       在org.jvnet.mjiip.v_2_2.XJC22Mojo.doExecute(XJC22Mojo.java:28)       at org.jvnet.jaxb2.maven2.RawXJC2Mojo.doExecute(RawXJC2Mojo.java:505)       在org.jvnet.jaxb2.maven2.RawXJC2Mojo.execute(RawXJC2Mojo.java:328)

     

PKCS11 KeyStore不可用

我使用apache http与这个安全的webservice建立了连接。所以我知道我可以连接到它。我使用了这个测试代码:

@Test
public void testCertificate() throws CertificateException, NoSuchAlgorithmException, KeyStoreException, IOException, KeyManagementException, UnrecoverableKeyException {
    // Trust own CA and all self-signed certs
    KeyStore ks = getKeystore();
    ks.load(null, "1111".toCharArray());
    SSLContext sslcontext = SSLContexts.custom().loadTrustMaterial(ks,new TrustSelfSignedStrategy()).loadKeyMaterial(ks,"1111".toCharArray())
            .build();
    // Allow TLSv1 protocol only
    SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
            sslcontext,
            new String[]{"TLSv1"},
            null,
            SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    CloseableHttpClient httpclient = HttpClients.custom()
            .setSSLSocketFactory(sslsf)
            .build();
    try {

        HttpGet httpget = new HttpGet("https://...?wsdl");

        System.out.println("Executing request " + httpget.getRequestLine());

        CloseableHttpResponse response = httpclient.execute(httpget);
        try {
            HttpEntity entity = response.getEntity();

            System.out.println("----------------------------------------");
            System.out.println(response.getStatusLine());
            EntityUtils.consume(entity);
        } finally {
            response.close();
        }
    } finally {
        httpclient.close();
    }
}




    private KeyStore getKeystore(){
    String pin = "1111";
    char[] pin_arr = pin.toCharArray();
    String pkcs11config = "name = SmartCard\n"  + "library = c:/windows/system32/eTPKCS11.dll";
    byte[] pkcs11configBytes = pkcs11config.getBytes();
    ByteArrayInputStream configStream = new ByteArrayInputStream(pkcs11configBytes);
    Provider pkcs11Provider = new sun.security.pkcs11.SunPKCS11(configStream);
    Security.addProvider(pkcs11Provider);

    KeyStore smartCardKeyStore = null;
    try {
        smartCardKeyStore = KeyStore.getInstance("PKCS11");
    } catch (KeyStoreException e) {
        e.printStackTrace();
    }
    return smartCardKeyStore;
}

我没有其他想法。也许jaxb2不是最好的选择。我尝试使用Apache CXF,但问题仍然存在。我无法进行身份验证。

THX。

0 个答案:

没有答案