preface

Stateless authentication based on JWT(Json Web Token) is becoming more and more popular. Together with SpringSecurity and SpringBoot, the authentication function can be elegant.

About SpringBoot + the interpretation of the Security, can refer to my previous article: www.toutiao.com/i6704647082…

In order to reduce the workload of repeated wheel building and facilitate everyone’s copying and reference, I put a complete project developed by SpringBoot+JWT+SpringSecurity+Mybatis-Plus on my Github, which is convenient for myself and others.

Original statement

This article was published on Happyjava. Happy nuggets address: juejin.cn/user/398428… , Happy’s personal blog :(blog.happyjava.cn)[blog.happyjava.cn] Welcome to reprint, but keep this statement.

The source code for

Github address, refer to the end of the article.

The functionality implemented by the project

1. Integrated with convenient Mybatis- Plus

2. Integrated JWT

3. Integrated Spring Security

A simple demo

Configure the path for blocking and releasing

Unlogged request

Will be intercepted and returned to 401. The content returned can be customized by the user

The login

After successful login, access the required interface

The data was successfully requested.

Get the currently logged in user by annotation

Project deployment

Database preparation

Create database test, create table as follows:

CREATE TABLE `admin` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `username` varchar(255) NOT NULL,
  `password` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
Copy the code

After the table is created, insert the username and password itself.

Modify the configuration file applicatoin.properties

Correct configurations such as data and passwords

spring.datasource.url=jdbc:mysql://localhost:3306/test? useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai spring.datasource.username=root Spring. The datasource. Password = 123456 spring. The datasource. The driver - class - name = com. Mysql. Cj. JDBC. Driver # need handwritten mapper open this configuration Mybatis - plus. Mapper - locations = classpath: mappers / *. XML # JWT key JWT. Secret. The key = # happyjava1234214214asfasfasfasdf JWT expiration time  jwt.token.expired=360000Copy the code

You can also customize the JWT encryption key and token expiration time

Start the project

Start the project and test it through the interface

The source address

Github.com/Happy4Java/…

Pay attention to the information of public number

Search the official account [Happyjava], reply [ebook] and , you can get a large number of high-quality e-books and big data, Kafka, nginx, MySQL and other video materials