Example usage for javax.net.ssl HttpsURLConnection setDefaultHostnameVerifier

List of usage examples for javax.net.ssl HttpsURLConnection setDefaultHostnameVerifier

Introduction

In this page you can find the example usage for javax.net.ssl HttpsURLConnection setDefaultHostnameVerifier.

Prototype

public static void setDefaultHostnameVerifier(HostnameVerifier v) 

Source Link

Document

Sets the default HostnameVerifier inherited by a new instance of this class.

Usage

From source file:org.wso2.carbon.membership.scheme.kubernetes.api.KubernetesHttpsApiEndpoint.java

private static void disableCertificateValidation() {

    TrustManager[] dummyTrustMgr = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return new X509Certificate[0];
        }/*ww w.  j  ava2 s  . c om*/

        public void checkClientTrusted(X509Certificate[] certs, String authType) {
            // do nothing
        }

        public void checkServerTrusted(X509Certificate[] certs, String authType) {
            // do nothing
        }
    } };

    // Ignore differences between given hostname and certificate hostname
    HostnameVerifier dummyHostVerifier = new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            // always true
            return true;
        }
    };

    // Install the all-trusting trust manager
    try {
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, dummyTrustMgr, new SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        HttpsURLConnection.setDefaultHostnameVerifier(dummyHostVerifier);
    } catch (Exception ignored) {
    }
}

From source file:net.reichholf.dreamdroid.helpers.SimpleHttpClient.java

private void init() {
    //TODO Do not trust all hosts without asking the user
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            return true;
        }// w  ww  . j  a  v  a2s .co  m
    });

    applyConfig();
}

From source file:com.ethercamp.harmony.util.TrustSSL.java

public static void applyAnother() {
    try {/* w w  w.  ja  v a2  s  . c  o  m*/
        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
            @Override
            public void checkClientTrusted(java.security.cert.X509Certificate[] x509Certificates,
                    String authType) throws CertificateException {
                System.out.println(
                        "x509Certificates = [" + x509Certificates + "], authType = [" + authType + "]");
            }

            @Override
            public void checkServerTrusted(java.security.cert.X509Certificate[] x509Certificates,
                    String authType) throws CertificateException {
                System.out.println(
                        "x509Certificates = [" + x509Certificates + "], authType = [" + authType + "]");
            }

            public java.security.cert.X509Certificate[] getAcceptedIssuers() {
                return null;
            }
        } };

        // Install the all-trusting trust manager
        SSLContext sc = SSLContext.getInstance("SSL");
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
        HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());

        // Create all-trusting host name verifier
        HostnameVerifier allHostsValid = new HostnameVerifier() {
            public boolean verify(String hostname, SSLSession session) {
                System.out.println("hostname = [" + hostname + "], session = [" + session + "]");
                return true;
            }
        };

        // Install the all-trusting host verifier
        HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:test.integ.be.fedict.trust.XKMSTrustTest.java

@Test
public void testValidateUnilateralTLSTrustFail() throws Exception {
    LOG.debug("validate using unilateral TLS Trust, should fail.");

    // Setup/*  w  w  w  .j a  va  2s . com*/
    KeyPair keyPair = TestUtils.generateKeyPair();

    /*
     * Override default verification that CN of server SSL certificate has
     * to be equal to the hostname.
     */
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
        public boolean verify(String hostname, SSLSession session) {
            if (TestUtils.XKMS_WS_HOST.equals(hostname)) {
                return true;
            }
            return false;
        }
    });

    // setup
    List<X509Certificate> signCertificateChain = TestUtils.getSignCertificateChain();
    XKMS2Client client = new XKMS2Client(
            "https://" + TestUtils.XKMS_WS_HOST + ":" + port + TestUtils.XKMS_WS_CONTEXT_PATH);
    client.setServicePublicKey(keyPair.getPublic());

    /*
     * Operate: validate non repudiation
     */
    try {
        client.validate(TrustServiceDomains.BELGIAN_EID_NON_REPUDIATION_TRUST_DOMAIN, signCertificateChain);
        fail();
    } catch (ClientTransportException e) {
        // expected
    }
}

