在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()的更多相关文章

  1. @Secured()、 @PreAuthorize() 、 @RolesAllowed()

    在Spring security的使用中,为了对方法进行权限控制,通常采用的三个注解,就是@Secured().@PreAuthorize().@RolesAllowed(). 示例,修改用户密码必须 ...

  2. @Secured(), @PreAuthorize()

    前面简单的提到过这两个注解的区别,那只是从配置以及原理上做的说明,今天,将从使用即代码层面加以说明这两个的使用注意事项! 首先, 若是自己实现用户信息数据库存储的话,需要注意UserDetails的函 ...

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

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

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

    [相关已翻译的本系列其他文章,点击分类里面的spring security 4] 上一篇:Spring Security 4 整合Hibernate 实现持久化登录验证(带源码) 原文地址:http: ...

  5. 【Spring】关于Boot应用中集成Spring Security你必须了解的那些事

    Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...

  6. 关于Boot应用中集成Spring Security你必须了解的那些事

    Spring Security Spring Security是Spring社区的一个顶级项目,也是Spring Boot官方推荐使用的Security框架.除了常规的Authentication和A ...

  7. Spring Boot中集成Spring Security 专题

    check to see if spring security is applied that the appropriate resources are permitted: @Configurat ...

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

  9. spring security 在controller层 方法级别使用注解 @PreAuthorize("hasRole('ROLE_xxx')")设置权限拦截 ,无权限则返回403

    1.前言 以前学习的时候使用权限的拦截,一般都是对路径进行拦截 ,要么用拦截器设置拦截信息,要么是在配置文件内设置拦截信息, spring security 支持使用注解的形式 ,写在方法和接口上拦截 ...

随机推荐

  1. Makefile学习(1) arm-linux-ld arm-linux-objcopy arm-linux-objdump

    记录自己所学的点点滴滴O(∩_∩)O哈哈~ makefile: link.bin: start.o main.o arm-linux-ld -Tlink.lds -o link.elf $^ arm- ...

  2. 利用range() 控制循环

    s = ['a','b','c','d','e'] for i in range(len(s)):...     if i < len(s)-1:...         print s[i] a ...

  3. dalvik虚拟内存管理之二——垃圾收集

    转载自http://www.miui.com/thread-75028-1-1.html 垃圾收集是dalvik虚拟机内存管理的核心,垃圾收集的性能在很大程度上影响了一个Java程序内存使用的效率.顾 ...

  4. MyEclipse+Struts+Hibernate+Mysql开发环境配置

    软件: jdk-6u22-windows-x64.exe apache-tomcat-6.0.29.exe mysql-5.1.51-winx64.exe myeclipse-8.6.0-win32. ...

  5. 转 关于C#中派生类调用基类构造函数的理解

    关于C#中派生类调用基类构造函数的理解 .c#class       本文中的默认构造函数是指在没有编写构造函数的情况下系统默认的无参构造函数 1.  当基类中没有自己编写构造函数时,派生类默认的调用 ...

  6. 安装Python+Pywin32(version 3.3)

    1.下载python3.3,默认设置,安装. 2.完成后,在开始-程序中运行python IDLE.我在运行时出现了应用程序运行异常,原因是与其他软件内存发生冲突,如.net framework等. ...

  7. Eclipse反编译插件jad安装

    下载jadClipse地址: 链接: http://pan.baidu.com/s/1kTN4TPd  提取码: 3fvd 将net.sf.jadclipse_3.3.0.jar拷贝到eclipse的 ...

  8. spring mvc显示图片(个人记录)

    @ResponseBody @RequestMapping(value = {"/",""}, method = RequestMethod.GET, prod ...

  9. c++语法集锦

    1.指针的引用 他也是引用,引用是特定内存块的别名 2.变量定义 更准确的说是内存使用约定,并为该约定命名 命名3.指向常变量的指针和常指针 有点拗口,都是指针,但对于所在内存块的使用约定不同.常变量 ...

  10. MFC开发上位机到底用Dialog结构还是文档结构?

    最近要跟着导师一起开发一款大型上位机.MFC新人在考虑用对话框结构还是文档结构. 虽然说书上说大型结构的软件都需要文档结构,但是目前来看,对话框可以实现功能,并且对话框的程序更小一些,节省资源加载速度 ...