I'm trying to implement Spring Security in a resource server with "Cognito Oauth2", however I don't seem to find too much info. about it (or if It's even possible to do so).
My nearest approach was using "Nimbus+JOSE" to check the validity of the "Access Token" with the "JWKS" and give permissions to acccess the resource. (Similar to the example they give with the "API Gateway Resource Protection Implementation" found here: https://aws.amazon.com/es/blogs/mobile/integrating-amazon-cognito-user-pools-with-api-gateway/)
We can create Spring Boot resource server, keeping Cognito as Identity Provider.
Dependency:
Spring Security Configuration:
Cognito Access Token Converter:
Here we are converting the Cognito claims to Spring Security consumable format.
application.properties
For complete article, refer: Integrate Spring Boot Resource Server with Cognito Identity Provider
A great starting point for Oauth2 using the latest Sprint Boot 2.x / Sprint Security 5.x can be found here : https://spring.io/blog/2018/03/06/using-spring-security-5-to-integrate-with-oauth-2-secured-services-such-as-facebook-and-github
It uses Facebook / Github as an example but you can apply it to AWS Cognito also.
This is by far the easiest way to setup a secure REST backend with Spring Security / Cognito OAuth2. Your backend will be secured via Spring Security, and AWS Cognito will be used as the identity provider.
You can setup a vanilla spring boot app using the spring security starter as outlined in the article using the following dependencies :
and provide your cognito configuration (client registration + provider definition) like this :
As far as Cognito is concerned you need to have a user pool / identity pool with a couple of users and a valid app client ( =
client-id
in spring config) in cognito withclient-secret
in the spring config)redirect-uri-template
in the spring config)jwk-set-uri
in the spring config)With everything in place, the Spring Boot app will automatically generate a login url
Redirecting you to the cognito login page where you can enter your cognito credentials
And after a successful authentication you'll be able to do a secure REST call
With a REST controller like this :