Search in sources :

Example 81 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project OpenAM by OpenRock.

the class ClusterStateService method checkServerUp.

/**
     * Internal method for checking health status using sock.connect()
     * <p/>
     * TODO -- Use a better mechanism for alive status. 10.1+.
     *
     * @param info server info instance
     * @return true if server is up, false otherwise
     */
private boolean checkServerUp(StateInfo info) {
    if (info == null) {
        return false;
    }
    if (localServerId.equals(info.id)) {
        return true;
    }
    boolean result = false;
    Socket sock = null;
    InputStream is = null;
    try {
        /*
             * If we need to check for a front end proxy, we need
             * to send a request.  
             */
        if (!doRequest) {
            sock = new Socket();
            sock.connect(info.address, timeout);
            result = true;
        } else {
            HttpURLConnection connection = null;
            int responseCode = 0;
            try {
                connection = (HttpURLConnection) info.url.openConnection();
                connection.setConnectTimeout(timeout);
                connection.setReadTimeout(timeout);
                if (connection instanceof HttpsURLConnection) {
                    ((HttpsURLConnection) connection).setHostnameVerifier(new HostnameVerifier() {

                        @Override
                        public boolean verify(String hostname, SSLSession session) {
                            return true;
                        }
                    });
                }
                is = connection.getInputStream();
                responseCode = connection.getResponseCode();
                readStream(is);
            } catch (IOException ioe) {
                if (connection != null) {
                    readStream(connection.getErrorStream());
                }
            }
            result = responseCode == HttpURLConnection.HTTP_OK;
        }
    } catch (Exception ex) {
        result = false;
    } finally {
        if (sock != null) {
            try {
                sock.close();
            } catch (IOException ioe) {
            //ignored
            }
        }
        IOUtils.closeIfNotNull(is);
    }
    return result;
}
Also used : HttpURLConnection(java.net.HttpURLConnection) InputStream(java.io.InputStream) SSLSession(javax.net.ssl.SSLSession) IOException(java.io.IOException) Socket(java.net.Socket) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 82 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project wildfly by wildfly.

the class WebSecurityCERTTestCase method getHttpsClient.

