A reference proof-of-concept that leverages Spring-security to implement JWT based Authentication and Authorization.
- JwtAuthenticationFilter.java
- SecurityConfiguration.java
- ApiPathExclusion.java
- CORSFilter.java
- JwtUtility.java
- JwtConfigurationProperties.java
Any request to an endpoint that is specified to be authenticated will be intercepted by the JwtAuthenticationFilter which is configured in the SecurityConfiguration, Any APIs that need to be made public can be specified under the appropriate HTTP method in ApiPathExclusion.
Inside JwtAuthenticationFilter, the token from request header Authorization
is extracted and decrypted, the corresponding user in database is fetched and is loaded onto the SecurityContextHolder object which signifies the current logged-in user for that HTTP request. The user details are added as part of custom claims in generated JWT. [refer JwtUtility]
The secret key which is to be used for JWT generation and decryption, along with Access-token and Refresh-token validity can be specified in application.properties
file corresponding to JwtConfigurationProperties.java. Below is a sample .properties
file snippet that can be used to configure the required parameters.
# JWT Configuration
com.behl.cerberus.jwt.secret-key=093617ebfa4b9af9700db274ac204ffa34195494d97b9c26c23ad561de817926
com.behl.cerberus.jwt.access-token.validity=30
com.behl.cerberus.jwt.refresh-token.validity=5
Run the below given commands in the projects base directory to create an image and start a container from the given Dockerfile
docker build -t jwt-auth-flow-spring-security .
docker container run -d -p 8080:8080 jwt-auth-flow-spring-security