Example project for JWT authentication with Spring Boot and Spring Security. It can be used as an ad hoc template for the creation of a Spring Boot project that requires one or more authentication providers.
In the example we use the classic authentication provider based on username and password. There is also an example of a custom authentication provider (e.g. integration with external authentication systems via API).
- Install MySQL server
- Create a "jwtexample" database
- Create the example tables
CREATE TABLE `custom_user` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`email` VARCHAR(100) NOT NULL,
`name` VARCHAR(50) NOT NULL,
`password` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE INDEX `UQ_custom_user` (`email`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
CREATE TABLE `movie` (
`id` BIGINT(20) NOT NULL AUTO_INCREMENT,
`title` VARCHAR(100) NOT NULL,
`author` VARCHAR(100) NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB;
- Edit application.yml file (path src/main/resources) with your database details
- Import the project using your favourite IDE as a maven project
- Install lombok plugin into your IDE (see: https://www.baeldung.com/lombok-ide)
- Start JwtexampleApplication with your JDK
This project has been tested with these versions:
Spring Boot 2.3.4
Java version 11.0.5
Apache Maven 3.6.3
MySQL server 8.0.18