use of org.apache.http.conn.ssl.TrustSelfSignedStrategy in project incubator-gobblin by apache.
the class AzkabanClient method createHttpClient.
/**
* Create a {@link CloseableHttpClient} used to communicate with Azkaban server.
* Derived class can configure different http client by overriding this method.
*
* @return A closeable http client.
*/
private CloseableHttpClient createHttpClient() throws AzkabanClientException {
try {
// SSLSocketFactory using custom TrustStrategy that ignores warnings about untrusted certificates
// Self sign SSL
SSLContextBuilder sslcb = new SSLContextBuilder();
sslcb.loadTrustMaterial(null, (TrustStrategy) new TrustSelfSignedStrategy());
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslcb.build());
HttpClientBuilder builder = HttpClientBuilder.create();
RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT).setSocketTimeout((int) this.requestTimeout.toMillis()).setConnectTimeout((int) this.requestTimeout.toMillis()).setConnectionRequestTimeout((int) this.requestTimeout.toMillis()).build();
builder.disableCookieManagement().useSystemProperties().setDefaultRequestConfig(requestConfig).setConnectionManager(new BasicHttpClientConnectionManager()).setSSLSocketFactory(sslsf);
return builder.build();
} catch (Exception e) {
throw new AzkabanClientException("HttpClient cannot be created", e);
}
}
use of org.apache.http.conn.ssl.TrustSelfSignedStrategy in project sagacity-sqltoy by chenrenfei.
the class ElasticEndpoint method initRestClient.
/**
* @param restClient the restClient to set
*/
public void initRestClient() {
if (StringUtil.isBlank(this.getUrl())) {
return;
}
if (restClient == null) {
// 替换全角字符
String[] urls = this.getUrl().replaceAll("\\;", ";").replaceAll("\\,", ",").replaceAll("\\;", ",").split("\\,");
// 当为单一地址时使用httpclient直接调用
if (urls.length < 2) {
return;
}
List<HttpHost> hosts = new ArrayList<HttpHost>();
for (String urlStr : urls) {
try {
if (StringUtil.isNotBlank(urlStr)) {
URL url = new java.net.URL(urlStr.trim());
hosts.add(new HttpHost(url.getHost(), url.getPort(), url.getProtocol()));
}
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
if (!hosts.isEmpty()) {
HttpHost[] hostAry = new HttpHost[hosts.size()];
hosts.toArray(hostAry);
RestClientBuilder builder = RestClient.builder(hostAry);
final ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(Charset.forName(this.charset == null ? "UTF-8" : this.charset)).build();
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(this.requestTimeout).setConnectTimeout(this.connectTimeout).setSocketTimeout(this.socketTimeout).build();
final CredentialsProvider credsProvider = new BasicCredentialsProvider();
final boolean hasCrede = (StringUtil.isNotBlank(this.getUsername()) && StringUtil.isNotBlank(getPassword())) ? true : false;
// 是否ssl证书模式
final boolean hasSsl = StringUtil.isNotBlank(this.keyStore);
// 凭据提供器
if (hasCrede) {
credsProvider.setCredentials(AuthScope.ANY, // 认证用户名和密码
new UsernamePasswordCredentials(getUsername(), getPassword()));
}
SSLContextBuilder sslBuilder = null;
try {
if (hasSsl) {
KeyStore truststore = KeyStore.getInstance(StringUtil.isBlank(keyStoreType) ? KeyStore.getDefaultType() : keyStoreType);
truststore.load(FileUtil.getFileInputStream(keyStore), (keyStorePass == null) ? null : keyStorePass.toCharArray());
sslBuilder = SSLContexts.custom().loadTrustMaterial(truststore, keyStoreSelfSign ? new TrustSelfSignedStrategy() : null);
}
final SSLContext sslContext = (sslBuilder == null) ? null : sslBuilder.build();
final boolean disableAuthCaching = !authCaching;
builder.setHttpClientConfigCallback(new RestClientBuilder.HttpClientConfigCallback() {
@Override
public HttpAsyncClientBuilder customizeHttpClient(HttpAsyncClientBuilder httpClientBuilder) {
httpClientBuilder.setDefaultConnectionConfig(connectionConfig).setDefaultRequestConfig(requestConfig);
// 禁用抢占式身份验证
if (disableAuthCaching) {
httpClientBuilder.disableAuthCaching();
}
// 用户名密码
if (hasCrede) {
httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
}
// 证书
if (hasSsl) {
httpClientBuilder.setSSLContext(sslContext);
}
return httpClientBuilder;
}
});
restClient = builder.build();
} catch (Exception e) {
e.printStackTrace();
}
}
}
}
Aggregations