View Javadoc
1   package civitas.common;
2   
3   import java.io.IOException;
4   import java.nio.file.Files;
5   import java.nio.file.Paths;
6   import java.security.KeyStore;
7   import java.security.KeyStoreException;
8   import java.security.NoSuchAlgorithmException;
9   import java.security.PublicKey;
10  import java.security.cert.CertificateException;
11  import java.util.HashMap;
12  import java.util.Map;
13  
14  import org.springframework.beans.factory.annotation.Autowired;
15  import org.springframework.stereotype.Controller;
16  
17  @Controller
18  public class GetPublicKey {
19  	@Autowired
20  	Configuration configuration;
21  
22  	Map<String, PublicKey> cache = new HashMap<>();
23  
24  	public PublicKey apply(final String storeFile, final String storePassword, final String serverKeyEntry)
25  			throws KeyStoreException, NoSuchAlgorithmException, CertificateException, IOException {
26  		if (null == cache.get(serverKeyEntry)) {
27  			char[] pwdArray = storePassword.toCharArray();
28  			KeyStore store = KeyStore.getInstance("JKS");
29  			store.load(Files.newInputStream(Paths.get(storeFile)), pwdArray);
30  			cache.put(serverKeyEntry, store.getCertificate(serverKeyEntry).getPublicKey());
31  		}
32  		return cache.get(serverKeyEntry);
33  	}
34  }