文章目录

  • 1. SpringSecurity注解说明
    • 1.1 @Secured
    • 1.2 @PreAuthorize
    • 1.3 @PostAuthorize
    • 1.4 @PostFilter
    • 1.5 @PreFilter
  • 2. 权限表达式

1. SpringSecurity注解说明

1.1 @Secured

判断是否具有角色,注意这里匹配的字符串需要添加前缀“ROLE_”

  1. 先要开启注解功能
@EnableGlobalMethodSecurity(securedEnabled = true)
  1. 在控制器方法上添加注解
    @Secured({"ROLE_read"})@RequestMapping(value = "/testSecured")@ResponseBodypublic String testSecured() {return "testSecured";}

1.2 @PreAuthorize

适合进入方法前的权限验证,可以将登录用户的角色/权限参数传到方法中。
·

  1. 先要开启注解
@EnableGlobalMethodSecurity(prePostEnabled = true)
  1. 使用
//    @PreAuthorize("hasRole('ROLE_read1')")@PreAuthorize("hasAnyAuthority('write')")@RequestMapping(value = "/testPreAuthorize")@ResponseBodypublic String preAuthorize() {return "preAuthorize";}

1.3 @PostAuthorize

在方法执行后再进行权限校验,适合验证带有返回值的权限

  1. 开启注解功能
@EnableGlobalMethodSecurity(prePostEnabled = true)
  1. 使用
    @PostAuthorize("hasAnyAuthority('write')")@RequestMapping(value = "/testPostAuthorize")@ResponseBodypublic String postAuthorize() {System.out.println("方法进入了。。。。");return "preAuthorize";}

1.4 @PostFilter

权限验证之后对数据进行过滤

    @PostAuthorize("hasAnyAuthority('write')")@PostFilter("filterObject.username == 'admin1'")@RequestMapping(value = "/testPostAuthorize")@ResponseBodypublic List<UserPO> postAuthorize() {List<UserPO> userList = new ArrayList<>();UserPO user1 = new UserPO();UserPO user2 = new UserPO();user1.setId(1);user1.setUsername("admin1");user1.setPassword("12345");user2.setId(2);user2.setUsername("admin2");user2.setPassword("ancgsas");userList.add(user1);userList.add(user2);System.out.println(JSONObject.toJSONString(userList));return userList;}

1.5 @PreFilter

进入控制器之前对数据进行过滤

    @RequestMapping("/getTestPreFilter")@PreAuthorize("hasRole('ROLE_管理员')")@PreFilter(value = "filterObject.id%2==0")@ResponseBodypublic List<UserPO> getTestPreFilter(@RequestBody List<UserPO> list) {list.forEach(t -> {System.out.println(t.getId() + "\t" + t.getUsername());});return list;}

2. 权限表达式

[todo]

