![71b84aeb157195d80407b9b7a711569b.png](https://img-blog.csdnimg.cn/img_convert/71b84aeb157195d80407b9b7a711569b.png)
通过前面的文章,我们已经实现了基于数据进行登录鉴权及基于注解的方式进行方法鉴权
第二十四章:整合SpringSecurity之最简登录及方法鉴权
第二十五章:整合SpringSecurity之基于数据库实现登录鉴权
第二十六章:整合SpringSecurity之前后端分离使用JSON格式交互
第二十七章:整合SpringSecurity之前后端分离使用Token实现登录鉴权
第二十八章:整合SpringSecurity之前后端分离使用JWT实现登录鉴权
注解方式的方法鉴权:
通过 @EnableGlobalMethodSecurity 注解来开启方法鉴权。
securedEnabled:开启 @Secured 注解
- 单个角色:@Secured(“ROLE_USER”)
- 多个角色任意一个:@Secured({“ROLE_USER”,“ROLE_ADMIN”})
prePostEnabled:开启 @PreAuthorize 及 @PostAuthorize 注解,分别适用于进入方法前后进行鉴权,支持表达式
- 允许所有访问:@PreAuthorize(“true”)
- 拒绝所有访问:@PreAuthorize(“false”)
- 单个角色:@PreAuthorize(“hasRole(‘ROLE_USER’)”)
- 多个角色与条件:@PreAuthorize(“hasRole(‘ROLE_USER’) AND hasRole(‘ROLE_ADMIN’)”)
- 多个角色或条件:@PreAuthorize(“hasRole(‘ROLE_USER’) OR hasRole(‘ROLE_ADMIN’)”)
jsr250Enabled:开启 JSR-250 相关注解
- 允许所有访问:@PermitAll
- 拒绝所有访问:@DenyAll
- 多个角色任意一个:@RolesAllowed({“ROLE_USER”, “ROLE_ADMIN”})
虽然非常灵活,但是毕竟是硬编码,不符合实际的生产需求,在项目中,每个角色的可访问权限必须是可调整的,一般情况下使用数据库进行持久化。
目标
整合 SpringSecurity 及 MybatisPlus 实现使用读取数据库数据进行方法鉴权
思路
使用配置类的 HttpSecurity 提供的 access 方法,通过扩展SpEL表达式,实现自定义鉴权
.access("@authService.canAccess(request, authentication)")
其中 authService 是 Spring 容器中的 Bean,canAccess 是其中的一个方法。
@Servicepublic class AuthService { public boolean canAccess(HttpServletRequest request, Authentication authentication) { //在这里编写校验代码… return true; }}
准备工作
创建用户表 user、角色表 role、用户角色关系表 user_role,资源表 resource,资源角色关系表 role_resource
![a7d2071c5d34068398e883da3fa78b14.png](https://img-blog.csdnimg.cn/img_convert/a7d2071c5d34068398e883da3fa78b14.png)
操作步骤
添加依赖
引入 Spring Boot Starter 父工程
![4e172d805c747c725248b0edaad1b0f7.png](https://img-blog.csdnimg.cn/img_convert/4e172d805c747c725248b0edaad1b0f7.png)
添加 springSecurity 及 mybatisPlus 的依赖,添加后的整体依赖如下
![aa2b1b74bb82e5dd9dfa77f307dfc429.png](https://img-blog.csdnimg.cn/img_convert/aa2b1b74bb82e5dd9dfa77f307dfc429.png)
配置
配置一下数据源
![454e50277d09b5f4b32bd78e2fc756c6.png](https://img-blog.csdnimg.cn/img_convert/454e50277d09b5f4b32bd78e2fc756c6.png)
编码
用户登录相关代码请参考 第二十五章:整合SpringSecurity之基于数据库实现登录鉴权,这里不再粘贴。
实体类
角色实体类 Role,实现权限接口 GrantedAuthority
![06a3d9dac2be320dcbf33f7b5484c706.png](https://img-blog.csdnimg.cn/img_convert/06a3d9dac2be320dcbf33f7b5484c706.png)
资源实体
![35f2856053df390178d13e14839e88e0.png](https://img-blog.csdnimg.cn/img_convert/35f2856053df390178d13e14839e88e0.png)
资源角色关系实体
![6a08ae34c831606a15cae8988e79631d.png](https://img-blog.csdnimg.cn/img_convert/6a08ae34c831606a15cae8988e79631d.png)
Repository 层
分别为三个实体类添加 Mapper
![1e799fa9edc971f0448be8ceee119cff.png](https://img-blog.csdnimg.cn/img_convert/1e799fa9edc971f0448be8ceee119cff.png)
实现自定义方法鉴权
![c611aba80036e2b3f08700c389a75cd8.png](https://img-blog.csdnimg.cn/img_convert/c611aba80036e2b3f08700c389a75cd8.png)
注册配置
不用再声明 @EnableGlobalMethodSecurity 注解,注册自定义鉴权方法 authService.canAccess。
![db6fa7694a257fd9bf01179d1f5f20e0.png](https://img-blog.csdnimg.cn/img_convert/db6fa7694a257fd9bf01179d1f5f20e0.png)
去掉原来的方法鉴权相关注解
![2d0eccd68d5591cdfa9b4c2ed65ea165.png](https://img-blog.csdnimg.cn/img_convert/2d0eccd68d5591cdfa9b4c2ed65ea165.png)
启动类
![bc556c59b4b58d6a45f1a17a7cb567cc.png](https://img-blog.csdnimg.cn/img_convert/bc556c59b4b58d6a45f1a17a7cb567cc.png)
验证结果
初始化数据
执行测试用例进行初始化数据
![b40e31f429fc45093970ed504229c892.png](https://img-blog.csdnimg.cn/img_convert/b40e31f429fc45093970ed504229c892.png)
校验
使用 admin 登录可以访问 /hello 及 /secure,使用 user 登录则只能访问 /hello
源码地址
本章源码 : https://gitee.com/gongm_24/spring-boot-tutorial.git
参考
249.Spring Boot+Spring Security:基于URL动态权限:扩展access()的SpEL表达式