private static CloseableHttpClient getHttpsClient(String alias) {
    try {
        SSLContext ctx = SSLContext.getInstance("TLS");
        JBossJSSESecurityDomain jsseSecurityDomain = new JBossJSSESecurityDomain("client-cert");
        jsseSecurityDomain.setKeyStorePassword("changeit");
        ClassLoader tccl = Thread.currentThread().getContextClassLoader();
        URL keystore = tccl.getResource("security/client.keystore");
        jsseSecurityDomain.setKeyStoreURL(keystore.getPath());
        jsseSecurityDomain.setClientAlias(alias);
        jsseSecurityDomain.reloadKeyAndTrustStore();
        KeyManager[] keyManagers = jsseSecurityDomain.getKeyManagers();
        TrustManager[] trustManagers = jsseSecurityDomain.getTrustManagers();
        ctx.init(keyManagers, trustManagers, null);
        HostnameVerifier verifier = (string, ssls) -> true;
        //SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        SSLConnectionSocketFactory ssf = new SSLConnectionSocketFactory(ctx, verifier);
        Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("https", ssf).build();
        HttpClientConnectionManager ccm = new BasicHttpClientConnectionManager(registry);
        return HttpClientBuilder.create().setSSLSocketFactory(ssf).setSSLHostnameVerifier(new NoopHostnameVerifier()).setConnectionManager(ccm).build();
    } catch (Exception ex) {
        ex.printStackTrace();
        return null;
    }
}
Also used : SSLContext(javax.net.ssl.SSLContext) RegistryBuilder(org.apache.http.config.RegistryBuilder) Arquillian(org.jboss.arquillian.junit.Arquillian) URL(java.net.URL) ServerSetup(org.jboss.as.arquillian.api.ServerSetup) RunWith(org.junit.runner.RunWith) TrustManager(javax.net.ssl.TrustManager) JBossJSSESecurityDomain(org.jboss.security.JBossJSSESecurityDomain) WebCERTTestsSecurityDomainSetup(org.jboss.as.test.integration.web.security.WebCERTTestsSecurityDomainSetup) StatusLine(org.apache.http.StatusLine) RunAsClient(org.jboss.arquillian.container.test.api.RunAsClient) Registry(org.apache.http.config.Registry) ArquillianResource(org.jboss.arquillian.test.api.ArquillianResource) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ShrinkWrap(org.jboss.shrinkwrap.api.ShrinkWrap) WebArchive(org.jboss.shrinkwrap.api.spec.WebArchive) CommonCriteria(org.jboss.as.test.categories.CommonCriteria) Test(org.junit.Test) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) Category(org.junit.experimental.categories.Category) KeyManager(javax.net.ssl.KeyManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) HttpGet(org.apache.http.client.methods.HttpGet) Deployment(org.jboss.arquillian.container.test.api.Deployment) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) HttpResponse(org.apache.http.HttpResponse) SecuredServlet(org.jboss.as.test.integration.web.security.SecuredServlet) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) ManagementClient(org.jboss.as.arquillian.container.ManagementClient) Assert.assertEquals(org.junit.Assert.assertEquals) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) SSLContext(javax.net.ssl.SSLContext) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) JBossJSSESecurityDomain(org.jboss.security.JBossJSSESecurityDomain) URL(java.net.URL) TrustManager(javax.net.ssl.TrustManager) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) KeyManager(javax.net.ssl.KeyManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Example 83 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project jedis by xetorthio.

the class SSLJedisTest method connectWithShardInfoAndCustomSocketFactory.

/**
   * Tests opening an SSL/TLS connection to redis with a custom socket factory.
   */
@Test
public void connectWithShardInfoAndCustomSocketFactory() throws Exception {
    final URI uri = URI.create("rediss://localhost:6390");
    final SSLSocketFactory sslSocketFactory = createTrustStoreSslSocketFactory();
    final SSLParameters sslParameters = new SSLParameters();
    HostnameVerifier hostnameVerifier = new BasicHostnameVerifier();
    JedisShardInfo shardInfo = new JedisShardInfo(uri, sslSocketFactory, sslParameters, hostnameVerifier);
    shardInfo.setPassword("foobared");
    Jedis jedis = new Jedis(shardInfo);
    jedis.get("foo");
    jedis.disconnect();
    jedis.close();
}
Also used : Jedis(redis.clients.jedis.Jedis) SSLParameters(javax.net.ssl.SSLParameters) JedisShardInfo(redis.clients.jedis.JedisShardInfo) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URI(java.net.URI) HostnameVerifier(javax.net.ssl.HostnameVerifier) Test(org.junit.Test)

Example 84 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project NoHttp by yanzhenjie.

the class OkHttpNetworkExecutor method execute.

@Override
public Network execute(IBasicRequest request) throws Exception {
    URL url = new URL(request.url());
    HttpURLConnection connection = URLConnectionFactory.getInstance().open(url, request.getProxy());
    connection.setConnectTimeout(request.getConnectTimeout());
    connection.setReadTimeout(request.getReadTimeout());
    connection.setInstanceFollowRedirects(false);
    if (connection instanceof HttpsURLConnection) {
        SSLSocketFactory sslSocketFactory = request.getSSLSocketFactory();
        if (sslSocketFactory != null)
            ((HttpsURLConnection) connection).setSSLSocketFactory(sslSocketFactory);
        HostnameVerifier hostnameVerifier = request.getHostnameVerifier();
        if (hostnameVerifier != null)
            ((HttpsURLConnection) connection).setHostnameVerifier(hostnameVerifier);
    }
    // Base attribute
    connection.setRequestMethod(request.getRequestMethod().toString());
    connection.setDoInput(true);
    boolean isAllowBody = request.getRequestMethod().allowRequestBody();
    connection.setDoOutput(isAllowBody);
    // Adds all request header to connection.
    Headers headers = request.headers();
    // To fix bug: accidental EOFException before API 19.
    List<String> values = headers.getValues(Headers.HEAD_KEY_CONNECTION);
    if (values == null || values.size() == 0)
        headers.add(Headers.HEAD_KEY_CONNECTION, Headers.HEAD_VALUE_CONNECTION_KEEP_ALIVE);
    if (isAllowBody)
        headers.set(Headers.HEAD_KEY_CONTENT_LENGTH, Long.toString(request.getContentLength()));
    Map<String, String> requestHeaders = headers.toRequestHeaders();
    for (Map.Entry<String, String> headerEntry : requestHeaders.entrySet()) {
        String headKey = headerEntry.getKey();
        String headValue = headerEntry.getValue();
        Logger.i(headKey + ": " + headValue);
        connection.setRequestProperty(headKey, headValue);
    }
    // 5. Connect
    connection.connect();
    return new OkHttpNetwork(connection);
}
Also used : URL(java.net.URL) HostnameVerifier(javax.net.ssl.HostnameVerifier) HttpURLConnection(java.net.HttpURLConnection) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Map(java.util.Map) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 85 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project intellij-community by JetBrains.

the class IpnbConnection method configureHttpsConnection.

private void configureHttpsConnection() {
    HttpsURLConnection.setDefaultSSLSocketFactory(CertificateManager.getInstance().getSslContext().getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String s, SSLSession session) {
            return myURI.getHost().equals(s);
        }
    });
}
Also used : SSLSession(javax.net.ssl.SSLSession) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Aggregations

HostnameVerifier (javax.net.ssl.HostnameVerifier)94 SSLSession (javax.net.ssl.SSLSession)41 SSLContext (javax.net.ssl.SSLContext)30 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)27 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)24 TrustManager (javax.net.ssl.TrustManager)19 IOException (java.io.IOException)18 URL (java.net.URL)18 X509Certificate (java.security.cert.X509Certificate)17 X509TrustManager (javax.net.ssl.X509TrustManager)17 Test (org.junit.Test)16 HttpURLConnection (java.net.HttpURLConnection)14 SecureRandom (java.security.SecureRandom)14 InputStream (java.io.InputStream)12 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)10 CertificateException (java.security.cert.CertificateException)10 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)10 KeyManagementException (java.security.KeyManagementException)9 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8