Java webservices Tomcat Jax-WS ssl,如何设置客户端来验证证书

时间:2015-06-04 13:10:10

标签: java web-services tomcat ssl ssl-certificate

地狱大家, 我已经通过下面的链接在tomcat + ssl Connection上设置了java webservices http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat-ssl-connection/。它工作正常。

我现在的问题是在这段代码中客户端部分没有authencticate证书或ssl连接,我只有部分检查主机名,由主机名验证器,但现在我有一个自签名证书,不知道应该怎么做我做。 如何扩展这个类。我从论坛找到的代码很少,但我没有得到一个完整的想法,密钥库或信任库来自哪里。参考指导我的任何博客或链接都非常感谢。

我的客户端代码位于

之下
public IExample create() throws MalformedURLException{
        try{
            TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
                public X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                    // Trust always
                }

                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                    // Trust always
                }

            } };
            // Install the all-trusting trust manager
             sc = SSLContext.getInstance("SSL");
            // Create empty HostnameVerifier
            HostnameVerifier hv = new HostnameVerifier() {
                public boolean verify(String hostname, SSLSession arg1) {               

                        if(hostname.equals(arg1.getPeerHost()) && hostname.equals("example.com"))
                        {
                            return true;
                        }else{ 
                            return false;
                        }
                    }
                };

            sc.init(null,trustAllCerts , new java.security.SecureRandom());

            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
            HttpsURLConnection.setDefaultHostnameVerifier(hv);
         }
         catch(Exception e){
            e.printStackTrace();
         }

        try{
            URL url = new URL( urlString );

            //1st argument service URI, refer to wsdl document above
            //2nd argument is service name, refer to wsdl document above
                QName qname = new QName("http://synchronization.ws/", "ExampleImplclass");

                Service service = Service.create(url, qname);
                IExample iExample = service.getPort(IExample.class);       
                return iExample;        
        }catch(Exception e)
        {
            e.printStackTrace();
            return null;
        }
    }

0 个答案:

没有答案