HttpsURLConnection.setDefaultHostnameVerifier:方法验证未被调用

时间:2015-02-17 12:31:04

标签: java ssl jboss

我像这样配置HttpsUrlConnection:

HttpsURLConnection.setDefaultSSLSocketFactory(sslFactory);
HttpsURLConnection.setDefaultHostnameVerifier(new DummyHostnameVerifier());

DummyHostnameVerifier:

public class DummyHostnameVerifier implements HostnameVerifier {
    @Override
    public boolean verify(String s, SSLSession sslSession) {
        return true;
    }
}

当然,它只是配置的一部分。但问题是不调用DummyHostnameVerifier中的verify方法。 当我在本地机器上测试我的应用程序,glassfish 3服务器,验证调用,我没有收到任何异常。 但是当我在远程环境中测试它时,不会调用verify,我收到了这个:

java.io.IOException: The https URL hostname does not match the Common Name (CN) on the server certificate.  To disable this check (NOT recommended for production) set the CXF client TLS configuration property "disableCNCheck" to true.

在远程环境应用程序上运行jboss 5。 也许这取决于一些jboss配置?我无法理解,设置验证程序后默认主机名验证程序已更改。

2 个答案:

答案 0 :(得分:0)

我认为如果你想传递certificateValidation,你需要创建一个不会用于证书验证的Trustmanager

HttpsURLConnection.setDefaultHostnameVerifier(new DummyHostnameVerifier());
        //  Create a TrustManager which wont validate certificate chains start 

        javax.net.ssl.TrustManager[] trustAllCertificates = new javax.net.ssl.TrustManager[1];

        javax.net.ssl.TrustManager tm = new miTM();

        trustAllCertificates[0] = tm;

        javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL");

        sc.init(null, trustAllCertificates, null);
    //  Create a TrustManager which wont validate certificate chains end 
HttpsURLConnection.setDefaultSSLSocketFactory(sslFactory);

如果你得到了解决方案,可以试试上面的代码并告诉我吗?

答案 1 :(得分:0)

问题在于:不知何故,在给服务器的消息中没有动作名称。 我像这样配置连接:

HttpsURLConnection.setDefaultSSLSocketFactory(sslFactory);         HttpsURLConnection.setDefaultHostnameVerifier(new DummyHostnameVerifier());

    URL url = null;
    try {
        url = new URL(endpoint + "/wsdl");
    } catch (MalformedURLException e) {
        LOG.error(e.getMessage());
    }

    javax.xml.ws.Service s = MyService.create(url, new QName(MyService.NAMESPACE, MyService.SERVICE));
    ServiceSoap port = s.getPort(ServiceSoap.class);

    Map<String, Object> reqCtx = ((BindingProvider)port).getRequestContext();
    reqCtx.put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, endpoint);
    reqCtx.put(BindingProvider.SOAPACTION_USE_PROPERTY, Boolean.TRUE);
    reqCtx.put(BindingProvider.SOAPACTION_URI_PROPERTY, actionName);

    Client client = ClientProxy.getClient(port);
    HTTPConduit http = (HTTPConduit) client.getConduit();
    HTTPClientPolicy httpClientPolicy = new HTTPClientPolicy();
    httpClientPolicy.setConnection(ConnectionType.CLOSE);
    http.setClient(httpClientPolicy);
    TLSClientParameters tls = new TLSClientParameters();
    tls.setSSLSocketFactory(sslFactory);
    tls.setDisableCNCheck(true);
    http.setTlsClientParameters(tls);

所以,配置了端口,一切都开始工作了。