SpringSecurity 学习指南大全
开源 Spring Security 前后端分离 - 后端示例
SpringSecurity - 前后端分离简单实战 - 环境准备
看完上面的文章,我们再结合一次实战来更深入的了解 SpringSecurity 的运行规则,上面的文章我只挑选了最重要的进行翻译,也就是它的架构部分。SpringSecurity 的核心就是其过滤器,因为它就是根据过滤器来实现的,其它扩展功能都是添加新的过滤器来实现。
所以我们想玩转 SpringSecurity 进行安全管理,就是要去熟悉它的过滤器,然后才能自定义自己的过滤器,来进行认证和授权处理。
而且网上的博客很多教程已经落后,我们使用当前最新版的 5.7 来自定义我们 web 的安全管理。
最好的理解方法
边实战,边对比官方的架构,比如整体架构,认证架构和授权架构。只要理解了架构,以后对 SpringSecurity 的功能扩展,将更加的得心应手。而不是只能根据别人的配置来进行修改,一旦增加新的功能,就无从下手。古人云:授人以鱼不如授人以渔。
网上的文章大部分都是:授人以鱼,对其核心思想讲解的少。只教你配置方法。
简单实战,我们不会对其源码进行过多的深入讲解。
环境准备
我们使用当前流行的技术:
- JDK1.8:现在大部分企业流行版本
- Maven
- MySQL 5.7
- Redis
- Postman:懒得写前端,所以用Api测试工具,大家按自己喜欢的来就行,我这里使用 Postman
技术前提
掌握了常见的流行框架,比如SSM、SpringBoot
项目架构
我这里使用 SpringBoot 2.7,因为 2.7 的 Security 就是 5.7 版本,我们都知道前后端分离就是使用 token 技术,不知 token 的可以百度下,其实就是用来替换单机 sessionid 的。就一串字符串,没什么特别的。
网上常用的就是 Security + JWT,而我这里不选用 JWT ,因为 JWT 虽然把 token 交由客户端管理过期等等,我还是喜欢后端来处理,而且在安全方面,比如简单的加密,就是使用签名来进行前后端交互,所以如果使用了 Redis,就不推荐使用 JWT 了。
我这里会用 Redis 来管理 token 的有效期,和用户信息缓存,当然是简单实现。
项目创建
新建一个 Maven 项目,这个应该不需要多说吧。
然后引入依赖即可
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<!-- Spring Boot 父项目-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.0</version>
</parent>
<groupId>org.example</groupId>
<artifactId>bootDemo</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!--spring-Date-Redis-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<!-- Spring-Date-Redis连接池依赖 commons的pool2 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-pool2</artifactId>
</dependency>
<!-- mybatis -->
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.5.2</version>
</dependency>
<!-- mysql驱动包 -->
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<!-- Spring 安全框架 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<!-- lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- apache工具包 -->
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
</dependency>
<!-- Spring 监控-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<!-- Spring Boot 属性配置执行器 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
<!-- Spring Boot开发者工具 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
</project>
Security 的依赖引入对应的启动器即可
项目配置
简单配置即可
server:
port: 8888
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://localhost:3306/test?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8
username: root
password: 123456
redis:
host: 127.0.0.1
port: 6379
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
# 热部署端口避免和其它应用重复
devtools:
livereload:
port: 35730
# 日志级别
debug: false
mybatis-plus: # mybatisPlus的日志输出
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
数据库配置
数据库配置就一张简单的 user 表,不是一个严格的 RBAC 模式,后续可自行添加。
表不是唯一的,你可以自己创建用户表,只是用于查询用户信息。以下只是参考
CREATE TABLE `app_system_user` (
`id` varchar(50) NOT NULL,
`username` varchar(50) NOT NULL,
`password` varchar(100) NOT NULL,
`nickname` varchar(50) DEFAULT NULL,
`email` varchar(50) DEFAULT NULL,
`enabled` tinyint(1) DEFAULT NULL,
`is_delete` tinyint(1) DEFAULT NULL,
`last_login_date` datetime DEFAULT NULL,
`create_date` datetime NOT NULL,
`create_user` varchar(50) NOT NULL,
`update_date` datetime DEFAULT NULL,
`update_user` varchar(50) DEFAULT NULL,
PRIMARY KEY (`id`) USING BTREE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 ROW_FORMAT=COMPACT;
insert into `app_system_user`(`id`,`username`,`password`,`nickname`,`email`,`enabled`,`is_delete`,`last_login_date`,`create_date`,`create_user`,`update_date`,`update_user`) values
('4028368176b7e2210176b7e22acd0000','kfry','$2a$10$yIHA8hzMZ5L0tl2Dsn44weHXqESddNIuD2Y7LP4AeBP5X3ADtEIgi','开发人员',NULL,0,0,NULL,'2020-12-31 08:20:21','system_#','2021-07-29 08:53:11','4028368176b7e2210176b7e22acd0000'),
('402836817af18fed017af19047200000','admin','$2a$10$nhI0EGVSoEFLe028nwsG6esEtfGC4MTBKU2fLbkJEIw4OiTvTSubK','admin','123',1,0,NULL,'2021-07-29 09:20:02','4028368176b7e2210176b7e22acd0000','2021-07-29 11:21:13','4028368176b7e2210176b7e22acd0000');
字段解释:
- username:用户名
- password:密码
- nickname:昵称
- email:邮箱
- enabled:是否启用
- is_delete:是否删除
- last_login_date:最后登录时间
非常简单的 user 表,而且在简单实现中,很多字段都用不到
默认密码为123
4835

被折叠的 条评论
为什么被折叠?



