JavaFX: Consume secure RESTful services

纵然是瞬间 提交于 2019-12-11 21:14:06

问题


I have a JavaFX client that needs to consume webservices hosted over SSL, like: https://myserver.com/testservice/getdata.

When I try to access this service from the JavaFX client, I get the exception: javax.net.ssl.SSLHandshakeException: java.security.cert.CertificateException: No name matching localhost found

I searched for this exception and found that the following code needs to be inserted to get rid of this exception:

static {
    //for localhost testing only
    javax.net.ssl.HttpsURLConnection.setDefaultHostnameVerifier(
    new javax.net.ssl.HostnameVerifier(){

        public boolean verify(String hostname,
                javax.net.ssl.SSLSession sslSession) {
            if (hostname.equals("localhost")) {
                return true;
            }
            return false;
        }
    });
}

But, this code is just to ignore the local setup of the services. Can you please guide on what am I missing so as to consume the RESTful services hosted over SSL?

From the JavaFX client, I am trying to access the services using the following code:

public List<String> getData() throws IOException
{

    String servicesUrl = "https://myserver.com/testservice/getdata";
    try
    {
        URL url = new URLservicesUrl 
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();

        if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK)
        {
            String data = restTemplate.getForObject(servicesUrl, String.class);
            return data;
        }

    }
    catch (IOException ioex)
    {
        ioex.printStackTrace();
    }
}

I did try using the HttpsURLConnection API, but without any success.

I am thinking that the JavaFX client needs to be able to validate the certificates or do some kind of handshake, which is automatically taken care of by the browsers but not in this case where a thick client sends requests to a url over https.

来源:https://stackoverflow.com/questions/19362136/javafx-consume-secure-restful-services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!