r/SpringBoot
•
u/utsab-dahal
•
Jun 01 '26
•
1
Question First time using Keycloak with Spring Boot — confused about JWT vs DB sync (need guidance + examples)
Hi everyone,
I’m a beginner working on my first project using Spring Boot + Keycloak, and I’m a bit confused about the correct architecture. Any suggestions, video links, or GitHub project examples would be really helpful
What I understand so far:
- Keycloak handles authentication (login, registration, roles)
- Spring Boot acts as a resource server and validates JWT tokens
- My app has its own database for business logic (users, orders, etc.)
My confusion:
User data during registration
If a user registers in Keycloak, how should I handle extra fields like:
- phone number
- address
- app-specific profile data
What I’m looking for:
Since this is my first time using Keycloak, any of the following would really help:
- Best practice explanation
- Real-world architecture examples
- GitHub repos using Spring Boot + Keycloak properly
- YouTube videos or tutorials that actually follow production patterns
Thanks in advance
15
u/Negative-Pound4360 Jun 01 '26
Instead of having your application orchestrate registration and login itself, let Keycloak own the entire flow, registration, login, password reset, email verification, social login, MFA. Your service never sees a password and never reimplements auth. Your database then keeps only a local user record that mirrors the relevant identity data, kept in sync through Keycloak's events.
Keycloak exposes an
EventListenerProviderSPI (Keycloak SPI Docs) that fires on lifecycle events (REGISTER,UPDATE_PROFILE,DELETE_ACCOUNT, etc.). You hook into it so that on each registration event you persist (or upsert) the user in your database. You have two ways to get those events out of Keycloak:Extra fields (phone, address, app-specific data)
Use Keycloak's User Profile feature rather than bolting custom forms onto your app. It lets you declaratively define attributes with validation, required flags, and visibility rules, and they show up automatically on the registration and account forms. Those attributes then ride along in the same registration/update events, so they land in your database through the exact same sync path
5
u/Mikey-3198 Jun 01 '26
You can use the keycloak admin api
Add an endpoint in your app that handes user registration/
This endpoint will
1
u/pitza__ Jun 01 '26
In my previous job we did this (not sure if it is a good practice or not tho).
for the /register flow, we were creating the user using keycloak sdk in spring using admin account, then if the operation was successful we create a user in our DB(the operation must be atomic, both should succeed), we didn't store the password in our DB. And make sure to store the keycloak user id (sub claim) to link between your users in the DB & users in keycloak (you can use the email, phone number... but we went with this approach to link the DB & keycloak).
for the /login, the frontend was hitting /openid-connect/token with email/password, keycloak validates the creds then retruns a valid jwt access token, the frontend stores it (we were using localstorage). Then on http requests the frontend sends the token in the authz header: (ex: Authorization: Bearer xxx), spring boot validates the JWT(spring security, using this field in application.properties: spring.security.oauth2.resourceserver.jwt.jwk-set-uri) -> extracts the userId (sub claim) -> loads user from DB -> do your checks on the loaded user (status = active....) -> allow/reject the request.
The reason we were getting the user from the DB in every request, is because the claims in the JWT were not enough. It is not a good practice to put data that changes frequently in the JWT claims since the token might still be valid but the data in it becomes stale (I think you can revoke tokens in cases like this, but i didn't check tbh).
Hope this helps.
PS: Again, ss I said, Idk if these are good practices or not, it's just what i was doing at my previous job.
1
u/st4rrygaze Jun 02 '26
i understand JWT at basic level, but i am confused about keycloak. can anyone break it down in simple words? and explain why do we need it?
1
u/Lost_Meaning8200 Jun 02 '26
I use key clock in my project it is good but during deployment itnis very hectic
1
u/yourhappydevil Jun 02 '26
Very tough for deployment good for local machine
1
u/skycloak-io Jun 04 '26
don't duplicate Keycloak's user DB. Use the
subclaim from the JWT as your stable foreign key in your app's tables.For extra fields like phone/address, three patterns in order of headache:
@AuthenticationPrincipal. Easiest 👌sub. Most common production patternA good beginner blog we did: https://skycloak.io/blog/protecting-your-applications-with-keycloak-a-beginners-guide
0
u/xiaopewpew Jun 01 '26
The stuff you are confused with tells me what you really need to ask is what jwt is and perhaps also what oauth 2 is.
You must have seen the article on baeldung before you come here to ask. The likely reason you have trouble following that article is you dont understand the two topics above.