【问题标题】:Apache Wink connect to https resourcesApache Wink 连接到 https 资源
【发布时间】:2013-12-16 12:52:19
【问题描述】:

我从 Apache Wink Web 服务向位于云中的外部资源发送请求(我无法将证书放入 JVM),我知道当我尝试从浏览器发出请求时,我得到了一个正确的答案。

String serviceURL = "https://someurl&ciUser=user&ciPassword=password";

ClientConfig clientConfig = new ClientConfig();
clientConfig.setBypassHostnameVerification(true);

RestClient client = new RestClient(clientConfig);

Resource resource = client.resource(serviceURL); 

但我得到以下异常:

[err] org.apache.wink.client.ClientRuntimeException: java.lang.RuntimeException: javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
[err]   at org.apache.wink.client.internal.ResourceImpl.invoke(ResourceImpl.java:240)
[err]   at org.apache.wink.client.internal.ResourceImpl.invoke(ResourceImpl.java:189)
[err]   at org.apache.wink.client.internal.ResourceImpl.get(ResourceImpl.java:302)

更新

我也尝试过,但得到了同样的错误

String serviceURL = "https://url&ciUser=user&ciPassword=password";

//Perform basic http auth
ClientConfig clientConfig = new ClientConfig();
BasicAuthSecurityHandler basicAuthSecurityHandler = new BasicAuthSecurityHandler("user", "password");
clientConfig.handlers(basicAuthSecurityHandler);

RestClient client = new RestClient(clientConfig);

这个问题能解决吗?

【问题讨论】:

    标签: java web-services rest ssl apache-wink


    【解决方案1】:

    试试下面的步骤

    1. 使用 https 和我导出的 jks 文件运行应用程序。

    2. 使用浏览器查看证书。

    3. 导出并保存到 .cer 文件中

    4. 用 java keytool 导入。

    这是命令:

    keytool -import -trustcacerts -alias localhost -keystore "%JAVA_HOME%/jre/lib/security/cacerts" -file "D:/apache tomcat 6/bin/example.cer"
    

    【讨论】:

    • 感谢重播。但我在上面写了“我不能把证书放到 JVM”
    【解决方案2】:

    虽然这是一个老问题,但我想在这里粘贴我的代码,如果您想在使用 Wink 客户端时信任所有证书,这应该会有所帮助:

    public static void doRequest() {
        ClientConfig config = new ClientConfig();
        RestClient restClient = new RestClient(config);
        Resource resource = restClient.resource(serviceURL);
        trustAllCertificates();//trust all certificates before doing the request 
        ClientResponse clientResponse = resource.get(); 
    }
    public static void trustAllCertificates() throws RuntimeException {
        try {
            TrustManager[] trustManager = new TrustManager[] {
                new X509TrustManager() {
                    @Override
                    public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                        return null;
                    }
                    @Override
                    public void checkClientTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                    }
                    @Override
                    public void checkServerTrusted(
                        java.security.cert.X509Certificate[] certs, String authType) {
                    }
                }
            };
    
            SSLContext sc = SSLContext.getInstance("TLS");//or SSL, it depends on the certificate
            sc.init(null, trustManager, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    
        } catch (NoSuchAlgorithmException e) {
            throw new RuntimeException(e.getMessage());
        } catch (KeyManagementException e) {
            throw new RuntimeException(e.getMessage());
        } 
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-08
      • 2016-07-05
      • 1970-01-01
      相关资源
      最近更新 更多