简体   繁体   English

如何使用证书发出REST Get请求?

[英]How to make REST Get request using certificate?

I can query get request using curl: 我可以使用curl查询get请求:

curl -k --key some.key --cert some.crt --url "https://app.com:7078/subscribers/checkExists/1"

After that query I get 200 OK Response. 该查询后,我得到200 OK响应。 How I can achieve the same in Java? 如何在Java中实现相同的目标? Using Spring RestTemplate ? 使用Spring RestTemplate吗?

I tried to disable certification checking by manuals in internet: 我试图通过互联网上的手册禁用认证检查:

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);

Also I tried it via @Configuration : 我也尝试通过@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;
}

And that code response me 400 Bad Request error. 该代码会向我发送400 Bad Request错误。

UPD UPD

I also added my some.crt file to %JAVA_HOME%\\lib\\security and imported it with command - keytool -import -alias ca -file some.crt -keystore cacerts -storepass changeit 我还将我的some.crt文件添加到%JAVA_HOME%\\lib\\security并使用命令将其导入keytool -import -alias ca -file some.crt -keystore cacerts -storepass changeit

So, how can I provide my some.crt file in GET Request using Spring RestTemplate ? 那么,如何使用Spring RestTemplateGET Request中提供我的some.crt文件?

You need to add your keys to the Java Key Store(JKS). 您需要将密钥添加到Java密钥库(JKS)。 See the answer found here about importing keys using the keytool: 请参阅此处找到的有关使用keytool导入密钥的答案:

How to import an existing x509 certificate and private key in Java keystore to use in SSL? 如何导入Java密钥库中的现有x509证书和私钥以在SSL中使用?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Rest-Assured java 中使用证书进行 HTTPS GET 调用 - How to make HTTPS GET call with certificate in Rest-Assured java 如何在Spring Boot下运行的REST控制器中获取与HTTPS请求相关的SSL证书(X.509)? - How to get SSL certificate (X.509) relevant to HTTPS request in REST controller running under Spring boot? 使用.cer证书发出HTTPS请求 - Using .cer certificate to make an HTTPS request 如何使用REST请求进行gnip查询(Java) - How do I make a gnip query using a REST request (Java) 如何使用 Quarkus 以两种方式 SSL 从 REST 服务中的请求中检索客户端证书 - How to retrieve the client certificate from a request in a REST service using Quarkus in two way SSL 如何使用 Rest 确保使用.crt 证书和 public.key 令牌发送 HTTPS 请求 - How to send HTTPS request with Rest Assured using .crt certificate and public .key token Rest 保证,Java - 在发布请求中使用 TLS/SSL 证书 - Rest Assured, Java - using TLS/SSL certificate in post request 如何使用Jersey(REST)读取GET请求的输入XML? - How to read input XML for GET request using Jersey (REST)? 如何使用Jersey获取完整的REST请求体? - How to get full REST request body using Jersey? 如何从Java中的剩余请求中检索客户端证书 - How to retrieve client certificate from rest request in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM