This is Spring Boot REST API JSON Web Token (JWT) demo. In order to have a JWT token and use for each request, we need to get authenticate first (login).
- JDK 8
mvnw clean package
mvnw clean test
From maven:
mvnw spring-boot:run
or, after mvn clean package
, execute the file from /target
folder:
java -jar jwtauth-0.0.1-SNAPSHOT.jar
Web REST API can be access from port: 8080
.
Use curl
:
Login:
# username: foo, password: bar
curl -i -d "{\"username\": \"foo\",\"password\": \"bar\"}" -H "Content-Type: application/json" -X POST http://localhost:8080/login
Then you will get the result, attach this response Authorization
value with each request:
# request example
curl -H "Authorization: BearereyJhbGciOiJIUzUxMiJ9.eyJqdGkiOiJjYTBkZWYwOGVmZGU4ZGI2NzViZTY2NzYxOGM2ZDEwMSIsInN1YiI6ImZvbyIsImlhdCI6MTUzNzEwMjg5NywiZXhwIjoxNTM4NDE2ODk3LCJhdXRob3JpdGllcyI6W3siYXV0aG9yaXR5IjoiUk9MRV9ERUZBVUxUIn1dfQ.fqDnvfcW-HU6QVzeFy2fg04WhlHEg4vVbZc5qBBPrWOSilriZHYmEuUssf28suINVZRhEUPc33vqw5UADxKe8w" http://localhost:8080/echo/maikel
# will return: maikel
Enjoy! ❤️