KEYCLOAK-829 Adjustment to KetstoreUtil to support loading keystore from classpath

This commit is contained in:
Timon Veenstra 2014-11-06 21:43:49 +01:00
parent c0f377c8c7
commit 7c9e3f4555

View file

@ -2,6 +2,7 @@ package org.keycloak.util;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.security.KeyStore;
/**
@ -9,14 +10,16 @@ import java.security.KeyStore;
* @version $Revision: 1 $
*/
public class KeystoreUtil {
private static final String PROTOCOL_CLASSPATH = "classpath:";
public static KeyStore loadKeyStore(String filename, String password) throws Exception {
KeyStore trustStore = KeyStore.getInstance(KeyStore
.getDefaultType());
File truststoreFile = new File(filename);
FileInputStream trustStream = new FileInputStream(truststoreFile);
KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
InputStream trustStream = (filename.startsWith(PROTOCOL_CLASSPATH))
?KeystoreUtil.class.getResourceAsStream(filename.replace(PROTOCOL_CLASSPATH, ""))
:new FileInputStream(new File(filename));
trustStore.load(trustStream, password.toCharArray());
trustStream.close();
return trustStore;
}
}