Skip to content

warumono-for-develop/spring-boot-security-jwt-tutorial

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot with Security and JWT Tutorial

An tutorial application using Spring Boot as REST API with Security and JWT back-end.

More details about the codes, please read the online Spring Boot.

Requirements

Running in

Optional

  • YAML
  • Logback

Dependencies

Latest Update

  • 1.0 (Apr 20, 2017)

How to Run

  • Clone this Git repository
  • Build the project gradle build
  • Run the application ./gradlew bootRun
$ ./gradle bootRun

Test accounts

Reference file data.sql

ADMIN

STAFF

USER

API

Configuration

By default Spring Boot applications run on port 9090. But may vary depending on what ports are in use on your machine (check the terminal after entering the ./gradlew bootRun command). If you require to change which port the application runs on by default, add the following to:

application.yml

server:
    port: 9090 # --> change other port via. 8080
  • Request a token

Request

command

curl -X POST -H "X-Requested-With: XMLHttpRequest" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{"username": "<username>", "password": "<password>"}' "http://localhost:9090/api/auth/login"

command sample

curl -X POST -H "X-Requested-With: XMLHttpRequest" -H "Content-Type: application/json" -H "Cache-Control: no-cache" -d '{"username": "[email protected]", "password": "test1234"}' "http://localhost:9090/api/auth/login"

Response

{"token":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzdGFmZkBtZS5jb20iLCJzY29wZXMiOlsiUk9MRV9TVEFGRiIsIlJPTEVfVVNFUiJdLCJpc3MiOiJzcHJpbmctYm9vdC1zZWN1cml0eS1qd3QtdHV0b3JpYWwtaXNzdXJlciIsImlhdCI6MTQ5MjU4NjcxMCwiZXhwIjoxNDkyNTg3MDEwfQ.rTNoYxEmdhG7MH6O-xK5rLpCkyzCloHxMuLMtIURVCmF-KGlbVgnC3gQ9gh4dzR2P9JRj2HT9523R1sasn2IGg","refreshToken":"eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzdGFmZkBtZS5jb20iLCJzY29wZXMiOlsiUk9MRV9SRUZSRVNIX1RPS0VOIl0sImlzcyI6InNwcmluZy1ib290LXNlY3VyaXR5LWp3dC10dXRvcmlhbC1pc3N1cmVyIiwiaWF0IjoxNDkyNTg2NzEwLCJleHAiOjE0OTI1ODcwMTAsImp0aSI6ImM4OTU3ZmNhLTA3YjgtNDYzYi1iMDc5LWIwZDBlMGQyYWNlYyJ9.ZstM2QsK_3NmYsiXSiSePHO_ctPPHmaZX-Jxlqj8nFCT-Jy6xIVpe9BFvOahAZK1ajza8huIwNug_-bnIXw2_g"}
  • Request a user

Request

command

curl -X GET -H "X-Authorization: Bearer <token>" -H "Cache-Control: no-cache" "http://localhost:9090/api/profile/me"

command sample

curl -X GET -H "X-Authorization: Bearer eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJzdGFmZkBtZS5jb20iLCJzY29wZXMiOlsiUk9MRV9TVEFGRiIsIlJPTEVfVVNFUiJdLCJpc3MiOiJzcHJpbmctYm9vdC1zZWN1cml0eS1qd3QtdHV0b3JpYWwtaXNzdXJlciIsImlhdCI6MTQ5MjU4NjcxMCwiZXhwIjoxNDkyNTg3MDEwfQ.rTNoYxEmdhG7MH6O-xK5rLpCkyzCloHxMuLMtIURVCmF-KGlbVgnC3gQ9gh4dzR2P9JRj2HT9523R1sasn2IGg" -H "Cache-Control: no-cache" "http://localhost:9090/api/profile/me"

Response

{"username":"[email protected]","authorities":[{"authority":"ROLE_STAFF"},{"authority":"ROLE_USER"}]}

Author

warumono - [email protected]

License

spring-boot-security-jwt-tutorial is available under the Apache license. See the LICENSE file for more info.

Releases

No releases published

Packages

No packages published

Languages