I need to implement Authentication & Authorization using spring boot oauth2 with keycloak as a provider. I also need to support muti-tenancy. I tried example with authentication using spring-boot-starter-auth2-client to authenticate, but not able to add multi-tenancy.
When I used spring-boot-starter-auth2-client, I need to configure hardcode keycloak urls(specific to one tenant) in properties and not able to support multi-tenancy.
I also analyze spring-boot-starter-auth2-resouce-server, but not clear. I understand that resouce server use for validation of token and expiry.
Note: I don't want to use keycloak adapter library which is provided by keycloak.
Could you please help me -
- Where need to use spring-boot-starter-oauth2-client and spring-boot-starter-oauth2-resouce-server?
- Is spring-boot-starter-oauth2-resouce-server also use to authentication?
- How to authenticat user using spring-boot-starter-oauth2-client and pass to spring-boot-starter-oauth2-resouce-server for authorization.
- How to implement multi-tenacy e.g. take tenant id from url and redirect user to tenant specific keycloak login page.
- I tried some example but won't succeed, working example will be helpful with - Spring Webflux + spring-boot-starter-oauth2-client+ spring-boot-starter-oauth2-resouce-server + multi-tenancy + keycloak as a provider.
Thanks & Regards, Pravin Nawale
tried some example found on internet, but didn't work.
This question should not be answered because:
But as it seems to be a first question... (break it down next time, give more details and edit your question when you get comments asking precisions)
0. Usefull Resource
I maintain up to date samples and tutorials covering most OAuth2 use-cases with Spring for both reactive applications and servlets. Start with tutorials main README if you are new to OAuth2 with Spring.
1. Where need to use
spring-boot-starter-oauth2-client
andspring-boot-starter-oauth2-resouce-server
?This one is important to start with as I suspect you lack OAuth2 background, specifically regarding involved parties and how it is implemented with spring-security:
spring-boot-starter-oauth2-client
is to be used with OAuth2 clients:oauth2Login
(@Controllers
with methods returning template names)WebClient
,@FeignClient
,RestTemplate
spring-boot-starter-oauth2-resouce-server
is to be used with resource-servers: apps serving REST APIs (@RestController
or@Controller
with@ResponseBody
)Now, if your app has controllers for both the resources and the UI to manipulate it (with Thymeleaf or any other server-side rendering engine), then define two different security filter-chains: one for each, ordered, and with
securityMatcher
in the first in order to limit the routes it applies to (the second being used as fallback for unmatched routes). Sample in this answer (the sample is for servlet, but it's the exact same principles): Use Keycloak Spring Adapter with Spring Boot 32. Is
spring-boot-starter-oauth2-resouce-server
also use to authentication?OAuth2 requests should be authorized with an
Authorization
header containing aBearer
access-token.The client is responsible for acquiring such an access-token from the authorization-server before sending requests to resource-server.
Your question is not quite clear but here are a few statements which could answer:
oauth2Login
in resource-server filter-chain. Again, this is client business3. How to authenticat user using
spring-boot-starter-oauth2-client
and pass tospring-boot-starter-oauth2-resouce-server
for authorization.This question is not focused enough to get a single answer: what kind of client? what kind of request? context?
I see three main cases here:
oauth2Login
and refer to its documentation to overrides defaults and implement your authorization-server selection logicspring-cloud-gateway
withtokenRelay
filter) and refer to its doc for implementing your logic in itIf that can be of any help, I have:
4. How to implement multi-tenacy e.g. take tenant id from url and redirect user to tenant specific keycloak login page
Note
One of key principles of OAuth2 is that identities (tokens) are emitted (issued) by trusted 3rd parties (authorization-servers) => you must configure the list of issuers your resource-servers can trust (and clients can fetch tokens from). This list is static (loaded with conf at startup). The only reasonable trick for "dynamic" multi-tenancy is configuring an authentication manager resolver for a given host and dynamically creating authentication manager for new realms on this host. There is a tutorial covering that case among those linked at point 0.
Accept identities from various issuers on the resource-server
This is done by overriding the default
ReactiveAuthenticationManagerResolver<ServerWebExchange>
in yourSecurityWebFilterChain
configuration:http.oauth2ResourceServer().authenticationManagerResolver(authenticationManagerResolver)
I provide with thin wrappers around
spring-boot-starter-oauth2-resource-server
which support "static" multi-tenancy just by defining properties. Complete sample there:Instead of
spring-boot-starter-oauth2-resource-server
(which is a transient dependency):Instead of all your resource-server Java conf (unless you want access control from configuration and not with method-security, in which case, you'd have to define an
AuthorizeExchangeSpecPostProcessor
bean here). Of course, you'll have to add here a client filter-chain with a restrictivesecurityMatcher
if you also serve UI client withoauth2Login
:Instead of
spring.security.oauth2.resourceserver
properties:If you don't want to use "my" wrappers, just copy from the source, it is open.
Redirect the user to the right authorization-server from client UI
As explained at point 3., this depends on the kind of client, used framework and if BFF pattern is applied or not
5. I tried some example but won't succeed, working example will be helpful with - Spring Webflux +
spring-boot-starter-oauth2-client
+spring-boot-starter-oauth2-resouce-server
+ multi-tenancy + keycloak as a providerWith all the elements above and linked resources, you should have enough to find your own path