Java 读取公钥

2020年02月29日 48点热度 0人点赞 0条评论

将公钥文件中的内容读出

String publicKey=null;
Resource resource=new ClassPathResource("oauth2.pub");
try (BufferedReader bufferedReader=new BufferedReader(new FileReader(resource.getFile()))){
    publicKey=bufferedReader.lines().collect(Collectors.joining("\n"));
    System.out.println(publicKey);
} catch (IOException e) {
    e.printStackTrace();
}

将读出的内容转换成PublicKey对象

public static PublicKey getPublicKey(String key) throws Exception {
    byte[] byteCert = Base64.decode(key.getBytes());
    ByteArrayInputStream bain = new ByteArrayInputStream(byteCert);
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    X509Certificate cert = (X509Certificate)cf.generateCertificate(bain);
    return cert.getPublicKey();
}

公钥是读取出来了,但是jwt报错说长度不一致,还木有解决= =

likole

一个后端小白

文章评论