Hi @서인국님
Thank you for posting this in Microsoft Q&A.
I understand you query is related to validating Azure AD access tokens in java.
Can you check exp
and aud
values in access token. It's possible that thejwsObject.verify(verifier)
method is returning false because the token has expired or has been tampered with.The exp
claim contains the expiration time of the token and the iss
claim contains the issuer of the token.
Below is the Sample code:
public boolean validateToken(String token) {
try {
URL jwkSetURL = new URL("https://login.microsoftonline.com/" + tenantId + "/discovery/keys?appid=" + clientId);
JWKSet jwkSet = JWKSet.load(jwkSetURL);
JWSObject jwsObject = JWSObject.parse(token);
JWSAlgorithm algorithm = jwsObject.getHeader().getAlgorithm();
if (!algorithm.equals(JWSAlgorithm.RS256)) {
throw new IllegalArgumentException("No RS256");
}
RSAKey rsaKey = null;
for (JWK jwk : jwkSet.getKeys()) {
if (jwk.getKeyID().equals(jwsObject.getHeader().getKeyID())) {
rsaKey = (RSAKey) jwk;
break;
}
}
if (rsaKey == null) {
throw new IllegalArgumentException("No publicKey");
}
PublicKey publicKey = rsaKey.toRSAPublicKey();
RSASSAVerifier verifier = new RSASSAVerifier((RSAPublicKey) publicKey);
boolean isVerified = jwsObject.verify(verifier);
if (!isVerified) {
return false;
}
Date expirationTime = jwsObject.getPayload().getExpirationTime();
if (expirationTime != null && expirationTime.before(new Date())) {
return false;
}
For your reference: https://www.baeldung.com/java-jwt-check-expiry-no-exception
Please let me know if you are facing any issue while executing code via comments section.
Hope this helps. Do let us know if you any further queries.
Thanks,
Navya.