shiro 用户权限验证注解

@RequiresAuthentication

接口的源码注释大意是:
要求当前Subject在当前会话期间已经被验证,以便访问或调用带注释的类/实例/方法。 这比@RequiresUser注释更具限制性。
这个注解保证了: subject.isAuthenticated() === true

//java.lang.annotation.Target     用于设定注解范围
//java.lang.annotation.ElementYype     Target通过ElementType来指定注解适用范围的枚举集合
//这里表示 声明 只作用在方法、类或者接口
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresAuthentication {
}

@Retention注解解释

@RequiresUser

接口的源码注释大意是:
要求当前subject是要访问或调用的带注释的类/实例/方法的应用程序用户。 这比RequiresAuthentication注释的限制要少。
Shiro 将“用户”定义为“记住”或经过身份验证的主题:
经过身份验证的用户是在当前会话期间成功登录(证明其身份)的subject。
被记住的用户是任何至少一次证明其身份的主体,尽管不一定是在他们当前的会话期间,并要求系统记住他们。

简单来说就是:
验证用户是否被记忆,user有两种含义:
  一种是成功登录的(subject.isAuthenticated() 结果为true);
  另外一种是被记忆的(subject.isRemembered()结果为true)。

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresUser {
}

@RequiresGuest

这个就是@RequiresUser的逆逻辑,!@RequiresGuest = @RequiresUser
RequiresGuest ===subject.getPrincipal() == null
源码解释:


/**
 * Requires the current Subject to be a &quot;guest&quot;, that is, they are not authenticated <em>or</em> remembered
 * from a previous session for the annotated class/instance/method to be accessed or invoked.
 * <p/>
 * This annotation is the logical inverse of the {@link RequiresUser RequiresUser} annotation. That is,
 * <code>RequiresUser == !RequiresGuest</code>, or more accurately,
 * <p/>
 * <code>RequiresGuest === subject.{@link org.apache.shiro.subject.Subject#getPrincipal() getPrincipal()} == null</code>.
 *
 * @see RequiresAuthentication
 * @see RequiresUser
 *
 * @since 0.9.0
 */
@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresGuest {
}

@RequiresRoles

例如:@RequiresRoles(“aRoleName”);
  void someMethod();
  如果subject中有aRoleName角色才可以访问方法someMethod。如果没有这个权限则会抛出异常AuthorizationException。

@RequiresPermissions

例如: @RequiresPermissions({“file:read”, “write:aFile.txt”} )
  void someMethod();
  要求subject中必须同时含有file:read和write:aFile.txt的权限才能执行方法someMethod()。否则抛出异常AuthorizationException。

@Target({ElementType.TYPE, ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequiresPermissions {

    /**
     * 将传递给org.apache.shiro.subject.Subject.isPermitted(String)
     * 以确定是否允许用户调用受此注释保护的代码的权限字符串。 
     */
    String[] value(); 
    /**
     * 指定多个角色时权限检查的逻辑操作。 AND 是默认值 
     */
    Logical logical() default Logical.AND; 

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值