List of usage examples for javax.net.ssl SSLContext getSocketFactory
public final SSLSocketFactory getSocketFactory()
From source file:de.bps.webservices.clients.onyxreporter.OnyxReporterConnector.java
private boolean isServiceAvailable(String target) { HostnameVerifier hv = new HostnameVerifier() { @Override//from ww w .j av a 2 s .com public boolean verify(String urlHostName, SSLSession session) { if (urlHostName.equals(session.getPeerHost())) { return true; } else { return false; } } }; HttpsURLConnection.setDefaultHostnameVerifier(hv); try { URL url = new URL(target + "?wsdl"); HttpURLConnection con = (HttpURLConnection) url.openConnection(); if (con instanceof HttpsURLConnection) { HttpsURLConnection sslconn = (HttpsURLConnection) con; SSLContext context = SSLContext.getInstance("SSL"); context.init(SSLConfigurationModule.getKeyManagers(), SSLConfigurationModule.getTrustManagers(), new java.security.SecureRandom()); sslconn.setSSLSocketFactory(context.getSocketFactory()); sslconn.connect(); if (sslconn.getResponseCode() == HttpURLConnection.HTTP_OK) { sslconn.disconnect(); return true; } } else { con.connect(); if (con.getResponseCode() == HttpURLConnection.HTTP_OK) { con.disconnect(); return true; } } } catch (Exception e) { Tracing.createLoggerFor(getClass()).error("Error while trying to connect to webservice: " + target, e); } return false; }
From source file:com.produban.cloudfoundry.bosh.bosh_javaclient.BoshClientImpl.java
/** * This method sets up {@link HttpsURLConnection} so that no certificate or * hostname check is performed./* w ww. ja v a 2s .com*/ */ private void disableSSLChecks() { try { TrustManager trustAllCertificates = new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException { // I do nothing because the way to say "OK" is not to throw // a CertificateException } @Override public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException { // I do nothing because the way to say "OK" is not to throw // a CertificateException } }; TrustManager[] trustAllCertificatesArray = { trustAllCertificates }; SSLContext sslContext = SSLContext.getInstance("SSL"); sslContext.init(null, trustAllCertificatesArray, new java.security.SecureRandom()); HostnameVerifier allHostsValid = new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; // I always say 'OK' } }; HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory()); HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); } catch (NoSuchAlgorithmException | KeyManagementException e) { throw new IllegalStateException("Something strange happened here", e); } }
From source file:com.ibm.caas.CaaSResource.java
/** * Pass throughout CERTs [workaround]//from w w w. j a va 2 s. c o m */ public void relaxHostChecking() { // Override SSL Trust manager without certificate chains validation TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { public java.security.cert.X509Certificate[] getAcceptedIssuers() { return null; } public void checkClientTrusted(X509Certificate[] certs, String authType) { } public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); // Hostname verification. HostnameVerifier allHostsValid = new HostnameVerifier() { /** * Verify that the host name is an acceptable match with the server's authentication scheme. * @hostname - the host name * @session - SSLSession used on the connection to host * @return true if the host name is acceptable */ public boolean verify(String hostname, SSLSession session) { return true; } }; // Sets the default HostnameVerifier by all-trusting host verifier. HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); } catch (NoSuchAlgorithmException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (KeyManagementException e) { // TODO Auto-generated catch block e.printStackTrace(); } }
From source file:com.dao.ShopThread.java
private HttpsURLConnection getHttpSConn(String payurl, String method, String strlength) throws Exception { // SSLContext?? TrustManager[] tm = { new MyX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, tm, new java.security.SecureRandom()); // SSLContextSSLSocketFactory SSLSocketFactory ssf = sslContext.getSocketFactory(); // Acts like a browser URL obj = new URL(payurl); HttpsURLConnection conn;/*from w w w .ja v a 2s .c o m*/ conn = (HttpsURLConnection) obj.openConnection(); conn.setSSLSocketFactory(ssf); conn.setRequestMethod(method); conn.setRequestProperty("Host", "mypay.5173.com"); conn.setRequestProperty("User-Agent", USER_AGENT); conn.setRequestProperty("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); conn.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded"); // conn.setRequestProperty("X-Requested-With", "XMLHttpRequest"); conn.setRequestProperty("Referer", payurl); conn.setRequestProperty("Connection", "keep-alive"); // conn.setRequestProperty("Pragma", "no-cache"); // conn.setRequestProperty("Cache-Control", "no-cache"); conn.setRequestProperty("Content-Length", strlength); conn.setDoOutput(true); conn.setDoInput(true); return conn; }
From source file:com.dao.ShopThread.java
private HttpsURLConnection getHttpSConn(String httpsurl) throws Exception { // SSLContext?? TrustManager[] tm = { new MyX509TrustManager() }; SSLContext sslContext = SSLContext.getInstance("SSL", "SunJSSE"); sslContext.init(null, tm, new java.security.SecureRandom()); // SSLContextSSLSocketFactory SSLSocketFactory ssf = sslContext.getSocketFactory(); // Acts like a browser URL obj = new URL(httpsurl); HttpsURLConnection conn;/* w ww.j av a 2s .c o m*/ conn = (HttpsURLConnection) obj.openConnection(); conn.setSSLSocketFactory(ssf); conn.setRequestMethod("GET"); if (null != this.cookies) { conn.addRequestProperty("Cookie", GenericUtil.cookieFormat(this.cookies)); } conn.setRequestProperty("Host", "security.5173.com"); conn.setRequestProperty("User-Agent", USER_AGENT); conn.setRequestProperty("Accept", "*/*"); conn.setRequestProperty("Accept-Language", "zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3"); conn.setRequestProperty("Accept-Encoding", "gzip, deflate"); conn.setRequestProperty("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8"); conn.setRequestProperty("Connection", "keep-alive"); conn.setRequestProperty("Pragma", "no-cache"); conn.setRequestProperty("Cache-Control", "no-cache"); conn.setDoOutput(true); conn.setDoInput(true); return conn; }
From source file:com.njlabs.amrita.aid.gpms.ui.GpmsActivity.java
public Picasso getUnsecuredPicassoDownloader() { try {//ww w .ja v a 2 s. c o m TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public void checkClientTrusted(X509Certificate[] certs, String authType) { } @Override public void checkServerTrusted(X509Certificate[] certs, String authType) { } @Override public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0]; } } }; SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new SecureRandom()); Picasso.Builder builder = new Picasso.Builder(baseContext).listener(new Picasso.Listener() { @Override public void onImageLoadFailed(Picasso arg0, Uri arg1, Exception ex) { ex.printStackTrace(); } }); OkHttpClient client = new OkHttpClient.Builder().sslSocketFactory(sc.getSocketFactory()) .hostnameVerifier(new HostnameVerifier() { @Override public boolean verify(String hostname, SSLSession session) { return true; } }).build(); Downloader downloader = new OkHttp3Downloader(client); return builder.downloader(downloader).build(); } catch (Exception ignored) { Picasso.Builder builder = new Picasso.Builder(baseContext).listener(new Picasso.Listener() { @Override public void onImageLoadFailed(Picasso arg0, Uri arg1, Exception ex) { ex.printStackTrace(); } }); return builder.build(); } }
From source file:orca.ektorp.client.ContextualSSLSocketFactory.java
/** * @since 4.1// ww w . java 2 s . c om * @param sslContext SSL Context * @param hostnameVerifier Host Name Verifier */ public ContextualSSLSocketFactory(final SSLContext sslContext, final X509HostnameVerifier hostnameVerifier) { super(); if (sslContext == null) { throw new IllegalArgumentException("SSL context may not be null"); } this.socketfactory = sslContext.getSocketFactory(); this.hostnameVerifier = hostnameVerifier; this.nameResolver = null; }
From source file:net.subclient.subsonic.SubsonicConnection.java
/** * Prepares HTTPS connections to accept any domains and self-signed certificates * @throws NoSuchAlgorithmException /*from w w w .j a v a 2s .co m*/ * @throws KeyManagementException */ private void setSSLProperties() throws NoSuchAlgorithmException, KeyManagementException { //Disable certificate chacks to force trust self-signed certificates TrustManager[] trustAllCerts = new TrustManager[] { new X509TrustManager() { @Override public X509Certificate[] getAcceptedIssuers() { return null; } @Override public void checkClientTrusted(X509Certificate[] certs, String authType) { } @Override public void checkServerTrusted(X509Certificate[] certs, String authType) { } } }; SSLContext sc = null; sc = SSLContext.getInstance("SSL"); sc.init(null, trustAllCerts, new java.security.SecureRandom()); HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); //Set a hostname verifier that trust any hostname HostnameVerifier allHostsValid = new HostnameVerifier() { @Override public boolean verify(String arg0, SSLSession arg1) { return true; } }; HttpsURLConnection.setDefaultHostnameVerifier(allHostsValid); }