简体   繁体   中英

Apache Wink connect to https resources

I send a request to an external resource, which is located in a cloud (I can't put certificates to JVM) from Apache Wink web service and I know that when I try to make a request from the browser, I get a correct answer.

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); 

But I get the following exception:

[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)

UPDATE

I also try this but get the same errors

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);

Is it possible to solve this problem?

Try below steps

  1. run the application with https with jks file i export.

  2. view certificates with browser.

  3. Export it and save into .cer file

  4. import it with java keytool.

this is the command :

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

Although it's an old question, I'd like to paste my code here, it should be helpful if you want to trust all the certificates when using Wink Client:

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());
    } 
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM