簡體   English   中英

使用 Java 測試網站支持相互 SSL

[英]Testing a website supports mutual SSL using Java

我需要編寫一個 Cucumber 測試用例來驗證網站是否支持 Java 中的 2-way SSL。 在無數次閱讀不同的文章和答案后,我不確定如何准確地做到這一點。 我已經為測試用例的客戶端生成了一個自簽名證書,並將其添加到請求中,但我不確定如何驗證我正在訪問的網站是否支持 2-way SSL。 到目前為止,這是我從在線使用不同的答案和文章中獲得的代碼。

        org.apache.log4j.BasicConfigurator.configure();

        try {
            String CERT_PASSWORD = "somePass";

            KeyStore identityKeyStore = KeyStore.getInstance("jks");
            FileInputStream identityKeyStoreFile = new FileInputStream(new File(System.getProperty("user.dir") + "/src/main/resources/files-for-testcases/ultimate.jks"));
            identityKeyStore.load(identityKeyStoreFile, CERT_PASSWORD.toCharArray());


            SSLContext sslContext = SSLContexts.custom()
                    // load identity keystore
                    .loadKeyMaterial(identityKeyStore, CERT_PASSWORD.toCharArray(), (aliases, socket) -> "bddsecurity")
                    .build();

            SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(sslContext,
                    new String[]{"TLSv1.2", "TLSv1.1"},
                    null,
                    SSLConnectionSocketFactory.getDefaultHostnameVerifier());

            CloseableHttpClient client = HttpClients.custom()
                    .setSSLSocketFactory(sslConnectionSocketFactory)
                    .build();

            // Call a SSL-endpoint
            return callEndPoint (client, url);
        } catch (Exception ex) {
            System.out.println("Boom, we failed: " + ex);
            ex.printStackTrace();
        }
        return 404;
    }

    private static int callEndPoint (CloseableHttpClient aHTTPClient, String aEndPointURL) {

        try {
            HttpGet httpGet = new HttpGet(aEndPointURL);

            LOG.info("**GET** request Url: " + httpGet.getURI());

            HttpResponse response = aHTTPClient.execute(httpGet);

            int responseCode = response.getStatusLine().getStatusCode();
            LOG.info("Response Code: " + responseCode);
            return responseCode;
        } catch (Exception ex) {
            System.out.println("Boom, we failed: " + ex);
            ex.printStackTrace();
        }
        return 404;
    }

所以只是為了更新,問題出在我的服務器上,沒有證書的連接沒有問題。 不是,然后我的應用程序拋出了一個錯誤,但診斷很簡單,現在一切都很好。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM