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

最新文章

  1. Jenkins在Windows系统dotnet平台持续集成
  2. php中include()和require()的区别
  3. Mongodb集群搭建过程及常见错误
  4. eclipse中java项目的build path详解
  5. 网站通用登录模块代码 分类: ASP.NET 2014-12-06 10:49 615人阅读 评论(0) 收藏
  6. 安全沙箱冲突:Loader.content:XX 不能访问 XX 可以通过调用 Security.allowDomain 来避免此冲突。
  7. install xdebug on fedora
  8. 在git bush中如何退出vim编辑器
  9. ASP.Net MVC与WebForm的区别
  10. HDU 1073 - Online Judge
  11. SQLite.dll混合模式程序集是针对“v2.0.50727”版的运行时生成的,在没有配置其他信息的情况下,无法在 4.0 运行时中加载该程序集。
  12. POJ 2084 Game of Connections(卡特兰数)
  13. 手机自动化测试:appium源码分析之bootstrap四
  14. thinkphp中使用phpexcel读取所有的行记录
  15. java制作验证码(java验证码小程序)
  16. JS实现鼠标滚动事件,兼容IE9,FF,Chrome.
  17. 201772020113李清华《面向对象程序设计(java)》第九周学习总结
  18. HTML5基础实例
  19. 资源 | 源自斯坦福CS229,机器学习备忘录在集结
  20. webapi + windows计划 + mshta 实现定时执行任务

热门文章

  1. DBImg: 图片文件-二进制文件的转换
  2. MySQL表名、列名区分大小写详解
  3. typeof、offsetof、container_of的解释
  4. 【Repost】A Practical Intro to Data Science
  5. 21. Merge Two Sorted Lists
  6. C++ Primer : 第十二章 : 文本查询程序
  7. 【渗透测试学习平台】 web for pentester -1.介绍与安装
  8. HTML DOM部分---做竖向横向的下拉导航 下拉菜单 图片轮播(圆点、箭头) 选项卡 进度条;
  9. leetcode 154. Find Minimum in Rotated Sorted Array II --------- java
  10. 救援行动(save) (BFS)