@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 {
}
@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 "guest", 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;
}