在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的官方参考文档

转载于:https://www.cnblogs.com/jpfss/p/8668228.html

区别: @Secured(), @PreAuthorize() 及 @RolesAllowed()相关推荐

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

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

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

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

  3. SpringSecurity给用户授权,一个用户能同时拥有多种身份Role,及权限鉴权注解方法hasRole及hasAuthority的使用区别

    文章目录 1.SpringSecurity一般分为两个重点 2.实际上用户存在一般就等于认证成功,认证成功之后就存在授权的问题 3.一个用户可以有多个身份 4.看图 5.hasRole及hasAuth ...

  4. SpringSecurity,jwt oathu sso,YeZiJie

    Spring Security 中文文档 参考手册 中文版 (springcloud.cc) 一 springsecurity 1 SpringSecurity框架简介 安全框架:解决系统安全问题的框 ...

  5. SpringSecurity入门到入土教程_1

    https://gitee.com/fakerlove/spring-security 文章目录 SpringSecurity 教程 1. 简介 1.1 概念 1.2 入门案例 1.3 自定义登录逻辑 ...

  6. Spring——Security安全框架之注解使用

    文章目录 前言 本次测试 注解介绍 注解使用 @Secured @PreAuthorize @PostAuthorize @PostFilter @PreFilter 代码下载 前言 之前securi ...

  7. SpringSecurity的简单使用

    目录 概述 HelloWorld 依赖 写一个Controller 访问接口 用户认证 设置登录的用户名和密码 通过配置文件 通过配置类 自定义编写实现类 查询数据库的用户认证 自定义登录页面 用户授 ...

  8. SpringSecurity与JWT认证

    SpringSecurity 教程 1. 简介 1.1 概念 1.2 入门案例 1.3 自定义登录逻辑 1.4 自定义登录页面 1.5 自定用户名参数 1.5 自定义成功处理器 1.6 登录失败处理器 ...

  9. 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 ...

  10. Spring Security - @PreAuthorize安全表达式hasRole、hasAuthority区别

    hasRole: 角色授权:授权代码,在我们返回的UserDetails的Authority需要加ROLE_前缀,Controller上使用时不要加前缀: hasAuthority: 权限授权:用户自 ...

最新文章

  1. HDU 排名(简单题)
  2. 一份详细的“入坑Phd指南”---教你如何做笔记、整理参考书目、管理时间、如何写作、对自己和导师合理预期...
  3. LeetCode-337 House Robber III
  4. 北京 | 免费高效训练及OpenVINO™加速推理深度学习实战,送Intel神经计算棒二代...
  5. [zz]GMM-HMM语音识别模型 原理篇
  6. winform分页案例简单实现方式~
  7. LeetCode 350. Intersection of Two Arrays II
  8. 2018年各大互联网前端面试题二(滴滴打车)
  9. C语言项目-俄罗斯方块
  10. 编程实现计算n!,键盘输入n
  11. win10小娜不能使用解决方案
  12. 软件测试:黑盒测试、白盒测试和灰盒测试
  13. 空指针异常NullPointerException(小结)
  14. 国庆的人山人海,生无可恋脸
  15. 港科夜闻|香港科技大学举办北京冬奥精神云分享会,借鉴成功经验延续奥运精神...
  16. Keil uvision 5 安装和使用
  17. 现代A200(MoboDA 3360)GPRS上网设置方法
  18. 美国电子工程计算机博士 陈,美国大学博士电气与电子工程排名
  19. Matlab数字(所有数值存储为双精度浮点数)
  20. 网页自动获取题目匹配题库答案填充(原创1.0)

热门文章

  1. [Android开发学iOS系列] ViewController - 圣骑士wind - 博客园
  2. DOJP1520星门跳跃题解
  3. inside-outside algorithm详解
  4. html5页面设计技术,H5页面设计技巧有哪些?-鱼爪网
  5. 华为nova7se怎么看云相册_华为nova7SE拍照怎么样?华为nova7SE夜拍照效果如何
  6. MybatisPlusException: Your property named “xxx“ cannot find the corresponding database column name!
  7. 循环小数(Repeating Decimals, ACM/ICPC World Finals 1990, UVa202)
  8. Xmind进阶教程:配色指南
  9. Excel学习1:图表配色
  10. 中考考试的指令广播_2019年初中学业水平考试广播指令