Skip to content

Example spring boot project for spring security with jwt integration.

License

Notifications You must be signed in to change notification settings

zhouhao/spring-boot-jwt-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

38 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Spring Boot JWT Example

Project Build

This is a simple demo for spring boot with JWT integration, which supports user signup/login and blog creation/reading.

Note: this example project is inspired by Implementing JWT Authentication on Spring Boot APIs . Thanks for that nice blog post.

How to Run

Note: This is a standard spring boot project with Maven, so you can use maven command to run it.

Steps:

  1. Run docker-compose up to start mariadb (add -d if you want to start it as “detached” mode).
  2. Start spring boot project, either inside IDE, or with command line.
  3. Play with this sample service with fun!

Endpoint

User

  1. POST: /signup

It needs request payload as below, and it will return 200 OK http status if succeeded.

{
  "username": "nice-user-name",
  "password": "a-strong-password"
}
  1. POST: /login

It needs the same request payload format as "signup", and it will return both access token and refresh token if succeeded.

{
  "username": "your-nice-user-name",
  "password": "your-strong-password"
}
  1. POST: /refresh_token

It needs request payload as below, and it will return an access token if succeeded.

{
  "refresh_token": "a-long-refresh-token-string"
}
  1. DELETE: /refresh_tokens

With valid access token in the http header, you can purge all your stored refresh tokens in the database, in case you leak your refresh token. So after access token expires, the attacker cannot access your content anymore. (This is the reason why we should have a small TTL for access token.)

Blog

  1. POST: /blog

It needs request payload as below, and it will return 200 OK http status if succeeded.

{
  "title": "nice-blog-title",
  "content": "wonderful-blog-content"
}
  1. GET: /blog/{id}

Fetch a single blog by its id.

  1. GET: /blogs

Fetch all blogs belong to the request user.

About

Example spring boot project for spring security with jwt integration.

Topics

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages