![](/img/trans.png)
[英]How to make HTTPS GET call with certificate in Rest-Assured java
[英]How to make REST Get request using certificate?
我可以使用curl查詢get請求:
curl -k --key some.key --cert some.crt --url "https://app.com:7078/subscribers/checkExists/1"
該查詢后,我得到200 OK
響應。 如何在Java中實現相同的目標? 使用Spring RestTemplate
嗎?
我試圖通過互聯網上的手冊禁用認證檢查:
CloseableHttpClient httpClient = HttpClients.custom()
.setSSLHostnameVerifier(new NoopHostnameVerifier())
.build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory();
requestFactory.setHttpClient(httpClient);
ResponseEntity<String> response = null;
String urlOverHttps = "https://app.com:7078/subscribers/checkExists/1";
response = new RestTemplate(requestFactory).exchange(urlOverHttps, HttpMethod.GET, null, String.class);
我也嘗試通過@Configuration
:
@Bean
public boolean disableSSLValidation() throws Exception {
final SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{new X509TrustManager() {
@Override
public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
}
@Override
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[0];
}
}}, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
public boolean verify(String hostname, SSLSession session) {
return true;
}
});
return true;
}
該代碼會向我發送400 Bad Request
錯誤。
UPD
我還將我的some.crt
文件添加到%JAVA_HOME%\\lib\\security
並使用命令將其導入keytool -import -alias ca -file some.crt -keystore cacerts -storepass changeit
那么,如何使用Spring RestTemplate
在GET
Request中提供我的some.crt
文件?
您需要將密鑰添加到Java密鑰庫(JKS)。 請參閱此處找到的有關使用keytool導入密鑰的答案:
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.