SpringSecurity(二): @Secured、@PreAuthorize、@PostAuthorize、@PostFilter、@PreFilter注解说明相关推荐

  1. Spring Security 4 使用@PreAuthorize,@PostAuthorize, @Secured, EL实现方法安全

    原文地址: http://blog.csdn.net/w605283073/article/details/51327182  本文探讨spring Security 4 基于@PreAuthoriz ...

  2. Spring Security 4 Method security using @PreAuthorize,@PostAuthorize, @Secured, EL--转

    原文地址:http://websystique.com/spring-security/spring-security-4-method-security-using-preauthorize-pos ...

  3. Security注解:@PreAuthorize,@PostAuthorize, @Secured, EL实现方法安全

    说明 (1)JDK版本:1.8 (2)Spring Boot 2.0.6 (3)Spring Security 5.0.9 (4)Spring Data JPA 2.0.11.RELEASE (5)h ...

  4. SpringSecurity 权限控制之开启动态权限注解支持

    开启授权的注解支持 这里给大家演示三类注解,但实际开发中,用一类即可! <!-- 开启权限控制注解支持 jsr250-annotations="enabled" 表示支持js ...

  5. SpringBoot集成SpringSecurity(二) 个性化登录配置(remember-me mongodb)

    前言 本文件所记录的是使用SpringSecurity实现remember me功能,有兴趣的朋友可以继续阅读,有何不足之处还请各位指出(本文未对用户 -  角色 - 权限三者的关系进行详细介绍详情见 ...

  6. java web学习总结(二十一) -------------------模拟Servlet3.0使用注解的方式配置Servlet...

    一.Servlet的传统配置方式 在JavaWeb开发中, 每次编写一个Servlet都需要在web.xml文件中进行配置,如下所示: 1 <servlet> 2 <servlet- ...

  7. 二、Java框架之Spring注解开发

    文章目录 1. IOC/DI注解开发 1.1 Component注解 @Component @Controller @Service @Repository 1.2 纯注解开发模式 1.3 注解开发b ...

  8. SpringSecurity(二十)---OAuth2:实现资源服务器(上)资源服务器搭建以及直接调用授权服务器模式

    一. 前言 本章将讨论如何使用Spring Security实现一个资源服务器,资源服务器是管理用户资源的组件.另外,学习本章有个前提,需要先把前面搭建授权服务器的相关文章先给阅读,否则可能后面出现的 ...

  9. (二)TestNG测试框架之注解及属性概览

    前言 TestNG提供了诸多注解,允许开发/测试人员灵活地组织强大的测试用例. 注解概览 注解/属性 描述 @BeforeSuite @AfterSuite @BeforeTest @AfterTes ...

  10. 二、Spring MVC之常用注解

    Spring MVC中常用的注解有以下几个:@Controller, @RequestMapping, @PathVariable, @CookieValue, @SessionAttributes. ...

最新文章

  1. python字符串符号替换punctuation
  2. linux ubuntu 16.04下deb文件的安装以及一些问题的解决
  3. 【ThinkPHP系列篇】Thinkphp框架的CURD操作(三)
  4. mysql幻读和不可重复读的区别_面试官:MySQL的可重复读级别能解决幻读吗
  5. C#:Dockpanel的一些入门的基本操作
  6. uniapp image图片切换动效_动效设计从概念到落地
  7. Struts2漏洞分析,漏洞波及全系版本
  8. java wsdl反向生成源码,并使用CXF实现客户端调用代码
  9. 均值模糊调试遇到的问题
  10. PostgreSQL 的 window函数
  11. LaTeX简历模版,自己瞎做的,请多指教
  12. 小程序 微信小商店 获取商品列表
  13. 【转】让ubuntu自带词典可以本地查…
  14. 基于Arduino开发的智能小车
  15. iPhone删除的照片能恢复吗?苹果手机照片怎么恢复
  16. 用mysql设计学籍管理系统_学生学籍管理系统(SQL数据库系统设计)(完整版).pdf...
  17. win10安装cuda10.2+tensorflow-GPU 1.14.0
  18. python内置函数bin,Python内置函数bin() oct()等实现进制转换
  19. 【KDD2022教程】图算法公平性:方法与趋势
  20. shell脚本 把十进制数据转换成十六进制

热门文章

  1. 阳春三月,正是樱花绽放时,借此机会给午饭献上鲜艳艳的视觉盛宴!
  2. html页面怎么适配拼接的大屏,大屏上的全屏页面的自适应适配方案
  3. 北京因地制宜编制村庄规划 着重体现京韵农味
  4. ELK——Kibana 安装使用性能监控工具 APM
  5. 微信人工客服终于来了,但是「此」微信客服非「彼」微信客服
  6. Meta 将使用微软 Azure 最新虚拟机 (VM) 系列,多达 5400 个 GPU
  7. BUUCTF 每日打卡 2021-8-11
  8. 目前股指期货保证金(目前股指期货保证金是多少)
  9. 黑盒测试和白盒测试的基本原理/区别是什么?
  10. 走近汇编理解与内核编程(楚狂人)