Security注解:@PreAuthorize,@PostAuthorize, @Secured
一、注解式方法级安全开启
需要在WebSecuirtyConfig添加配置:
@Configuration
@EnableWebSecurity //启用Spring Security.会拦截注解了@PreAuthrize注解的配置.
@EnableGlobalMethodSecurity(prePostEnabled=true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter{}
二、允许的注解
这里主要@PreAuthorize, @PostAuthorize, @Secured这三个注解可以使用。
2.1 @Secured
当@EnableGlobalMethodSecurity(securedEnabled=true)的时候,@Secured可以使用:
@GetMapping("/helloUser")
@Secured({"ROLE_normal","ROLE_admin"})
public String helloUser() {return "hello,user";
}
说明:拥有normal或者admin角色的用户都可以方法helloUser()方法。另外需要注意的是这里匹配的字符串需要添加前缀“ROLE_“。
如果我们要求,只有同时拥有admin & noremal的用户才能方法helloUser()方法,这时候@Secured就无能为力了。
2.2 @PreAuthorize
Spring的 @PreAuthorize/@PostAuthorize 注解更适合方法级的安全,也支持Spring 表达式语言,提供了基于表达式的访问控制。当@EnableGlobalMethodSecurity(prePostEnabled=true)的时候,@PreAuthorize可以使用:
@GetMapping("/helloUser")
@PreAuthorize("hasAnyRole('normal','admin')")
public String helloUser() {return "hello,user";
}
说明:拥有normal或者admin角色的用户都可以方法helloUser()方法。
此时如果我们要求用户必须同时拥有normal和admin的话,那么可以这么编码:
@GetMapping("/helloUser")
@PreAuthorize("hasRole('normal') AND hasRole('admin')")
public String helloUser() {return "hello,user";
}
此时如果使用user/123登录的话,就无法访问helloUser()的方法了。
2.3 @PostAuthorize
@PostAuthorize 注解使用并不多,在方法执行后再进行权限验证,适合验证带有返回值的权限,Spring EL 提供 返回对象能够在表达式语言中获取返回的对象returnObject。
当@EnableGlobalMethodSecurity(prePostEnabled=true)的时候,@PostAuthorize可以使用:
@GetMapping("/helloUser")
@PostAuthorize(" returnObject!=null && returnObject.username == authentication.name")
public User helloUser() {Object pricipal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();User user;if("anonymousUser".equals(pricipal)) {user = null;}else {user = (User) pricipal;}return user;
}
Security注解:@PreAuthorize,@PostAuthorize, @Secured相关推荐
- Spring Security 4 Method security using @PreAuthorize,@PostAuthorize, @Secured, EL--转
原文地址:http://websystique.com/spring-security/spring-security-4-method-security-using-preauthorize-pos ...
- 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 ...
- Spring Security 4 使用@PreAuthorize,@PostAuthorize, @Secured, EL实现方法安全
原文地址: http://blog.csdn.net/w605283073/article/details/51327182 本文探讨spring Security 4 基于@PreAuthoriz ...
- spring security 注解_Spring框架使用@Autowired自动装配引发的讨论
原文首发于掘金 作者:walkinger 链接:https://juejin.im/post/5d4163ede51d4561f64a078b 问题描述 有同事在开发新功能测试时,报了个错,大致就是, ...
- spring security 注解不生效的一些隐含问题
2019独角兽企业重金招聘Python工程师标准>>> 如果spring 使用方法级别控制 不能在配置文件中对 HttpSecurity 配置 authorizeRequests() ...
- SpringBoot 精通系列-如何使用Spring Boot Security进行权限控制
导语 如何设计一个高效健全的安全策略是保证企业数据安全的的关键,笔者见过设计的比较优秀的安全管理策略也见过设计的比较Low的安全管理策略.如何保证高效的安全策略,在Java中Spring官方提供了 ...
- Spring Security OAuth2.0认证授权知识概括
Spring Security OAuth2.0认证授权知识概括 安全框架基本概念 基于Session的认证方式 Spring Security简介 SpringSecurity详解 分布式系统认证方 ...
- spring security技术分享
Spring Security技术专题 一.初识认证和授权 1.1 认证 1.2 会话 1.3 授权 1.4 授权的数据模型 1.5 RBAC 1.5.1 角色访问控制 1.5.2 资源访问控制 1. ...
- Spring Security 4 整合Hibernate 实现持久化登录验证(带源码)
上一篇文章:Spring Security 4 整合Hibernate Bcrypt密码加密(带源码) 原文地址:http://websystique.com/spring-security/spri ...
- SpringSecurity给用户授权,一个用户能同时拥有多种身份Role,及权限鉴权注解方法hasRole及hasAuthority的使用区别
文章目录 1.SpringSecurity一般分为两个重点 2.实际上用户存在一般就等于认证成功,认证成功之后就存在授权的问题 3.一个用户可以有多个身份 4.看图 5.hasRole及hasAuth ...
最新文章
- 冒泡排序(java实现)
- KEIL-MDK 5 CMSIS的问题
- 【Laravel Cache】 配置redis 存储缓存,通俗易懂,一次就掌握
- nginx、uwsgi部署django项目理论+实战
- 如何解决ABBYY FineReader中表格检测不到问题
- when busy dialog closed iDuration renderFioriFlower jQuery Animation closeL
- Django设置TIME_ZONE和LANGUAGE_CODE为中国区域
- C/C++ 交换两个数,不使用第三个变量, 函数模板来实现
- 杭电oj2047-2049、2051-2053、2056、2058
- 往java里输入坐标值_java.让用户输入x坐标,和y坐标。当用户输入完x坐标(比如200),敲enter,...
- javascript 代码_如何使您JavaScript代码简单易读
- 推荐一本学Python的好书《Python程序设计(第2版)》
- LeetCode 701 二叉搜索树中的插入操作
- w3wp.exe进程资源占用过大问题
- XLSTransformer 导出Excel数据
- 星际译王,金山词霸,有道词典,词库下载 1
- 关于破解Quartus
- 监测-病毒篇(病毒的了解和认识)
- 机器学习教程之4-正则化(Regularization)
- MATLAB中画柱状图
热门文章
- Welcome home, Chtholly [Ynoi2018]五彩斑斓的世界
- 小白的倔强-NPN和PNP三极管的使用区别以及简单检测
- 中文转拼音全拼和首字母
- bootstrap编写响应式页面
- 基于python的网络聊天室论文_基于python的聊天室(2),实现,二
- 浅谈iOS进阶路线,让自己人生不迷茫!
- Linux动态链接库.so文件的命名及用途总结
- Fractions to Decimals_usaco2.4.5_暴力
- Android FileProvider详细解析和踩坑指南
- 火箭发射:点击率预估界的“神算子”是如何炼成的?