Spring boot Security + JWT Demo Example
Here I have created a maven project and configured JSON WEB TOKEN(JWT) with Spring Boot.
JWT Will Perform the below 2 operations:
1. Generate JWT Token — We Expose a POST API on /authenticate Endpoint. And Passing the correct username and password to generate a JSON Web Token(JWT).
2. Validate JWT Token — If a user tries to access GET API on /testuri Endpoint and passes The generated Token in the header as Authorization Bearer token. So It will allow access only if the request has a valid JSON Web Token(JWT)
JWT Token Generate with Valid credentials :
Make a POST request with URL http://localhost:8089/authenticate. The body should have a valid username and password. Here I used username: “ruhul” and password: “password”. Please make sure that the body should be in JSON format. If the username and password are valid then it will return a token and success Response with 200 Status code. An example is below :
Generate JWT Token
Validate JWT Token & Make Rest API call :
Try to access the URL http://localhost:8089/testuri using the above-generated token in the header as Bearer Token. If the Authorization Bearer token is valid it will return a valid response.
An example is below: Authenticate JWT Token
GitHub Link: https://github.com/ruhulmus/spring-boot-tutorial/tree/main/spring-boot-JWT
## For more tutorials, you can follow me on GitHub: https://github.com/ruhulmus