I am working on a Spring-based token mediating server with Keycloak as the authorization server.
Upon successful login, I receive a session ID, access token, and refresh token.
I save the session ID and refresh token in my token mediating server.
Here’s how my system works:
The frontend calls the session API with the session ID in a cookie.
The token mediating server uses the refresh token to generate a new access token and returns it to the frontend.
If the session expires in Keycloak, my token mediating server doesn’t know because it relies on its database for session IDs.
To address this, I implemented a check to verify the session status in Keycloak whenever I reference my session ID table.
This ensures that sessions are cleared from both the browser and the token mediating server database if they are no longer valid in Keycloak.
However, I face an issue where the session ID might be lost in the browser (e.g., due to clearing cookies or using incognito mode).
This leaves stale sessions in my database, although Keycloak eventually removes them.
I have a scheduler to periodically clear stale sessions from the token mediating server database if they are no longer in Keycloak.
My questions are:
Is it the right approach to keep session checks in both Keycloak and my database?
If I want to remove the table storing refresh tokens, how should I handle generating new tokens?
If I am not using refresh tokens, is re-initiating the login flow the right approach?
Any advice or best practices for managing sessions and tokens in this setup would be greatly appreciated. Thank you!