public static SslContext forKeystoreAndTruststore ( InputStream keystore , String keystorePassword , InputStream truststore , String truststorePassword , String keyManagerAlgorithm ) throws SecurityContextException { try { final KeyStore ks = KeyStore . getInstance ( KEYSTORE_JKS ) ; final KeyStore ts = KeyStore . getInstance ( KEYSTORE_JKS ) ; final KeyManagerFactory keystoreKmf = KeyManagerFactory . getInstance ( keyManagerAlgorithm ) ; final TrustManagerFactory truststoreKmf = TrustManagerFactory . getInstance ( keyManagerAlgorithm ) ; ks . load ( keystore , keystorePassword . toCharArray ( ) ) ; ts . load ( truststore , truststorePassword . toCharArray ( ) ) ; keystoreKmf . init ( ks , keystorePassword . toCharArray ( ) ) ; truststoreKmf . init ( ts ) ; SslContextBuilder ctxBuilder = SslContextBuilder . forClient ( ) . keyManager ( keystoreKmf ) ; ctxBuilder . trustManager ( truststoreKmf ) ; return ctxBuilder . build ( ) ; } catch ( Exception e ) { throw new SecurityContextException ( e ) ; } }
Builds SslContext using protected keystore and truststores overriding default key manger algorithm . Adequate for mutual TLS connections .