Skip to content

amnolan/spring-boot-spring-security-jwt-authentication-with-gradle

 
 

Repository files navigation

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

Use

Once you clone this project, from the root folder (where you can see the gradlew binary), do the following:

If you don't have docker-compose (which should be included with docker), go download and install it now for your OS .

First copy the docker-compose.yml into any directory outside the project. I had issues with running it from inside the project folder while the IDE was running. Next via terminal cd into the folder where docker-compose.yml is and run the command. If you're running Windows, just exclude the ./ from the commands:

# from the folder where you put your docker-compose.yml create your database container
./docker-compose up

# in a NEW terminal window - list your containers
./docker ps

# find the container name or id and then do the command, to get into bash:
./docker exec -it <container name or id> /bin/bash

# now we have to expose the user to be usable by your application
./mysql -uroot -p

# enter password as "password" and hit enter
Enter password:password

# run sql statements on your user
ALTER USER 'user'@'%' IDENTIFIED BY 'password';
FLUSH PRIVILEGES;

# from the root folder of the project build and start your application
./gradlew clean build
./gradlew bootRun

Just to see if it's working try to run a curl request, or postman, whatever you normally use, copy and paste this request in and you should get a response that the user was created:

curl --location --request POST 'http://localhost:8080/api/auth/signup/' \
     --header 'Content-Type: application/json' \
     --data-raw '{
        "email": "user@example.com",
        "username": "user_admin",
        "role": ["admin"],
        "password": "password"
     }'

Original readme content is below, note, you do not need to run the insert statements for the role table, as I included an init script already which runs with ApplicationRunner.

Also, the docker-compose mysql setup already matches the configuration in the application.properties, so you won't need to change that.


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

User Registration, User Login and Authorization process.

The diagram shows flow of how we implement 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

Fullstack Authentication

Spring Boot + Vue.js JWT Authentication

Spring Boot + Angular 8 JWT Authentication

Spring Boot + Angular 10 JWT Authentication

Spring Boot + Angular 11 JWT Authentication

Spring Boot + Angular 12 JWT Authentication

Spring Boot + React JWT Authentication

Fullstack CRUD App

Vue.js + Spring Boot + H2 Embedded database example

Vue.js + Spring Boot + MySQL example

Vue.js + Spring Boot + PostgreSQL example

Angular 8 + Spring Boot + Embedded database example

Angular 8 + Spring Boot + MySQL example

Angular 8 + Spring Boot + PostgreSQL example

Angular 10 + Spring Boot + MySQL example

Angular 10 + Spring Boot + PostgreSQL example

Angular 11 + Spring Boot + MySQL example

Angular 11 + Spring Boot + PostgreSQL example

Angular 12 + Spring Boot + Embedded database example

Angular 12 + Spring Boot + MySQL example

Angular 12 + Spring Boot + PostgreSQL example

Angular 13 + Spring Boot + H2 Embedded Database example

Angular 13 + Spring Boot + MySQL example

Angular 13 + Spring Boot + PostgreSQL example

React + Spring Boot + MySQL example

React + Spring Boot + PostgreSQL example

React + Spring Boot + MongoDB example

Run both Back-end & Front-end in one place:

Integrate Angular with Spring Boot Rest API

Integrate React.js with Spring Boot Rest API

Integrate Vue.js with Spring Boot Rest API

More Practice:

Spring Boot File upload example with Multipart File

Exception handling: @RestControllerAdvice example in Spring Boot

Spring Boot Repository Unit Test with @DataJpaTest

Deployment:

Deploy Spring Boot App on AWS – Elastic Beanstalk

Docker Compose Spring Boot and MySQL 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');

About

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

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Java 100.0%