Skip to content

Instantly share code, notes, and snippets.

@shoota
Last active August 29, 2015 14:12
Show Gist options
  • Save shoota/7a0d629cb2d1c87dc328 to your computer and use it in GitHub Desktop.
Save shoota/7a0d629cb2d1c87dc328 to your computer and use it in GitHub Desktop.
Javaクライアント認証(Android)
protected void registerClientCertToSSLSocket(Context context) throws Exception {
KeyManagerFactory keyManagerFactory;
final char[] PASSWORD = "password".toCharArray();
InputStream inputStream;
try {
inputStream = context.getResources().getAssets().open("client.p12");
KeyStore keyStore = KeyStore.getInstance("PKCS12");
keyStore.load(inputStream, PASSWORD);
keyManagerFactory = KeyManagerFactory.getInstance("X509");
keyManagerFactory.init(keyStore, PASSWORD);
SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(keyManagerFactory.getKeyManagers(), null, null);
HttpsURLConnection.setDefaultSSLSocketFactory(sslContext.getSocketFactory());
} catch (IOException e) {
log.error("#HttpsClient", "not found cert-file");
throw new Exception(e);
} catch (KeyStoreException e) {
log.error("#HttpsClient", "keystore instance exception");
throw new Exception(e);
} catch (NoSuchAlgorithmException e) {
log.error("#HttpsClient", "Not Support X509 Algorigthm");
throw new Exception(e);
} catch (CertificateException e) {
log.error("#HttpsClient", "Certification error");
throw new Exception(e);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment