2

I can query get request using curl:

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

After that query I get 200 OK Response. How I can achieve the same in Java? Using 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:

@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.

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

So, how can I provide my some.crt file in GET Request using Spring RestTemplate?

BSeitkazin
  • 2,889
  • 25
  • 40

1 Answers1

0

You need to add your keys to the Java Key Store(JKS). See the answer found here about importing keys using the keytool:

How to import an existing x509 certificate and private key in Java keystore to use in SSL?

mad_fox
  • 3,030
  • 5
  • 31
  • 43