How to integrate Java Spring Boot application with AWS Cognito using OIDC?

karthik
3 min readSep 26, 2023

--

In this blog, I will explain how to integrate a Spring Boot web app with AWS Cognito. Spring Boot is one of the most widely used framework in the Java world and AWS Cognito is one of the most widely used Identity providers.

Use Case

Many organizations around the world build web applications using Spring framework. All these applications require some kind of authentication. AWS Cognito is a IDaaS service which supports OpenID Connect (OIDC) protocol. Spring Boot applications can be integrated with AWS Cognito for user authentication.

Architecture

  1. User enters the Spring Boot web app URL in the browser
  2. If the user has not already logged in i.e. if there is no active session, web app will redirect the user to Cognito /oauth2/authorize endpoint.
  3. Cognito will display the hosted UI login page .
  4. User enters the credentials (username / password).
  5. Cognito validates the credentials after the user clicks the login button.
  6. If the credentials are valid, Cognito will redirect to Spring Boot web app redirect_uri with an authorization code in the URL parameter.
  7. Spring Boot web app will call the /oauth2/token API to get the ID, Access and Refresh tokens.
  8. Cognito will return the tokens if the authorization code and other parameters are valid.
  9. Spring Boot will validate the tokens and create a session for the user.
  10. App will display the home page. App can have business logic based on the ID or Access tokens to show or hide features to the user.

How to configure a Spring Boot app to use Cognito as the Identity provider

Key points to note

  • I followed the instructions in this Spring blog to setup the Spring Boot app. Please note that some of the steps are outdated.
  • Please refer this official spring documentation to configure a OIDC provider.
  • Spring Boot provides out of the box support for OIDC integration. It takes care of URL redirection, token validations, creating sessions etc.
  • There are multiple ways to configure all the IdP URLs in application.yml file. The easiest way is to configure just the “spring.security.oauth2.client.provider.[providerId].issuer-uri”. Refer this documentation for Cognito issuer-uri. You can check the iss attribute in https://cognito-idp.Region.amazonaws.com/your user pool ID/.well-known/openid-configuration

If you want to integrate Java Spring Boot application with AzureAD using OIDC, please follow the below blog

If you want to integrate Java Spring Boot application with Okta using OIDC, please follow the below blog

Thanks for reading this article. Please subscribe to the below YouTube channel and follow me in medium to learn about security and IAM.

--

--

No responses yet