![](/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.