Search in sources :

Example 61 with TrustSelfSignedStrategy

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);
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) RetryException(com.github.rholder.retry.RetryException) TimeoutException(java.util.concurrent.TimeoutException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException)

Example 62 with TrustSelfSignedStrategy

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();
            }
        }
    }
}
Also used : RequestConfig(org.apache.http.client.config.RequestConfig) MalformedURLException(java.net.MalformedURLException) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) ArrayList(java.util.ArrayList) RestClientBuilder(org.elasticsearch.client.RestClientBuilder) BasicCredentialsProvider(org.apache.http.impl.client.BasicCredentialsProvider) CredentialsProvider(org.apache.http.client.CredentialsProvider) SSLContext(javax.net.ssl.SSLContext) KeyStore(java.security.KeyStore) URL(java.net.URL) MalformedURLException(java.net.MalformedURLException) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) HttpAsyncClientBuilder(org.apache.http.impl.nio.client.HttpAsyncClientBuilder) HttpHost(org.apache.http.HttpHost) SSLContextBuilder(org.apache.http.ssl.SSLContextBuilder) ConnectionConfig(org.apache.http.config.ConnectionConfig) TrustSelfSignedStrategy(org.apache.http.conn.ssl.TrustSelfSignedStrategy)

Aggregations

TrustSelfSignedStrategy (org.apache.http.conn.ssl.TrustSelfSignedStrategy)62 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)47 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)32 SSLContext (javax.net.ssl.SSLContext)23 IOException (java.io.IOException)18 HttpClient (org.apache.http.client.HttpClient)15 KeyStore (java.security.KeyStore)14 HttpComponentsClientHttpRequestFactory (org.springframework.http.client.HttpComponentsClientHttpRequestFactory)14 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)12 KeyManagementException (java.security.KeyManagementException)11 KeyStoreException (java.security.KeyStoreException)11 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)11 Test (org.junit.jupiter.api.Test)11 File (java.io.File)10 NoopHostnameVerifier (org.apache.http.conn.ssl.NoopHostnameVerifier)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 SSLContextBuilder (org.apache.http.conn.ssl.SSLContextBuilder)9 RequestConfig (org.apache.http.client.config.RequestConfig)8 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)7 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)7