JavaFX: Consume secure RESTful services

2019-09-04 10:23发布

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.

0条回答
登录 后发表回答