[英]Testing a website supports mutual SSL using Java
I need to write a Cucumber Test case to verify if a website supports 2-way SSL in Java.我需要编写一个 Cucumber 测试用例来验证网站是否支持 Java 中的 2-way SSL。 I'm not sure how to exactly do it after countlessly reading through different articles and answers.
在无数次阅读不同的文章和答案后,我不确定如何准确地做到这一点。 I've generated a self signed certificate for the client side of the test case, and I add it in the request, but I'm not sure how exactly to verify that the website I'm accessing supports 2-way SSL.
我已经为测试用例的客户端生成了一个自签名证书,并将其添加到请求中,但我不确定如何验证我正在访问的网站是否支持 2-way SSL。 This is my code so far that I've gotten from using different bits of answers and articles online.
到目前为止,这是我从在线使用不同的答案和文章中获得的代码。
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;
}
So just to update, the issue was with my server that was alright with connections that didn't have a certificate.所以只是为了更新,问题出在我的服务器上,没有证书的连接没有问题。 It wasn't, and then my application threw an error but the diagnosis was straightforward and it now is all good.
不是,然后我的应用程序抛出了一个错误,但诊断很简单,现在一切都很好。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.