Search in sources :

Example 46 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project Gargoyle by callakrsos.

the class HostNameVertifierInitializer method initialize.

@Override
public void initialize() throws Exception {
    LOGGER.debug(getClass().getName() + "  initialize.");
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        @Override
        public boolean verify(String arg0, SSLSession arg1) {
            LOGGER.debug(arg0);
            return true;
        }
    });
}
Also used : SSLSession(javax.net.ssl.SSLSession) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 47 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project ddf by codice.

the class CometDClient method doTrustAllCertificates.

private void doTrustAllCertificates() throws NoSuchAlgorithmException, KeyManagementException {
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

        @Override
        public void checkClientTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
        }

        @Override
        public void checkServerTrusted(X509Certificate[] x509Certificates, String s) throws CertificateException {
        }

        @Override
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }
    } };
    SSLContext sslContext = SSLContext.getInstance("SSL");
    sslContext.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    HostnameVerifier hostnameVerifier = (s, sslSession) -> s.equalsIgnoreCase(sslSession.getPeerHost());
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
}
Also used : X509Certificate(java.security.cert.X509Certificate) SSLContext(javax.net.ssl.SSLContext) StringUtils(org.apache.commons.lang.StringUtils) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) LongPollingTransport(org.cometd.client.transport.LongPollingTransport) LocalDateTime(java.time.LocalDateTime) LoggerFactory(org.slf4j.LoggerFactory) TrustManager(javax.net.ssl.TrustManager) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) SecureRandom(java.security.SecureRandom) HashSet(java.util.HashSet) HttpClient(org.eclipse.jetty.client.HttpClient) CollectionUtils(org.apache.commons.collections.CollectionUtils) ISO_DATE_TIME(java.time.format.DateTimeFormatter.ISO_DATE_TIME) Map(java.util.Map) LocalTime(java.time.LocalTime) ClientSessionChannel(org.cometd.bayeux.client.ClientSessionChannel) ConnectException(java.net.ConnectException) Channel(org.cometd.bayeux.Channel) URI(java.net.URI) HostnameVerifier(javax.net.ssl.HostnameVerifier) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Logger(org.slf4j.Logger) JsonPath(com.jayway.restassured.path.json.JsonPath) Set(java.util.Set) KeyManagementException(java.security.KeyManagementException) CertificateException(java.security.cert.CertificateException) Collectors(java.util.stream.Collectors) TimeUnit(java.util.concurrent.TimeUnit) List(java.util.List) X509TrustManager(javax.net.ssl.X509TrustManager) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) Optional(java.util.Optional) ClientTransport(org.cometd.client.transport.ClientTransport) BasicAuthentication(org.eclipse.jetty.client.util.BasicAuthentication) Comparator(java.util.Comparator) Collections(java.util.Collections) Message(org.cometd.bayeux.Message) BayeuxClient(org.cometd.client.BayeuxClient) X509TrustManager(javax.net.ssl.X509TrustManager) SecureRandom(java.security.SecureRandom) SSLContext(javax.net.ssl.SSLContext) X509Certificate(java.security.cert.X509Certificate) TrustManager(javax.net.ssl.TrustManager) X509TrustManager(javax.net.ssl.X509TrustManager) HostnameVerifier(javax.net.ssl.HostnameVerifier)

Example 48 with HostnameVerifier

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

the class HttpClientBuilderTest method createClientCanPassCustomVerifierToApacheBuilder.

