区别: @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 支持使用注解的形式 ,写在方法和接口上拦截 ...
随机推荐
- jQuery对象与DOM对象之间的转换
刚开始学习jQuery,可能一时会分不清楚哪些是jQuery对象,哪些是DOM对象.至于DOM对象不多解释,我们接触的太多了,下面重点介绍一下jQuery,以及两者相互间的转换. 什么是jQuery对 ...
- 堆(Heap)和二叉堆(Binary heap)
堆(Heap) The operations commonly performed with a heap are: create-heap: create an empty heap heapify ...
- allpaths 使用
软件下载与说明:http://www.broadinstitute.org/software/allpaths-lg/blog/?page_id=12 原始数据的深度要达到100以上. 至少要两个库, ...
- 理解 %IOWAIT (%WIO)
%iowait 是 “sar -u” 等工具检查CPU使用率时显示的一个指标,在Linux上显示为 %iowait,在有的Unix版本上显示为 %wio,含义都是一样的,这个指标常常被误读,很多人把它 ...
- ANTLR3完全参考指南读书笔记[07]
前言 真正意义上的程序员都很懒,懒的连多余的一行代码也不写. 如果能将底层满手油污的活儿都可以交给别人去做,自己就扮演个智囊团成员的角色,生活会比想象中的还要惬意. 严格的按照指令执行长时间不知疲倦的 ...
- xampp改到phpmyadmin的root密碼無法登錄
open /Applications/XAMPP/xamppfiles/phpmyadmin/config.inc.php $cfg['Servers'][$i]['auth_type'] = 'co ...
- ZOJ 1110 Dick and Jane
原题链接 题目大意:Dick和Jane的年龄之和等于他们宠物的年龄,宠物之间的年龄关系XXX. 解法:小学奥数题目,枚举法. 参考代码: #include<iostream> #inclu ...
- 前后台Json的转换
jsp:JSON.stringify(params):params:表示数组 servlet:Store store = (Store) JSONObject.toBean(JSONObject.fr ...
- springMVC访问静态资源的两种方式
1<mvc:default-servlet-handler/> 2 <mvc:resources mapping="/images/**" location=&q ...
- POJ-1741 Tree (树上点分治)
题目大意:一棵带边权无根树,边权代表距离,求距离小于等于k的点对儿数. 题目分析:这两个点之间的路径只有两种可能,要么经过根节点,要么在一棵子树内.定义depth(i)表示点 i 到根节点的距离,be ...