springboot 代码片段

77 阅读1分钟
private KeyStore loadKeyStore() throws KeyStoreException, IOException, NoSuchAlgorithmException, CertificateException {
    ClassPathResource resource = new ClassPathResource(keystoreFile);
    KeyStore keyStore = KeyStore.getInstance("JKS");
    keyStore.load(resource.getInputStream(), keystorePassword.toCharArray());
    return keyStore;
}

private RSAPublicKey loadPublicKey() throws Exception {
    KeyStore keyStore = loadKeyStore();
    Certificate certificate = keyStore.getCertificate(keystoreAlias);
    return (RSAPublicKey) certificate.getPublicKey();
}

private JWKSource<SecurityContext> loadJWKSource() throws Exception {
    KeyStore keyStore = loadKeyStore();
    PrivateKey privateKey = (PrivateKey) keyStore.getKey(keystoreAlias, keystorePassword.toCharArray());
    Certificate certificate = keyStore.getCertificate(keystoreAlias);
    RSAPublicKey publicKey = (RSAPublicKey) certificate.getPublicKey();
    JWK jwk = new RSAKey.Builder(publicKey).privateKey(privateKey).build();
    return new ImmutableJWKSet<>(new JWKSet(jwk));
}