下面列出了怎么用org.apache.http.conn.ssl.AllowAllHostnameVerifier的API类实例代码及写法,或者点击链接到github查看源代码。
public HttpClient getHttpsClientTrustAll() {
try {
SSLSocketFactory sf = new SSLSocketFactory(new TrustStrategy(){
@Override
public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
return true;
}
}, new AllowAllHostnameVerifier());
PlainSocketFactory psf = PlainSocketFactory.getSocketFactory();
SchemeRegistry registry = new SchemeRegistry();
registry.register(new Scheme("http", 80, psf));
registry.register(new Scheme("https", 443, sf));
ClientConnectionManager ccm = new PoolingClientConnectionManager(registry);
return new DefaultHttpClient(ccm);
} catch (Exception ex) {
log.error("Failed to create TrustAll https client", ex);
return new DefaultHttpClient();
}
}
public AbstractRestTemplateClient ignoreAuthenticateServer() {
//backward compatible with android httpclient 4.3.x
if(restTemplate.getRequestFactory() instanceof HttpComponentsClientHttpRequestFactory) {
try {
SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
X509HostnameVerifier verifier = ignoreSslWarning ? new AllowAllHostnameVerifier() : new BrowserCompatHostnameVerifier();
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(sslContext, verifier);
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
((HttpComponentsClientHttpRequestFactory)restTemplate.getRequestFactory()).setHttpClient(httpClient);
} catch (Exception e) {
e.printStackTrace();
}
} else {
Debug.error("the request factory " + restTemplate.getRequestFactory().getClass().getName() + " does not support ignoreAuthenticateServer");
}
return this;
}
private static RestTemplate createRestTemplate(String host, String username, String password, Set<ClientHttpRequestInterceptor> interceptors) throws GeneralSecurityException {
CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
credentialsProvider.setCredentials(new AuthScope(host, 25555),
new UsernamePasswordCredentials(username, password));
SSLContext sslContext = SSLContexts.custom()
.loadTrustMaterial(null, new TrustSelfSignedStrategy())
.useTLS()
.build();
SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext, new AllowAllHostnameVerifier());
HttpClient httpClient = HttpClientBuilder.create()
.disableRedirectHandling()
.setDefaultCredentialsProvider(credentialsProvider)
.setSSLSocketFactory(connectionFactory)
.build();
RestTemplate restTemplate = new RestTemplate(new HttpComponentsClientHttpRequestFactory(httpClient));
restTemplate.getInterceptors().addAll(interceptors);
return restTemplate;
}
public static CloseableHttpClient getNotValidatingClient(int timeout, String proxyServer, Integer proxyPort,
String proxyUser, String proxyPassword) {
try {
HttpClientBuilder clientBuilder = HttpClients.custom();
RequestConfig.Builder requestBuilder = RequestConfig.custom().setConnectTimeout(timeout * 1000)
.setSocketTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000)
.setRedirectsEnabled(true);
if (StringUtils.isNotEmpty(proxyServer)) {
HttpHost proxyHost = new HttpHost(proxyServer, proxyPort);
requestBuilder.setProxy(proxyHost);
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxyHost);
clientBuilder.setRoutePlanner(routePlanner);
if (StringUtils.isNotEmpty(proxyUser)) {
CredentialsProvider credentialProvider = new BasicCredentialsProvider();
credentialProvider.setCredentials(AuthScope.ANY,
new UsernamePasswordCredentials(proxyUser, proxyPassword));
clientBuilder.setRoutePlanner(routePlanner);
}
}
RequestConfig requestConfig = requestBuilder.build();
CloseableHttpClient httpclient = clientBuilder.setHostnameVerifier(new AllowAllHostnameVerifier())
.setSslcontext(new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build()).setDefaultRequestConfig(requestConfig).build();
return httpclient;
} catch (Throwable t) {
return null;
}
}
private CloseableHttpClient getDefaultHttpClient() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).useTLS().build();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
new AllowAllHostnameVerifier());
return HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();
}
private CloseableHttpClient getDefaultHttpClient() throws KeyManagementException, NoSuchAlgorithmException, KeyStoreException {
SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).useTLS().build();
SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
new AllowAllHostnameVerifier());
return HttpClients.custom().setSSLSocketFactory(sslSocketFactory).build();
}
public static HttpClient getClient() {
try {
SSLContext context = new SSLContextBuilder()
.loadTrustMaterial(null, (arg0, arg1) -> true)
.build();
return HttpClients.custom()
.setHostnameVerifier(new AllowAllHostnameVerifier())
.setSslcontext(context)
.build();
} catch (Exception e) {
e.printStackTrace();
return HttpClients.createDefault();
}
}
private CloseableHttpClient getApacheSslBypassClient() throws NoSuchAlgorithmException,
KeyManagementException, KeyStoreException {
return HttpClients.custom().
setHostnameVerifier(new AllowAllHostnameVerifier()).
setSslcontext(new SSLContextBuilder()
.loadTrustMaterial(null, (arg0, arg1) -> true)
.build()).build();
}