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:wptools.cmds.DumpCerts.java

private static void installDummyCertManager() {
    // Create a trust manager
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public X509Certificate[] getAcceptedIssuers() {
            return null;
        }//w w  w  . ja v  a  2  s  . c o  m

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

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

        private void dumpCerts(X509Certificate[] certs) {
            for (X509Certificate cert : certs)
                dumpCert(cert);
        }
    } };

    // Install the trust manager
    SSLContext sc = null;
    try {
        sc = SSLContext.getInstance("SSL");
    } catch (NoSuchAlgorithmException e) {
        throw new RuntimeException(e);
    }

    // Create empty HostnameVerifier
    HostnameVerifier hv = new HostnameVerifier() {
        public boolean verify(String arg0, SSLSession arg1) {
            return true;
        }
    };

    try {
        sc.init(null, trustAllCerts, new java.security.SecureRandom());
    } catch (KeyManagementException e) {
        throw new RuntimeException(e);
    }
    HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
    HttpsURLConnection.setDefaultHostnameVerifier(hv);
}

From source file:com.bytelightning.opensource.pokerface.HelloWorldScriptTest.java

@AfterClass
public static void tearDownAfterClass() throws Exception {
    HttpsURLConnection.setDefaultHostnameVerifier(PrevHostnameVerifier);
    HttpsURLConnection.setDefaultSSLSocketFactory(PrevSocketFactory);

    if (proxy != null)
        proxy.stop();/*from  w ww.ja  va 2  s  .  c om*/
}

From source file:riddimon.android.asianetautologin.HttpUtils.java

private HttpUtils(Context context) {
    // private constructor to prevent instantiation
    this.context = context;
    try {//from   w  w  w  .ja va2  s  . c  o m
        // get version number to be set as part of user agent string
        version = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionName;
    } catch (NameNotFoundException e) {
    }
    if (debug) {
        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });
        try {
            TrustManager[] trustManagers = new X509TrustManager[1];
            trustManagers[0] = new TrustAllManager();

            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustManagers, null);
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception ex) {
        }
    }
    // We don't enable response cache because this scenario requires fresh
    // data every time
    //enableHttpResponseCache();
}

From source file:org.wisdom.framework.vertx.VertxDispatcherTest.java

public void prepareHttps() throws KeyManagementException, NoSuchAlgorithmException {
    // Create a trust manager that does not validate certificate chains
    TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {
        public java.security.cert.X509Certificate[] getAcceptedIssuers() {
            return null;
        }/*from  w  w w  . j  a v a  2  s  . co m*/

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

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

    // 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 = (hostname, session) -> true;

    // Install the all-trusting host verifier
    HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid);

}

From source file:org.qi4j.library.http.AbstractSecureJettyTest.java

@BeforeClass
public static void beforeSecureClass() throws IOException, GeneralSecurityException {
    defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
    defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
    HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {

        public boolean verify(String string, SSLSession ssls) {
            return true;
        }//w w  w .  j  ava  2  s  . c  om

    });
    KeyStore truststore = KeyStore.getInstance("JCEKS");
    truststore.load(new FileInputStream(TRUSTSTORE_FILE), KS_PASSWORD.toCharArray());
    SSLContext sslCtx = SSLContext.getInstance("TLS");
    TrustManagerFactory caTrustManagerFactory = TrustManagerFactory.getInstance(getX509Algorithm());
    caTrustManagerFactory.init(truststore);
    sslCtx.init(null, caTrustManagerFactory.getTrustManagers(), null);
    HttpsURLConnection.setDefaultSSLSocketFactory(sslCtx.getSocketFactory());
}

From source file:com.hybris.mobile.lib.http.manager.volley.VolleyPersistenceManager.java

public VolleyPersistenceManager(Context applicationContext, SSLSocketFactory sslSocketFactory,
        HostnameVerifier hostnameVerifier) {

    if (hostnameVerifier != null) {
        HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
    }//from w  ww.ja v  a2 s  . c om

    // One queue using background threads for callbacks
    this.mQueue = newRequestQueueBackgroundThread(applicationContext.getApplicationContext(), sslSocketFactory);

    // Image loader using regular queue from Volley
    this.mImageLoader = new ImageLoader(
            newRequestQueueBackgroundThread(applicationContext.getApplicationContext(), sslSocketFactory),
            new BitmapCache());
}

From source file:es.logongas.util.seguridad.AuthenticationProviderImplMoodle.java

