Spring Boot Spring Security JWT Authentication [GitHub]

In this post, we will share the project source code of the "Spring Boot Spring Security JWT Authentication" example on GitHub.

1. GitHub Repository

GitHub repository name: spring-boot-spring-security-jwt-authentication

GitHub repository link: https://github.com/sourcecodeexamples/spring-boot-spring-security-jwt-authentication

About GitHub repository: Spring Boot + Security: Token-Based Authentication example with JWT, Authorization, Spring Data & MySQL

2. Step by Step Tutorial

You can develop this example step by step using this tutorial: Spring Boot Refresh Token with JWT example

3. Github Repository README ( Notes)

Spring Boot JWT Authentication example with Spring Security & Spring Data JPA

User Registration, User Login, and Authorization process.

The diagram shows the flow of how we implement the User Registration, User Login, and Authorization process.

spring-boot-jwt-authentication-spring-security-flow

Spring Boot Server Architecture with Spring Security

You can have an overview of our Spring Boot Server with the diagram below:

spring-boot-jwt-authentication-spring-security-architecture

For more detail, please visit:

Secure Spring Boot App with Spring Security & JWT Authentication

For MongoDB 

Refresh Token

spring-boot-refresh-token-jwt-example-flow

For instruction: Spring Boot Refresh Token with JWT example

Dependency

– If you want to use PostgreSQL:

<dependency>
  <groupId>org.postgresql</groupId>
  <artifactId>postgresql</artifactId>
  <scope>runtime</scope>
</dependency>

– or MySQL:

<dependency>
  <groupId>mysql</groupId>
  <artifactId>mysql-connector-java</artifactId>
  <scope>runtime</scope>
</dependency>

Configure Spring Datasource, JPA, App properties

Open src/main/resources/application.properties

  • For PostgreSQL:
spring.datasource.url= jdbc:postgresql://localhost:5432/testdb
spring.datasource.username= postgres
spring.datasource.password= 123

spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation= true
spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.PostgreSQLDialect

# Hibernate ddl auto (create, create-drop, validate, update)
spring.jpa.hibernate.ddl-auto= update

# App Properties
bezkoder.app.jwtSecret= bezKoderSecretKey
bezkoder.app.jwtExpirationMs= 86400000
  • For MySQL
spring.datasource.url= jdbc:mysql://localhost:3306/testdb?useSSL=false
spring.datasource.username= root
spring.datasource.password= 123456

spring.jpa.properties.hibernate.dialect= org.hibernate.dialect.MySQL5InnoDBDialect
spring.jpa.hibernate.ddl-auto= update

# App Properties
bezkoder.app.jwtSecret= bezKoderSecretKey
bezkoder.app.jwtExpirationMs= 86400000

Run Spring Boot application

mvn spring-boot:run

Run following SQL insert statements

INSERT INTO roles(name) VALUES('ROLE_USER');
INSERT INTO roles(name) VALUES('ROLE_MODERATOR');
INSERT INTO roles(name) VALUES('ROLE_ADMIN');

4. Credits/References


Comments