我有一个字符串形式的令牌,我下载了公共证书并从中创建了一个公钥,如下所示.
但我不确定如何通过这么多信息进行验证.
我找到了C#和.NET的解决方案,但没有找到Java的解决方案.
请注意我没有jks文件或私钥.
FileInputStream fin = new FileInputStream("d://public.crt");
CertificateFactory f = CertificateFactory.getInstance("X.509");
X509Certificate certificate = (X509Certificate)f.generateCertificate(fin);
PublicKey pk = certificate.getPublicKey();
最佳答案 使用Auth0库(com.auth0:java-jwt)验证Java中的JWT:
>检索密钥已签名的算法,例如:
// Load your public key from a file
final PublicKey ecdsa256PublicKey = getPublicKey(...);
final Algorithm algorithm = Algorithm.ECDSA256((ECPublicKey) ecdsa256PublicKey, null);
>使用相应的算法验证其签名:
final DecodedJWT decodedJWT = JWT.decode("J.W.T[...]");
// Will throw a SignatureVerificationException if the token's signature is invalid
algorithm.verify(decodedJWT);