@Test
public void createClientCanPassCustomVerifierToApacheBuilder() throws Exception {
    final HostnameVerifier customVerifier = (s, sslSession) -> false;
    assertThat(builder.using(customVerifier).createClient(apacheBuilder, connectionManager, "test")).isNotNull();
    final Field hostnameVerifierField = FieldUtils.getField(org.apache.http.impl.client.HttpClientBuilder.class, "hostnameVerifier", true);
    assertThat(hostnameVerifierField.get(apacheBuilder)).isSameAs(customVerifier);
}
Also used : ProtocolException(org.apache.http.ProtocolException) SocketAddress(java.net.SocketAddress) Assertions.assertThat(org.assertj.core.api.Assertions.assertThat) SocketConfig(org.apache.http.config.SocketConfig) TlsConfiguration(io.dropwizard.client.ssl.TlsConfiguration) RequestConfig(org.apache.http.client.config.RequestConfig) Header(org.apache.http.Header) ProxySelector(java.net.ProxySelector) NoConnectionReuseStrategy(org.apache.http.impl.NoConnectionReuseStrategy) Registry(org.apache.http.config.Registry) Proxy(java.net.Proxy) SystemDefaultDnsResolver(org.apache.http.impl.conn.SystemDefaultDnsResolver) After(org.junit.After) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) InstrumentedHttpRequestExecutor(com.codahale.metrics.httpclient.InstrumentedHttpRequestExecutor) HttpRoutePlanner(org.apache.http.conn.routing.HttpRoutePlanner) URI(java.net.URI) HostnameVerifier(javax.net.ssl.HostnameVerifier) HttpHeaders(org.apache.http.HttpHeaders) SSLConnectionSocketFactory(org.apache.http.conn.ssl.SSLConnectionSocketFactory) InstrumentedHttpClientConnectionManager(com.codahale.metrics.httpclient.InstrumentedHttpClientConnectionManager) LifecycleEnvironment(io.dropwizard.lifecycle.setup.LifecycleEnvironment) HeaderIterator(org.apache.http.HeaderIterator) HttpClientMetricNameStrategies(com.codahale.metrics.httpclient.HttpClientMetricNameStrategies) DefaultConnectionKeepAliveStrategy(org.apache.http.impl.client.DefaultConnectionKeepAliveStrategy) DefaultConnectionReuseStrategy(org.apache.http.impl.DefaultConnectionReuseStrategy) InetSocketAddress(java.net.InetSocketAddress) HttpRequest(org.apache.http.HttpRequest) List(java.util.List) HttpGet(org.apache.http.client.methods.HttpGet) BasicHttpContext(org.apache.http.protocol.BasicHttpContext) Managed(io.dropwizard.lifecycle.Managed) ConnectionSocketFactory(org.apache.http.conn.socket.ConnectionSocketFactory) Optional(java.util.Optional) CredentialsProvider(org.apache.http.client.CredentialsProvider) Mockito.mock(org.mockito.Mockito.mock) MockitoAnnotations.initMocks(org.mockito.MockitoAnnotations.initMocks) DefaultRoutePlanner(org.apache.http.impl.conn.DefaultRoutePlanner) RegistryBuilder(org.apache.http.config.RegistryBuilder) BasicListHeaderIterator(org.apache.http.message.BasicListHeaderIterator) DnsResolver(org.apache.http.conn.DnsResolver) CookieSpecs(org.apache.http.client.config.CookieSpecs) HttpUriRequest(org.apache.http.client.methods.HttpUriRequest) HttpRoute(org.apache.http.conn.routing.HttpRoute) Mockito.spy(org.mockito.Mockito.spy) Duration(io.dropwizard.util.Duration) ProxyConfiguration(io.dropwizard.client.proxy.ProxyConfiguration) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) Credentials(org.apache.http.auth.Credentials) PlainConnectionSocketFactory(org.apache.http.conn.socket.PlainConnectionSocketFactory) FieldUtils(org.apache.commons.lang3.reflect.FieldUtils) SystemDefaultRoutePlanner(org.apache.http.impl.conn.SystemDefaultRoutePlanner) HttpRequestRetryHandler(org.apache.http.client.HttpRequestRetryHandler) HTTP(org.apache.http.protocol.HTTP) Before(org.junit.Before) Environment(io.dropwizard.setup.Environment) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) MetricRegistry(com.codahale.metrics.MetricRegistry) AuthConfiguration(io.dropwizard.client.proxy.AuthConfiguration) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) HttpProcessor(org.apache.http.protocol.HttpProcessor) Field(java.lang.reflect.Field) Mockito.validateMockitoUsage(org.mockito.Mockito.validateMockitoUsage) Mockito.verify(org.mockito.Mockito.verify) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) RedirectStrategy(org.apache.http.client.RedirectStrategy) AuthScope(org.apache.http.auth.AuthScope) HttpContext(org.apache.http.protocol.HttpContext) BasicHeader(org.apache.http.message.BasicHeader) HttpResponse(org.apache.http.HttpResponse) HttpHost(org.apache.http.HttpHost) Field(java.lang.reflect.Field) HostnameVerifier(javax.net.ssl.HostnameVerifier) Test(org.junit.Test)

Example 49 with HostnameVerifier

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

the class JerseyClientBuilderTest method usesACustomHostnameVerifier.

@Test
public void usesACustomHostnameVerifier() {
    final HostnameVerifier customHostnameVerifier = new NoopHostnameVerifier();
    builder.using(customHostnameVerifier);
    verify(apacheHttpClientBuilder).using(customHostnameVerifier);
}
Also used : NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) NoopHostnameVerifier(org.apache.http.conn.ssl.NoopHostnameVerifier) HostnameVerifier(javax.net.ssl.HostnameVerifier) Test(org.junit.Test)

Example 50 with HostnameVerifier

use of javax.net.ssl.HostnameVerifier in project jetty.project by eclipse.

the class SSLEngineTest method testURLConnectionChunkedPost.

@Test
public void testURLConnectionChunkedPost() throws Exception {
    StreamHandler handler = new StreamHandler();
    server.setHandler(handler);
    server.start();
    SSLContext context = SSLContext.getInstance("SSL");
    context.init(null, SslContextFactory.TRUST_ALL_CERTS, new java.security.SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(context.getSocketFactory());
    URL url = new URL("https://localhost:" + connector.getLocalPort() + "/test");
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    if (conn instanceof HttpsURLConnection) {
        ((HttpsURLConnection) conn).setHostnameVerifier(new HostnameVerifier() {

            @Override
            public boolean verify(String urlHostName, SSLSession session) {
                return true;
            }
        });
    }
    conn.setConnectTimeout(10000);
    conn.setReadTimeout(100000);
    conn.setDoInput(true);
    conn.setDoOutput(true);
    conn.setRequestMethod("POST");
    conn.setRequestProperty("Content-Type", "text/plain");
    conn.setChunkedStreamingMode(128);
    conn.connect();
    byte[] b = new byte[BODY_SIZE];
    for (int i = 0; i < BODY_SIZE; i++) {
        b[i] = 'x';
    }
    OutputStream os = conn.getOutputStream();
    os.write(b);
    os.flush();
    int len = 0;
    InputStream is = conn.getInputStream();
    int bytes = 0;
    while ((len = is.read(b)) > -1) bytes += len;
    is.close();
    assertEquals(BODY_SIZE, handler.bytes);
    assertEquals(BODY_SIZE, bytes);
}
Also used : InputStream(java.io.InputStream) ServletOutputStream(javax.servlet.ServletOutputStream) OutputStream(java.io.OutputStream) SSLSession(javax.net.ssl.SSLSession) SSLContext(javax.net.ssl.SSLContext) URL(java.net.URL) HostnameVerifier(javax.net.ssl.HostnameVerifier) HttpURLConnection(java.net.HttpURLConnection) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) Test(org.junit.Test)

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