Created
May 15, 2018 15:20
Generate RestTemplate with authentication and ignoring the certificates validation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.corunet; | |
import java.security.KeyManagementException; | |
import java.security.KeyStoreException; | |
import java.security.NoSuchAlgorithmException; | |
import javax.net.ssl.SSLContext; | |
import org.apache.http.client.HttpClient; | |
import org.apache.http.config.Registry; | |
import org.apache.http.config.RegistryBuilder; | |
import org.apache.http.conn.socket.ConnectionSocketFactory; | |
import org.apache.http.conn.socket.PlainConnectionSocketFactory; | |
import org.apache.http.conn.ssl.NoopHostnameVerifier; | |
import org.apache.http.conn.ssl.SSLConnectionSocketFactory; | |
import org.apache.http.conn.ssl.TrustAllStrategy; | |
import org.apache.http.impl.client.HttpClients; | |
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; | |
import org.apache.http.ssl.SSLContextBuilder; | |
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; | |
import org.springframework.http.client.support.BasicAuthorizationInterceptor; | |
import org.springframework.web.client.RestTemplate; | |
public class RestTemplateCallIgnoreSSL { | |
public static void main(String[] args) throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException { | |
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustAllStrategy()).build(); | |
// don't check Hostnames, either. | |
// -- use SSLConnectionSocketFactory.getDefaultHostnameVerifier(), | |
// if you don't want to weaken here's the special part: | |
// -- need to create an SSL Socket Factory, to use our weakened "trust strategy"; | |
// -- and create a Registry, to register it. | |
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); | |
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create() | |
.register("http", PlainConnectionSocketFactory.getSocketFactory()) | |
.register("https", sslSocketFactory).build(); | |
// now, we create connection-manager using our Registry. | |
// -- allows multi-threaded use | |
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry); | |
// finally, build the HttpClient; | |
HttpClient client = HttpClients.custom() | |
.setSSLContext(sslContext) | |
.setConnectionManager(connectionManager) | |
.build(); | |
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(); | |
requestFactory.setHttpClient(client); | |
RestTemplate restTemplate = new RestTemplate(requestFactory); | |
String username = ""; | |
String password = ""; | |
restTemplate.getInterceptors().add( | |
new BasicAuthorizationInterceptor( | |
username, | |
password | |
) | |
); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
working for my local env.. thanks. really helpful. there was not much example of it.