1

I am trying to verify the AccessToken using below code -

TokenVerifier verifier = TokenVerifier.create(StringAccessToken, AccessToken.class).withDefaultChecks(); PublicKey publicKey = getRealmPublicKey(verifier.getHeader()); return verifier.realmUrl(“someStringUrl”).publicKey(publicKey).verify().getToken();

But seems a realmUrl() method is deprecated. Can you please help with alternative solution for this?

or can you suggest an effective way to verify access token ?

  • 1
    I found the source of the mentioned class and the deprecation comment. https://github.com/keycloak/keycloak/blob/master/core/src/main/java/org/keycloak/TokenVerifier.java#L358 I would not be afraid of this. If the method disappers, your code wont compile any more. What is your context? are you inside springboot? are you authenticating a http request? – Matthias Wiedemann Sep 23 '21 at 12:39
  • I am authenticating a http request. I am trying to the verify the token with the public key of the keycloak server. Got below reply from keucloak people which helped resolved the issue - take a look at org.keycloak.TokenVerifier#withChecks which takes predicates for token validation. The realm check is performed by org.keycloak.TokenVerifier.RealmUrlCheck – Ganesh Dhongade Sep 24 '21 at 05:25
  • if you have found an answer to your question, it would be good, that you post your answer as answer and not as a comment, so that other people can see, that the question is answered and that no futher help is needed. – Matthias Wiedemann Sep 27 '21 at 05:51

1 Answers1

0

We can use org.keycloak.TokenVerifier#withChecks which takes predicates for token validation. The realm check is performed by org.keycloak.TokenVerifier.RealmUrlCheck

The modified code would be:

TokenVerifier<AccessToken> verifier = TokenVerifier.create(token, AccessToken.class).withChecks(new TokenVerifier.RealmUrlCheck(getRealmUrl()));
PublicKey publicKey = getRealmPublicKey(verifier.getHeader());
return verifier.publicKey(publicKey).verify().getToken();
JW Geertsma
  • 857
  • 3
  • 13
  • 19