Search code examples
springspring-bootspring-securityoauth-2.0spring-oauth2

How to store OAuth2 session into database and share it between Spring Boot servers


I want to create a solution based on that tutorial: https://www.baeldung.com/rest-api-spring-oauth2-angular

But it's not clear for me how several Spring Boot servers behind load balanced can share the same token session.

With session cookie the session data can be stored in MySQL and of server goes offline the session will be resumed by another Spring Boot server.

Can we have with OAuth2 the same setup with shared database so that the session data can be shared and switching of severs can be completely transparent for the end users?


Solution

  • Can we have with OAuth2 the same setup with shared database so that the session data can be shared and switching of severs can be completely transparent for the end users?

    Yes, You just have to define a JdbcTokenStore and use it instead of the default InMemoryStore to store your OAuth tokens. The JdbcTokenStore must use a DataSource which is pointing to a MySQL Database.

    Finally if you configure your Spring Boot apps to connect to the same MySQL Database to store Tokens by defining a DataSource you can get it working.

    This post could be a good starting point.