@Override
public Principal authenticate(Credential credential) {
    try {// ww w. j a  va 2s. c  om
        StrongPasswordEncryptor passwordEncryptor = new StrongPasswordEncryptor();

        if ((credential instanceof CredentialImplLoginPassword) == false) {
            return null;
        }

        CredentialImplLoginPassword credentialImplLoginPassword = (CredentialImplLoginPassword) credential;

        if (loginAdmin.equalsIgnoreCase(credentialImplLoginPassword.getLogin())) {

            if (passwordEncryptor.checkPassword(credentialImplLoginPassword.getPassword(),
                    passwordAdmin) == false) {
                return null;
            }

        } else {

            HostnameVerifier hostnameVerifier = org.apache.http.conn.ssl.SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
            HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
            DefaultHttpClient httpClientPost = new DefaultHttpClient();
            HttpPost httpPost = new HttpPost(moodleLoginURL);
            List<NameValuePair> nvps = new ArrayList<NameValuePair>();
            nvps.add(new BasicNameValuePair("username", credentialImplLoginPassword.getLogin()));
            nvps.add(new BasicNameValuePair("password", credentialImplLoginPassword.getPassword()));
            httpPost.setEntity(new UrlEncodedFormEntity(nvps));
            HttpResponse response1 = httpClientPost.execute(httpPost);
            InputStream inputStream = response1.getEntity().getContent();
            Document document = Jsoup.parse(inputStreamToString(inputStream));

            Elements divElements = document.getElementsByClass("logininfo");
            if (divElements.size() == 0) {
                return null;
            }
            Element divElement = divElements.get(0);
            Elements aElements = divElement.getElementsByTag("a");
            if (aElements.size() == 0) {
                return null;
            }
            Element aElement = aElements.get(aElements.size() - 1);
            if (aElement.attr("href").indexOf("logout") < 0) {
                return null;
            }

            DefaultHttpClient httpclient2 = new DefaultHttpClient();
            HttpGet httpGet = new HttpGet(aElement.attr("href"));

            httpclient2.execute(httpGet);

        }
        GenericDAO<Identity, Integer> genericDAO = daoFactory.getDAO(Identity.class);
        Identity identity = genericDAO.readByNaturalKey(credentialImplLoginPassword.getLogin());

        return identity;

    } catch (BusinessException ex) {
        return null;
    } catch (Exception ex) {
        log.info("Fallo al conectarse al moodle", ex);
        throw new RuntimeException(ex);
    }
}

From source file:io.getlime.push.configuration.PowerAuthWebServiceConfiguration.java

/**
 * Prepare a correctly configured PowerAuthServiceClient instance with the service
 * URL specified using 'powerauth.service.url' server property.
 *
 * @param marshaller JAXB marshaller/*w  w w .  j a v  a2s.  c  o m*/
 * @return Correctly configured PowerAuthServiceClient instance with the service
 * URL specified using 'powerauth.service.url' server property
 */
@Bean
public PowerAuthServiceClient powerAuthClient(Jaxb2Marshaller marshaller) {
    PowerAuthServiceClient client = new PowerAuthServiceClient();
    client.setDefaultUri(powerAuthServiceUrl);
    client.setMarshaller(marshaller);
    client.setUnmarshaller(marshaller);

    // if invalid SSL certificates should be accepted
    if (acceptInvalidSslCertificate) {

        HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() {
            @Override
            public boolean verify(String hostname, SSLSession session) {
                return true;
            }
        });

        TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() {

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

            public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }

            public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) {
            }

        } };

        try {
            SSLContext sc = SSLContext.getInstance("SSL");
            sc.init(null, trustAllCerts, new java.security.SecureRandom());
            HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
        } catch (Exception e) {
            // ... ignore
        }

    }

    // if there is a configuration with security credentials, add interceptor
    if (!clientToken.isEmpty()) {
        ClientInterceptor[] interceptors = new ClientInterceptor[] { securityInterceptor() };
        client.setInterceptors(interceptors);
    }
    return client;
}

From source file:org.simple.net.httpstacks.HttpUrlConnStack.java

private void configHttps(Request<?> request) {
    if (request.isHttps()) {
        SSLSocketFactory sslFactory = mConfig.getSslSocketFactory();
        // ?https
        if (sslFactory != null) {
            HttpsURLConnection.setDefaultSSLSocketFactory(sslFactory);
            HttpsURLConnection.setDefaultHostnameVerifier(mConfig.getHostnameVerifier());
        }/*  w ww.  j  av a 2  s .  c  o  m*/

    }
}

From source file:com.example.chengcheng.network.httpstacks.HttpUrlConnStack.java

private void configHttps(Request<?> request) {
    if (request.isHttps()) {
        SSLSocketFactory sslFactory = mConfig.getSslSocketFactory();
        // ?https
        if (sslFactory != null) {
            HttpsURLConnection.setDefaultSSLSocketFactory(sslFactory);
            HttpsURLConnection.setDefaultHostnameVerifier(mConfig.getHostnameVerifier());
        }//from  w w  w . j  a v  a 2s.  c o m
    }
}