public static PublicKey stringToPublicKey(String publicKeyString) throws Exception {
try {
if (publicKeyString.contains("-----BEGIN PUBLIC KEY-----") || publicKeyString.contains("-----END PUBLIC KEY-----"))
publicKeyString = publicKeyString.replace("-----BEGIN PUBLIC KEY-----", "").replace("-----END PUBLIC KEY-----", "");
byte[] keyBytes = Base64.decode(publicKeyString);
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
return keyFactory.generatePublic(spec);
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
<dependency>
<groupId>com.auth0</groupId>
<artifactId>java-jwt</artifactId>
<version>3.19.0</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
Jws<Claims> parseClaimsJws = Jwts.parser().setSigningKey(publicKey).parseClaimsJws(tokenValue);
keycloak校验
AccessToken token = TokenVerifier.create(accessTokenString, AccessToken.class).getToken();
AccessToken token2 = RSATokenVerifier.create(accessTokenString).realmUrl("http://191.168.9.146:8080/realms/exceeddata").checkActive(true).publicKey(publicKey).verify().getToken();