发布于 4年前

Spirng Security使用注解对方法做权限安全控制

Spring Security默认情况下是关闭了对方法级的安全控制。可以通过xml或者是在添加了@Configuration注解的bean上添加@EnableGlobalMethodSecurity来开启对方法级别的安全控制。Spring Security 支持三种方法级注解, 分别是 JSR-205/Secured 注解/prePostEnabled,可以在开启对方法级的安全控制时设置。

方法级的安全控制开启

通过xml开启对方法级的安全控制:

<global-method-security secured-annotations="enabled" />
<global-method-security jsr250-annotations="enabled" />
<global-method-security pre-post-annotations="enabled" />

通过注解的方式开启对方法级的安全控制:

//@Secured 注解
@EnableGlobalMethodSecurity(securedEnabled=true)
//JSR-205 注解
@EnableGlobalMethodSecurity(jsr250Enabled=true)
//@PreAuthorize 类型的注解(支持 Spring 表达式)
@EnableGlobalMethodSecurity(prePostEnabled=true)

prePostEnabled支持 Spring EL 表达式,而其他两种注解方式不支持。使用Spring EL表达式可以更灵活地定制一些权限规则。

@Secured注解的使用

只有满足角色的用户才能访问被注解的方法, 否则将会抛出 AccessDenied 异常.

@Secured("ROLE_TELLER","ROLE_ADMIN"), 该方法只允许 ROLE_TELLER 或 ROLE_ADMIN 角色的用户访问.
@Secured("IS_AUTHENTICATED_ANONYMOUSLY"), 该方法允许匿名用户访问.

JSR-205注解的使用

@DenyAll注解, 拒绝所有的访问
@PermitAll 注解, 运行所有访问
@RolesAllowed({"USER","ADMIN"}), 该方法只允许有 ROLE_USER 或 ROLE_ADMIN 角色的用户访问.

PreAuthorize的使用

@PreAuthorize 注解, 在方法调用之前, 基于表达式结果来限制方法的使用.
@PostAuthorize 注解, 允许方法调用, 但是如果表达式结果为 false, 将抛出一个安全性异常.
@PostFilter 注解, 允许方法调用, 但必要按照表达式来过滤方法的结果.
@PreFilter 注解, 允许方法调用, 但必须在进入方法之前过来输入值.

1、使用returnObject对返回值多检查

对于 @PostAuthorize 和 @PostFilter 注解, 可以在表达式中使用 returnObject 保留名, returnObject 代表着被注解方法的返回值, 我们可以使用 returnObject 保留名对注解方法的结果进行验证.

比如:

@PostAuthorize ("returnObject.owner == authentication.name")
public Book getBook();

2、使用#号引用方法参数

在表达式中, 可以使用 #arg 的形式来代表注解方法中的参数 arg.

比如:

@PreAuthorize ("#book.owner == authentication.name")
public void deleteBook(Book book);

使用@P注解或@Param注解给参数设置别名

@PreAuthorize("#c.name == authentication.name")
public void doSomething(@P("c") Contact contact);

3、内置的表达式列表

  • hasRole([role]) :如果有当前角色, 则返回 true(会自动加上 ROLE_ 前缀)
  • hasAnyRole([role1, role2]): 如果有任一角色即可通过校验, 返回true,(会自动加上 ROLE_ 前缀)
  • hasAuthority([authority]) :如果有指定权限, 则返回 true
  • hasAnyAuthority([authority1, authority2]): 如果有任一指定权限, 则返回true
  • principal:获取当前用户的 principal 主体对象
  • authentication:获取当前用户的 authentication 对象,
  • permitAll:总是返回 true, 表示全部允许
  • denyAll:总是返回 false, 代表全部拒绝
  • isAnonymous():如果是匿名访问, 返回true
  • isRememberMe(): 如果是remember-me 自动认证, 则返回 true
  • isAuthenticated(): 如果不是匿名访问, 则返回true
  • isFullAuthenticated(): 如果不是匿名访问或remember-me认证登陆, 则返回true
  • hasPermission(Object target, Object permission)
  • hasPermission(Object target, String targetType, Object permission)
©2020 edoou.com   京ICP备16001874号-3