How to install a certificate for verification on spring RestTemplate

I am using Spring RestTemplate in my application to access external web services. However, this SSL-enabled web service with a self-signed certificate (domain, etc.) is also not valid. This only happens on the local network, so I do not need to worry about some security issues. I want to make Spring to accept this certificate. This is what I have done so far:

1.) I configured my JBOSS 7 to use this keystore

<connector name="https" protocol="HTTP/1.1" socket-binding="https" scheme="https" enable-lookups="false" secure="true"> <ssl name="ssl" key-alias="my-private-key" password="rmi+ssl" certificate-key-file="../standalone/configuration/server-keystore.jks" protocol="TLSv1" verify-client="false"/> </connector> 

2.) Here is the configuration of my RestTemplate Bean (I use autwireing in my classes)

 <bean id="stringHttpConverter" class="org.springframework.http.converter.StringHttpMessageConverter"></bean> <bean id="httpClientParams" class="org.apache.commons.httpclient.params.HttpClientParams"> <property name="authenticationPreemptive" value="true"/> <property name="connectionManagerClass" value="org.apache.commons.httpclient.MultiThreadedHttpConnectionManager"/> </bean> <bean id="httpClient" class="org.apache.commons.httpclient.HttpClient"> <constructor-arg ref="httpClientParams"/> </bean> <bean id="httpClientFactory" class="org.springframework.http.client.CommonsClientHttpRequestFactory"> <constructor-arg ref="httpClient"/> </bean> <bean id="restTemplate" class="org.springframework.web.client.RestTemplate"> <constructor-arg ref="httpClientFactory"/> <property name="messageConverters"> <list> <!-- <ref bean="marshallingConverter" /> --> <ref bean="stringHttpConverter" /> </list> </property> </bean> 

I imported the server certificate into the keystore, it is definitely located there. What else do I need to do? I already checked all such questions here, but none of them helped. Thanks.

+6
source share
2 answers

Server-keystore.jks, which you specified in the connector for jboss-web, is used only as a server certificate for incoming connections.

For outgoing connections, JBoss acts like any other java client, so you need to import the server certificate into the standard java repository. You can use the default% JAVA_HOME% \ lib \ security \ cacerts and import your server certificate using:

 keytool -import -trustcacerts -keystore cacerts -storepass changeit -noprompt -alias mycert -file mycert.cer 

If you do not want to edit the cacerts default settings, you can define an alternative trust store by setting the system properties as described in: Java client certificates via HTTPS / SSL .

The third way is to override https ProtocolSocketFactory so that it accepts all certificates, for example: http://drumcoder.co.uk/blog/2011/mar/30/httpclient-self-signed-certificates/

+2
source

I made an easy way:

 static HttpComponentsClientHttpRequestFactory requestFactory = null; /** * * @return */ public static ServerProperties getServerProperties() { return serverProperties; } /** * @return */ public static HttpComponentsClientHttpRequestFactory getRequestFactory() { if (requestFactory == null) { TrustStrategy acceptingTrustStrategy = new TrustStrategy() { @Override public boolean isTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException { return true; } }; SSLContext sslContext = null; try { sslContext = org.apache.http.ssl.SSLContexts.custom() .loadTrustMaterial(null, acceptingTrustStrategy) .build(); SSLConnectionSocketFactory csf = new SSLConnectionSocketFactory(sslContext); CloseableHttpClient httpClient = HttpClients.custom() .setSSLSocketFactory(csf) .build(); requestFactory = new HttpComponentsClientHttpRequestFactory(); requestFactory.setHttpClient(httpClient); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (KeyManagementException e) { e.printStackTrace(); } catch (KeyStoreException e) { e.printStackTrace(); } } return requestFactory; } 

Then, when I instantiate restTemplate:

  RestTemplate restTemplate = new RestTemplate(getRequestFactory()); 

Simple

0
source

Source: https://habr.com/ru/post/905116/


All Articles