From source file:org.jevis.commons.driver.DataSourceHelper.java

static public void doTrustToCertificates() throws Exception {
    Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }/*w  w  w. j a v  a  2s . co m*/

        public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException {
            return;
        }

        public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException {
            return;
        }
    } };

    SSLContext sc = SSLContext.getInstance("SSL");
    sc.init(null, trustAllCerts, new SecureRandom());
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            if (!urlHostName.equalsIgnoreCase(session.getPeerHost())) {
                System.out.println("Warning: URL host '" + urlHostName + "' is different to SSLSession host '"
                        + session.getPeerHost() + "'.");
            }
            return true;
        }
    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
}

From source file:net.sileht.lullaby.Utils.java

public static void setSSLCheck(Boolean insecure) {
    if (insecure) {
        HttpsURLConnection.setDefaultSSLSocketFactory(SSLCertificateSocketFactory.getInsecure(-1, null));
        HttpsURLConnection.setDefaultHostnameVerifier(
                org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    } else {/*from  w ww. j  av a  2 s.c  o  m*/
        HttpsURLConnection.setDefaultSSLSocketFactory(SSLCertificateSocketFactory.getDefault(-1, null));
        HttpsURLConnection.setDefaultHostnameVerifier(
                org.apache.http.conn.ssl.SSLSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
    }
}

From source file:org.orcid.examples.jopmts.impl.SSLConfig.java

@Override
public void afterPropertiesSet() throws Exception {
    trustSelfSignedSSL();/*from  ww  w.  j ava2 s .co m*/

    HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String urlHostName, SSLSession session) {
            System.out.println("Warning: URL Host: " + urlHostName + " vs. " + session.getPeerHost());
            return true;
        }

    };
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
}

From source file:org.wso2.carbon.registry.es.utils.EmailUtil.java

/**
 * Initializes the httpClient./*from w  w  w.  j av a2  s  .c o  m*/
 */
public static void initialize() throws XPathExpressionException {

    DefaultHttpClient client = new DefaultHttpClient();

    HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
    SSLSocketFactory socketFactory = SSLSocketFactory.getSocketFactory();
    Scheme sch = new Scheme("https", 443, socketFactory);
    ClientConnectionManager mgr = client.getConnectionManager();
    mgr.getSchemeRegistry().register(sch);
    httpClient = new DefaultHttpClient(mgr, client.getParams());

    // Set verifier
    HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
}

From source file:com.zimbra.common.net.SocketFactories.java

private synchronized static void register(X509TrustManager tm) {
    if (registered)
        return;//from  ww  w. j av  a 2s  .c o  m

    // Set default TrustManager
    TrustManagers.setDefaultTrustManager(tm);

    // Register Apache Commons HTTP/HTTPS protocol socket factories
    ProtocolSocketFactory psf = defaultProtocolSocketFactory();
    Protocol.registerProtocol(HTTP, new Protocol(HTTP, psf, 80));
    ProtocolSocketFactory spsf = defaultSecureProtocolSocketFactory();
    Protocol.registerProtocol(HTTPS, new Protocol(HTTPS, spsf, 443));

    // HttpURLConnection already uses system ProxySelector by default
    // Set HttpsURLConnection SSL socket factory and optional hostname verifier
    HttpsURLConnection.setDefaultSSLSocketFactory(defaultSSLSocketFactory(false));
    if (tm instanceof CustomTrustManager) {
        HttpsURLConnection.setDefaultHostnameVerifier(new CustomHostnameVerifier());
    }

    // Set the system-wide default ProxySelector
    ProxySelector.setDefault(ProxySelectors.defaultProxySelector());

    registered = true;
}

From source file:org.hyperic.hq.plugin.appha.VSphereUtil.java

private static void configureSSLKeystore() {
    AgentKeystoreConfig keystoreConfig = new AgentKeystoreConfig();
    SSLProvider sslProvider = new DefaultSSLProviderImpl(keystoreConfig,
            keystoreConfig.isAcceptUnverifiedCert());
    SSLContext sslContext = sslProvider.getSSLContext();
    HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(new AllowAllHostnameVerifier());
}