KeystoreFactory { @SneakyThrows public KeyStore loadKeystore(String certResourceLocation, String privateKeyResourceLocation, String alias, String keyPassword) { KeyStore keystore = createEmptyKeystore(); X509Certificate cert = loadCert(certResourceLocation); RSAPrivateKey privateKey = loadPrivateKey(privateKeyResourceLocation); addKeyToKeystore(keystore, cert, privateKey, alias, keyPassword); return keystore; } KeystoreFactory();  KeystoreFactory(ResourceLoader resourceLoader); @SneakyThrows KeyStore loadKeystore(String certResourceLocation, String privateKeyResourceLocation, String alias, String keyPassword); @SneakyThrows void addKeyToKeystore(KeyStore keyStore, X509Certificate cert, RSAPrivateKey privateKey, String alias, String password); @SneakyThrows KeyStore createEmptyKeystore(); @SneakyThrows X509Certificate loadCert(String certLocation); @SneakyThrows RSAPrivateKey loadPrivateKey(String privateKeyLocation); void setResourceLoader(DefaultResourceLoader resourceLoader);  }
@Test public void loadKeystore() throws Exception { KeystoreFactory keystoreFactory = new KeystoreFactory(new DefaultResourceLoader()); KeyStore keyStore = keystoreFactory.loadKeystore("classpath:/localhost.cert", "classpath:/localhost.key.der", "alias", "password"); assertThat(keyStore.containsAlias("alias")).isTrue(); assertThat(keyStore.size()).isEqualTo(1); Certificate cert = keyStore.getCertificate("alias"); assertThat(cert.getType()).isEqualTo("X.509"); cert.verify(cert.getPublicKey()); Key key = keyStore.getKey("alias", "password".toCharArray()); assertThat(key.getAlgorithm()).isEqualTo("RSA"); assertThat(key.getFormat()).isEqualTo("PKCS#8"); }