Created
June 28, 2013 07:22
-
-
Save taichi/5883035 to your computer and use it in GitHub Desktop.
SSLのベリファイアをドスルーするアレ。
オレオレ証明書を使ってるhttpsサーバにアレコレする際に証明書のインストールをせずに済ますコード。
非常に危険なので基本的にテスト用途でのみ使用する事。
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.net.ssl.HostnameVerifier; | |
import javax.net.ssl.HttpsURLConnection; | |
import javax.net.ssl.SSLSession; | |
public class InSecure { | |
public static void setUp() { | |
try { | |
passthrough(); | |
trustAllHttpsCertificates(); | |
} catch (Exception e) { | |
throw new IllegalStateException(e); | |
} | |
} | |
public static void passthrough() { | |
HttpsURLConnection.setDefaultHostnameVerifier(new HostnameVerifier() { | |
@Override | |
public boolean verify(String hostname, SSLSession session) { | |
return true; | |
} | |
}); | |
} | |
public static void trustAllHttpsCertificates() throws Exception { | |
// Create a trust manager that does not validate certificate chains: | |
javax.net.ssl.TrustManager[] trustAllCerts = { null }; | |
trustAllCerts[0] = new TrustAllTrustManager(); | |
javax.net.ssl.SSLContext sc = javax.net.ssl.SSLContext.getInstance("SSL"); | |
javax.net.ssl.SSLSessionContext sslsc = sc.getServerSessionContext(); | |
sslsc.setSessionTimeout(0); | |
sc.init(null, trustAllCerts, null); | |
javax.net.ssl.HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory()); | |
} | |
static class TrustAllTrustManager implements javax.net.ssl.TrustManager, javax.net.ssl.X509TrustManager { | |
@Override | |
public java.security.cert.X509Certificate[] getAcceptedIssuers() { | |
return null; | |
} | |
@Override | |
public void checkServerTrusted(java.security.cert.X509Certificate[] certs, String authType) | |
throws java.security.cert.CertificateException { | |
return; | |
} | |
@Override | |
public void checkClientTrusted(java.security.cert.X509Certificate[] certs, String authType) | |
throws java.security.cert.CertificateException { | |
return; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment