Home > Back-end >  Keycloak JWT Validation using Java Spring Security KC Adapter
Keycloak JWT Validation using Java Spring Security KC Adapter

Time:06-22

I have gone through multiple blog posts and StackOverflow questions before writing my own. I have multiple queries and none of the posts answer them.

I am using Keycloak Spring Security Adapter to secure my legacy Spring application. I referred to the keycloak documentation enter image description here

For eg: Access to localhost:8080/about.htm will redirect me to keycloak login screen and after successful authentication, I will be able to view my page. I am also using the below code to read the user details from the token,

        KeycloakPrincipal<KeycloakSecurityContext> kp = (KeycloakPrincipal<KeycloakSecurityContext>) auth.getPrincipal();
        IDToken idToken = kp.getKeycloakSecurityContext().getIdToken();
        user.setUsername(idToken.getPreferredUsername());

Now when I test this application using postman and modify the generated access token, obviously the server gives an error. Ref : How to test application using postman.

However, this is the flow :

Client sends a request to the resource server, resource server checks for a token - if it exists, the client does the validation. If it doesn’t exist or is invalid, it redirects to the authorization server (KC).

My question is,

  1. Who is validating this token? How does postman flow throw an error if I fiddle with the token?
  2. Do I really need to write a JwtTokenValidator in my application for each request? Won't that be overkill?
  3. If I use Client Authenticator as Signed Jwt with client secret, will this validation still be required? I am not using it as it introduces latency.

Please assist.

CodePudding user response:

Answer to #1:

When you use any Keycloak adapters in your application (in your case the Spring adapter for Keycloak), that's the one who does the validation and redirects to the login if necessary. As part of the validation, it checks the signature of the token issued by Keycloak. So when you fiddle with the token, the signature doesn't match, hence it throws an error.

Answer to #2

No, you shouldn't need to implement a JwtTokenValidator. The adapter does it for you and a request should reach your endpoint/URL only if it has a valid token. You may only need to do that if you have a special requirements about validating the token (e.g. checking specific claim in the token against some service). Otherwise, you can safely use the claims in the token you received from the KeycloakSecurityContext. You can even setup authorization based on your URL patterns and Keycloak will enforce them too and allow the request to pass if user has necessary roles (like this example).

Answer to #3:

That option only changes the method used to authenticate your app to the Keycloak and has nothing to do with the user's token validation inside your app. In your current setup, when your app wants to communicate with Keycloak (e.g. to exchange auth code with auth token), it authenticate itself to Keycloak with a client-id/client-secret pair (otherwise Keycloak would not know it's your app and will reject the request).

If you choose the "Signed Jwt with Client Secret" option, your client can not just use a client-secret to authenticate to Keycloak. It should support the RFC7523 specification. So it's quite complex in compare with a simple clien-secret approach. In an environment in which you trust your clients (e.g. they're all known apps developed inside the company and you're not going to support public clients to join your Keycloak and use its services) it's quite common and safe to use client-secret approach.

  • Related