接上文

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";
}
}

最新文章

  1. java.util.List接口的方法subList()的使用注意事项
  2. .NET跨平台:在mac命令行下用vim手写ASP.NET 5 MVC程序
  3. Linux内核
  4. JeeSite是基于多个优秀的开源项目,高度整合封装而成的高效,高性能,强安全性的 开源 Java EE快速开发平台
  5. {$ecs_css_path}
  6. Windows 8.1 (64bit) 下搭建 Scrapy 0.22 环境
  7. eclipse修改SVN下载的项目“>”变成“*”
  8. centos7 搭建 docker 环境
  9. 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​ 测试
  10. 【Linux命令】数据库mysql配置命令
  11. JavaScript基础知识----六道有趣的Js基础题以及解答
  12. PHP - 验证用户名
  13. ASP.NET-FineUI开发
  14. 剖析touch事件在View中的传递
  15. 【题解】Hanoi塔问题
  16. [原创] JAVA 递归线程池测试 ExecutorService / ForkJoinPool
  17. h5 调起ios数字键盘的坑,限制特殊字符输入方案
  18. dp Surf
  19. 07 grep命令与正则表达式
  20. 在Windows下编译mongo-c-driver 1.3.x

热门文章

  1. 软件工程homework-002
  2. Go值类型和引用类型+作用域+空白标识符+常量
  3. ArcGIS温泉数据聚类分析、核密度分析
  4. CentOS7.5环境下Docker环境搭建
  5. JavaWeb 07_创建web项目连接MySQL实现注册登录功能
  6. 定位一个oom问题
  7. 阿里一面,说说你了解zookeeper的应用场景有哪些?
  8. 【基础】java环境搭建及配置--->【关注微信公众号:三叔测试笔记,及时获取干货】
  9. http多路复用?
  10. jQuery--选择器案例实战