springboot+springsecurity+mybatis plus注解实现对方法的权限处理
2024-10-18 06:35:52
文章目录
接上文
springboot+springsecurity+mybatis plus之用户授权
一、@Secured
需要在类上开启该注解 @EnableGlobalMethodSecurity(securedEnabled = true)
@RestController
@RequestMapping("/test")
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true)
public class SecurityController {
@GetMapping("/add")
@Secured({"ROLE_user","ROLE_admin"})
public String add() {
return "add merchandise";
}
}
该角色没有ROLE_user或者ROLE_admin权限,所以无法访问该方法
二、@PreAuthority
实现该注解需要在类上加入
@EnableGlobalMethodSecurity( prePostEnabled = true)
@RestController
@RequestMapping("/test")
@EnableGlobalMethodSecurity( prePostEnabled = true)
public class SecurityController {
@GetMapping("/add")
@PreAuthorize("hasAuthority('admin')")
public String add() {
return "add";
}
}
由于有该admin权限,所以可以猜到结果为add
三、@PostAuthorize
同样需要在类上加入
@EnableGlobalMethodSecurity(prePostEnabled = true)
这个注解会在方法执行之后进行权限判断
@RestController
@RequestMapping("/test")
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityController {
@GetMapping("/update")
@PostAuthorize("hasAnyAuthority('user')")
public String update() {
System.out.println("update方法已经执行!");
return "update";
}
}
最新文章
- java.util.List接口的方法subList()的使用注意事项
- .NET跨平台:在mac命令行下用vim手写ASP.NET 5 MVC程序
- Linux内核
- JeeSite是基于多个优秀的开源项目,高度整合封装而成的高效,高性能,强安全性的 开源 Java EE快速开发平台
- {$ecs_css_path}
- Windows 8.1 (64bit) 下搭建 Scrapy 0.22 环境
- eclipse修改SVN下载的项目“>;”变成“*”
- centos7 搭建 docker 环境
- M​i​c​r​o​s​o​f​t​ ​w​e​b​ ​a​p​p​l​i​c​a​t​i​o​n​ ​s​t​r​e​s​s​ ​t​o​o​l​ 测试
- 【Linux命令】数据库mysql配置命令
- JavaScript基础知识----六道有趣的Js基础题以及解答
- PHP - 验证用户名
- ASP.NET-FineUI开发
- 剖析touch事件在View中的传递
- 【题解】Hanoi塔问题
- [原创] JAVA 递归线程池测试 ExecutorService / ForkJoinPool
- h5 调起ios数字键盘的坑,限制特殊字符输入方案
- dp Surf
- 07 grep命令与正则表达式
- 在Windows下编译mongo-c-driver 1.3.x