区别: @Secured(), @PreAuthorize() 及 @RolesAllowed()
在Spring security的使用中,为了对方法进行权限控制,通常采用的三个注解,就是@Secured(), @PreAuthorize() 及 @RolesAllowed()。
但是着三者之间的区别,我之前也不是很清楚,现在看看,做个小小的记录,备忘吧!
现在举例,比如修改用户密码,必须是ADMIN的权限才可以。则可以用下面三种方法:
|
@Secured({"ROLE_ADMIN"}) public void changePassword(String username, String password); |
|
@RolesAllowed({"ROLE_ADMIN"}) public void changePassword(String username, String password); |
|
@PreAuthorize("hasRole('ROLE_ADMIN')") public void changePassword(String username, String password); |
然而,这三个的区别,其实很容易被大家忽视,虽然不是太大的区别。
1. @Secured(): secured_annotation
使用时,需要如下配置Spring Security (无论是通过xml配置,还是在Spring boot下,直接注解配置,都需要指明secured-annotations)
XML: <global-method-security secured-annotations="enabled"/>
Spring boot: @EnableGlobalMethodSecurity(securedEnabled = true)
2. @RolesAllowed(): jsr250-annotations
使用时,需要如下配置Spring Security (无论是通过xml配置,还是在Spring boot下,直接注解配置,都需要指明jsr250-annotations)
XML: <global-method-security jsr250-annotations="enabled"/>
Spring boot: @EnableGlobalMethodSecurity(jsr250Enabled = true)
3. @PreAuthorize(): pre-post-annotations
使用时,需要如下配置Spring Security (无论是通过xml配置,还是在Spring boot下,直接注解配置,都需要指明pre-post-annotations)
XML: <global-method-security pre-post-annotations="enabled"/>
Spring boot: @EnableGlobalMethodSecurity(prePostEnabled = true)
@Secured and @RolesAllowed are the same the only difference is @RolesAllowed is a standard annotation (i.e. not only spring security) whereas @Secured is spring security only.
@PreAuthorize is different in a way that it is more powerful then the
other 2. It allows for SpEL expression for a more fine-grained control.
Which to use well the simplest thing that could possible work, if you
don't need expression etc. go with the standard annotations to limit the
dependency on spring classes.
比较方法授权的类型
以下的快速参考表可能在你选择授权方法检查时派上用场:
|
方法授权类型 |
声明方式 |
JSR标准 |
允许SpEL表达式 |
|
@PreAuthorize @PostAuthorize |
注解 |
No |
Yes |
|
@RolesAllowed @PermitAll @DenyAll |
注解 |
Yes |
NO |
|
@Secure |
注解 |
No |
No |
|
protect-pointcut |
XML |
No |
No |
尾注:详细的信息,还是参考Spring的官方参考文档
区别: @Secured(), @PreAuthorize() 及 @RolesAllowed()的更多相关文章
- @Secured()、 @PreAuthorize() 、 @RolesAllowed()
在Spring security的使用中,为了对方法进行权限控制,通常采用的三个注解,就是@Secured().@PreAuthorize().@RolesAllowed(). 示例,修改用户密码必须 ...
- @Secured(), @PreAuthorize()
前面简单的提到过这两个注解的区别,那只是从配置以及原理上做的说明,今天,将从使用即代码层面加以说明这两个的使用注意事项! 首先, 若是自己实现用户信息数据库存储的话,需要注意UserDetails的函 ...
- Spring Security 4 Method security using @PreAuthorize,@PostAuthorize, @Secured, EL--转
原文地址:http://websystique.com/spring-security/spring-security-4-method-security-using-preauthorize-pos ...
- Spring Security 4 使用@PreAuthorize,@PostAuthorize, @Secured, EL实现方法安全
[相关已翻译的本系列其他文章,点击分类里面的spring security 4] 上一篇:Spring Security 4 整合Hibernate 实现持久化登录验证(带源码) 原文地址:http: ...
- 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事
Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...
- 关于Boot应用中集成Spring Security你必须了解的那些事
Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...
- Spring Boot中集成Spring Security 专题
check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...
- 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)hibe ...
- spring security 在controller层 方法级别使用注解 @PreAuthorize("hasRole('ROLE_xxx')")设置权限拦截 ,无权限则返回403
1.前言 以前学习的时候使用权限的拦截,一般都是对路径进行拦截 ,要么用拦截器设置拦截信息,要么是在配置文件内设置拦截信息, spring security 支持使用注解的形式 ,写在方法和接口上拦截 ...
随机推荐
- cron用法
cron用法说明 cron的用法老是记不住,索性写下来备忘.下文内容大部分是根据<Cron Help Guide>翻译而来,有些部分是自己加上的. 全文如下: cron来源于希腊单词chr ...
- 开源 VS 商业,消息中间件你不知道的那些事
11月23日,新炬网络中间件技术专家刘拓老师在DBA+社群中间件用户组进行了一次主题为“开源 VS 商业,消息中间件你不知道的那些事”的线上分享.小编特别整理出其中精华内容,供大家学习交流. 嘉宾简介 ...
- 关于Highcharts图表组件动态修改属性的方法(API)总结之Series
Highcharts图表组件内的Series很重要,如果说Categries是其大脑,那么Series就是其心脏.这两者才是Highcharts图表组件的重中之重啊.接下来就为大家介绍关于设置Seri ...
- 博客引索 - imsoft.cnblogs
Java C Delphi 日常工具 图片处理 视频处理 系统工具 知识经验 奇思妙想 网站推荐
- iOS学习笔记---oc语言第七天
类的扩展 NSDate是Cocoa中用于处理日期和时间的基础类,封装了某一给定的时刻,具体的日期 时间和时区 使用+date方法获取当前日期和时间 NSDate *date = [NSDate dat ...
- SM30维护视图添加按钮
转自http://blog.csdn.net/tsj19881202/article/details/7517232 遇到某需求,要求维护sm30的视图时,能加上排序按钮. 基本参考: http:// ...
- 原生js获取鼠标坐标方法全面讲解:clientX/Y,pageX/Y,offsetX/Y,layerX/Y,screenX/Y【转】
关于js鼠标事件综合各大浏览器能获取到坐标的属性总共以下五种 event.clientX/Y event.pageX/Y event.offsetX/Y event.layerX/Y event.sc ...
- php部分---include()与require()的区别、empty()与isset is_null的区别与用法详解
include()与require()的用途是完全一样的,不一定非得哪个放在最前面哪个放在中间.他们最根本的区别在于错误处理的方式不一样. 1.处理错误的方式: require()一个文件存在错误的话 ...
- apache开启url rewrite模块
在把服务器数据转移到本地服务器之后,本地打开首页出现排版紊乱等问题,经过大神指点说是url rewrite的问题. 本篇文章主要写怎样开启apache的url rewrite功能. 打开Apache2 ...
- poj1420 拓扑序
题意:给出一个表格,一部分单元格是给定的数字,而另一部分单元格则是一个式子,表示是其他一些单元格的和,让你输出最后计算出的所有格子的数. 因为有些格子需要其他格子先计算出来,所以计算顺序是按照拓扑序的 ...