How can I implement Single Sign On (SSO) multi tenancy functionality with Keycloak and Spring Boot OAuth2 clients?

Viewed 338

Problem

Implementing SSO login for multiple OAuth2 providers in Spring Boot.

Setup

Two Spring Boot web applications (App1 and App2) that are configured to be OAuth2 clients. Both will communicate with a Keycloak authorization server that has two realms.

Business Requirements

  • Implement Single Sign-On functionality (SSO).
  • Multi tenancy with shared user base.
  • Only one user can access one tenant at any time.

I have two spring boot applications which are OAuth2 clients running in docker containers. We are setting up multiple Keycloak realms that are configured for each tenant. From the Spring side of things, we include the auto configuration properties for two providers where each provider will be mapped to a different Keycloak realm. So the properties will look as follows:

  • spring.security.oauth2.client.provider.realm1......
  • spring.security.oauth2.client.provider.realm2......

Behavior

When a user logs into the first application (App1), Spring shows a generated html page. This page shows a list of each provider configured from the application.properties as an option to login to.

A user can select one and is redirected to the Keycloak login page with the realm that was mapped from Spring's provider properties. Then when successfully logged in, the user is redirected back as expected.

We use Spring Mongo session to store the session information and we also see in the Keycloak admin client the realm that shows the active session as well.

When trying to access the other application (App2), Spring does not detect the user or session and will show the same generated html page that shows the providers to select and login to.

When clicking on the same provider (realm), Spring will then find the session and will be redirected to the requested resource and all is well. This part is what I am trying to implement without asking for the provider first. The main reason is to enforce a business requirement where a user in a session can not access more than one realm at a time.

Attempted Solution

Provide a Spring Security login controller that will have a service layer to find the mongo session and then build the OAuth2 link Spring generates when you click a provider from the list.

However, I dont have the user yet. This also becomes a problem when opening a different tab as I dont believe I have any scope to the cookies that were created from the first application as well.

The only other thing I can think of is trying to get the client ip and store that in the session so I can find it later. However, when using nginx proxy configuration, this becomes a problem as I cant seem to get the actual ip and always seem to get the proxy ip instead even with the nginx headers I have seen from documentation.

Question

Is there anyway to find the session and redirect to the requested provider programmatically?

Note: I am currently aware of the keycloak starter dependencies that are available but I was trying to see if there is a more Spring oriented solution with its general OAuth2 client security configuration.

1 Answers

Front end solution

  1. User navigate to app1, app1 detects no user session (need for login), app1 redirects user to app2 with some query parameters indicating purpose of redirect.
  2. App2 receives redirect request and check if session exist (user logged in to app2). Now you can deal with it. redirect back to app1 or display some error, etc.
  3. User not logged in to app2 , app2 redirects back to app1 with indication "show providers"

Symmetrical behavior shall be implemented on app1 too.

Depending on security requirements query parameters can be encrypted to prevent manual url hacking.

If you need further protection Keycloak authentication can be extended with functionality to check your Session storage for already logged in users.

Related