【问题标题】:How to bypass SSL certificates issue in JBOSS REST ClientRequest如何绕过 JBOSS REST ClientRequest 中的 SSL 证书问题
【发布时间】:2016-06-13 10:03:18
【问题描述】:

我是 Jboss 的新手,在我的 jboss 代码中使用 rest easy 客户端进行连接。下面是代码-

---
import org.jboss.resteasy.client.ClientRequest;
import org.jboss.resteasy.client.ClientResponse;
---
public String login() throws Exception {
---
    String URL = "https://IP//service/perform.do?operationId=XXXXX";
    ClientRequest restClient = new ClientRequest(URL);
    restClient.accept(MediaType.APPLICATION_JSON);
    restClient.body(MediaType.APPLICATION_JSON, hmap);
    ClientResponse < String > resp = restClient.post(String.class);
    if (resp.getStatus() != 201) {
        throw new RuntimeException("Failed : HTTPS error code : " + resp.getStatus());
    }
    BufferedReader br = new BufferedReader(new InputStreamReader(
        new ByteArrayInputStream(resp.getEntity().getBytes())));
    String output;
    System.out.println("Output from Server .... \n");
    while ((output = br.readLine()) != null) {
        System.out.println(output);
    }
    return output;
}

与启用 SSL 的服务器连接时,出现证书错误“HTTP Status 500 - org.jboss.resteasy.spi.UnhandledException: javax.net.ssl.SSLException: hostname in certificate didn't match”。

我们目前无法更改证书,但是有什么方法可以信任任何证书吗?我用谷歌搜索了很多帖子,但没有任何帮助。

任何人都可以告诉我这个问题的解决方案是什么。

【问题讨论】:

  • 我得到了解决方案

标签: eclipse ssl-certificate jboss7.x resteasy


【解决方案1】:

我得到了以下解决方案:

public static void trustAllCertificates() throws NoSuchAlgorithmException, KeyManagementException
    {
        // Create a trust manager that does not validate certificate chains
        TrustManager[] trustAllCerts = new TrustManager[] {new X509TrustManager() {
                public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                    return null;
                }
                public void checkClientTrusted(X509Certificate[] certs, String authType) {
                }
                public void checkServerTrusted(X509Certificate[] certs, String authType) {
                }
            }
        };

        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        };

        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    }

【讨论】:

    猜你喜欢
    • 2021-02-08
    • 2010-11-09
    • 2017-08-06
    • 1970-01-01
    • 2017-08-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-07-07
    相关资源
    最近更新 更多