Amazon Cognito User Authentication in Spring Boot REST

Authentication is a core component to the design of most enterprise applications. Amazon Cognito allows developers to easily implement authentication through identity pools and federated IDs via JSON web tokens, which can be used for multiple application backends. In this blog post we will explore how to integrate Amazon Cognito with Spring Boot REST framework in order to authenticate users from an external system using a custom user model created by AWS Amplify

The “aws cognito spring boot jwt” is a service that allows users to authenticate with Amazon Cognito. It uses JSON Web Tokens and Spring Boot REST.

Amazon Cognito User Authentication in Spring Boot REST

In the last article, we learnt how to use Amazon Cognito for user authentication in a Spring Boot application. We’ll build on our prior knowledge in this session by doing the following.

  • To handle /login HTTP POST requests, create a Controller of Rest.
  • To authenticate using Amazon Cognito User Pool, read the username and password from the request body.
  • In the HTTP response, get a JWT access token and a refresh token.

Configurations for Amazon Cognito

When we authenticate users programmatically using Amazon Cognito, there are several parameters that will be altered. We’ll take a look at each one individually. First, we no longer need the Domain that we previously utilized for Amazon Cognito’s sign-up and sign-in sites. We’ll leave this blank.

Amazon-Cognito-User-Authentication-in-Spring-Boot-REST

After that, we may uncheck the box that says “Generate client secret.”

1637687658_529_Amazon-Cognito-User-Authentication-in-Spring-Boot-REST

Then, while building an App client, we’ll tick Enable username password auth for Admin APIs in the Auth Flows Configuration.

1637687659_917_Amazon-Cognito-User-Authentication-in-Spring-Boot-REST

The remainder of the options may be left alone. We’ll now move on to our Spring Boot application.

 

Using Amazon Cognito to Integrate a Spring Boot Application

application.properties

Let’s take a closer look at our application now. properties.txt To get the aws-access-key and aws-access-secret, first go to the Services page and choose IAM. It’ll take us to the IAM Dashboard. We’ll now choose Manage Access Keys.

1637687661_172_Amazon-Cognito-User-Authentication-in-Spring-Boot-RESTThen we’ll select Get New Access Key to get the file with the aws-access-key and aws-access-secret in it. Our application will be updated. properties file in the appropriate location.

Amazon-Cognito-User-Authentication-in-Spring-Boot-REST

In App Clients, under General Settings, we will change the value of aws.cognito.clientId to App client id. Similarly, we’ll change the value of aws.cognito.userPoolID and region in General Settings to the Pool Id and region.

aws.access-key = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx access-secret = xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx aws.cognito.userPoolId= xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx spring.security.oauth2.resourceserver.jwt.issuer-uri=https://cognito-idp.region.amazonaws.com/Pool Id/issuer-uri=https://cognito-idp.region.amazonaws.com/Pool Id/issuer-uri=https://cognito-idp.region.amazonaws.com/Po

Rest Controller

We have a Post API in our Controller class that we will use to authenticate our user by giving username and password as payload, which will be validated by Amazon Cognito in our Service, which we will discuss later in the Service section.

package com.example.cognitointegrationinspringrestapplication.rest; import com.example.cognitointegrationinspringrestapplication.model.UserLoginRequestPayload; import com.example.cognitointegrationinspringrestapplication.model.UserLoginResponsePayload; import com.example.cognitointegrationinspringrestapplication.service.UsersService; import org.spring @Autowired UsersService usersService; @PostMapping(path = “/login”) public ResponseEntity UserController

Watch This Video-

Amazon Cognito is a cloud-based identity service that allows users to sign in with their existing credentials or create new ones. In this tutorial, we’ll show you how to configure Spring Boot REST to use Amazon Cognito authentication. Reference: spring boot with aws cognito.

Related Tags

  • aws cognito authentication java example
  • aws cognito authentication java example spring boot
  • aws cognito spring boot github
  • spring security with aws cognito
  • aws cognito verify token java