Shiro AnnotationHandler注解处理器设计概念

顶端抽象类AnnotationHandler

该类是关乎注解的处理类,只涉及到注解的一些行为,符合面向对象的原则

承上启下抽象类AuthorizingAnnotationHandler

该类是权限注解处理器类,涉及到注解的行为所以继承了AnnotationHandler,又因为涉及到权限判断所以定义了判断权限的抽象方法,抽象化判断方法,使用子类具体实现判断方法

具体实现类RoleAnnotationHandler

提供权限注解的信息和权限判断的方法实现

public RoleAnnotationHandler() {
        super(RequiresRoles.class);
    }

具体实现

public void assertAuthorized(Annotation a) throws AuthorizationException {
    if (!(a instanceof RequiresRoles)) return;

    RequiresRoles rrAnnotation = (RequiresRoles) a;
    String[] roles = rrAnnotation.value();

    if (roles.length == 1) {
        getSubject().checkRole(roles[0]);
        return;
    }
    if (Logical.AND.equals(rrAnnotation.logical())) {
        getSubject().checkRoles(Arrays.asList(roles));
        return;
    }
    if (Logical.OR.equals(rrAnnotation.logical())) {
        // Avoid processing exceptions unnecessarily - "delay" throwing the exception by calling hasRole first
        boolean hasAtLeastOneRole = false;
        for (String role : roles) if (getSubject().hasRole(role)) hasAtLeastOneRole = true;
        // Cause the exception if none of the role match, note that the exception message will be a bit misleading
        if (!hasAtLeastOneRole) getSubject().checkRole(roles[0]);
    }
}

 

转载于:https://www.cnblogs.com/BINGJJFLY/p/9071235.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值