成人国产在线小视频_日韩寡妇人妻调教在线播放_色成人www永久在线观看_2018国产精品久久_亚洲欧美高清在线30p_亚洲少妇综合一区_黄色在线播放国产_亚洲另类技巧小说校园_国产主播xx日韩_a级毛片在线免费

資訊專欄INFORMATION COLUMN

自定義Shiro注解

褰辯話 / 3256人閱讀

摘要:自定義注解順序創(chuàng)建自定義的注解資源管理器,繼承,添加新注解支持?jǐn)r截器,繼承方法攔截器,繼承權(quán)限處理器,繼承,校驗權(quán)限一自定義注解二權(quán)限處理器自定義權(quán)限處理器多個權(quán)限,有一個就通過三方法攔截器自定義注解的方法攔截器驗證權(quán)限四切面攔截器自定義注

自定義Shiro注解 順序

創(chuàng)建自定義的注解

資源管理器,繼承AuthorizationAttributeSourceAdvisor,添加新注解支持

AOP攔截器,繼承AopAllianceAnnotationsAuthorizingMethodInterceptor

方法攔截器,繼承AuthorizingAnnotationMethodInterceptor

權(quán)限處理器,繼承AuthorizingAnnotationHandler,校驗權(quán)限

一、自定義注解
@Target({ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Permissions {
  String[] value();
}
二、權(quán)限處理器
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.aop.AuthorizingAnnotationHandler;
import org.apache.shiro.subject.Subject;

import java.lang.annotation.Annotation;

/**
 * 自定義權(quán)限處理器
 * @author BBF
 */
public class PermissionHandler extends AuthorizingAnnotationHandler {

  public PermissionHandler() {
    super(Permissions.class);
  }

  @Override
  public void assertAuthorized(Annotation a) throws AuthorizationException {
    if (a instanceof Permissions) {
      Permissions annotation = (Permissions) a;
      String[] perms = annotation.value();
      Subject subject = getSubject();

      if (perms.length == 1) {
        subject.checkPermission(perms[0]);
        return;
      }
      // 多個權(quán)限,有一個就通過
      boolean hasAtLeastOnePermission = false;
      for (String permission : perms) {
        if (subject.isPermitted(permission)) {
          hasAtLeastOnePermission = true;
          break;
        }
      }
      // Cause the exception if none of the role match,
      // note that the exception message will be a bit misleading
      if (!hasAtLeastOnePermission) {
        subject.checkPermission(perms[0]);
      }
    }
  }
}
三、方法攔截器
import org.apache.shiro.aop.AnnotationResolver;
import org.apache.shiro.aop.MethodInvocation;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.aop.AuthorizingAnnotationMethodInterceptor;

/**
 * 自定義注解的方法攔截器
 * @author BBF
 */
public class PermissionMethodInterceptor extends AuthorizingAnnotationMethodInterceptor {
  public PermissionMethodInterceptor() {
    super(new PermissionHandler());
  }

  public PermissionMethodInterceptor(AnnotationResolver resolver) {
    super(new PermissionHandler(), resolver);
  }

  @Override
  public void assertAuthorized(MethodInvocation mi) throws AuthorizationException {
    // 驗證權(quán)限
    try {
      ((PermissionHandler) getHandler()).assertAuthorized(getAnnotation(mi));
    } catch (AuthorizationException ae) {
      // Annotation handler doesn"t know why it was called, so add the information here if possible.
      // Don"t wrap the exception here since we don"t want to mask the specific exception, such as
      // UnauthenticatedException etc.
      if (ae.getCause() == null) {
        ae.initCause(new AuthorizationException("Not authorized to invoke method: " + mi.getMethod()));
      }
      throw ae;
    }
  }
}
四、切面攔截器
import org.apache.shiro.spring.aop.SpringAnnotationResolver;
import org.apache.shiro.spring.security.interceptor.AopAllianceAnnotationsAuthorizingMethodInterceptor;

/**
 * 自定義注解的AOP攔截器
 * @author BBF
 */
public class PermissionAopInterceptor extends AopAllianceAnnotationsAuthorizingMethodInterceptor {
  public PermissionAopInterceptor() {
    super();
    // 添加自定義的注解攔截器
    this.methodInterceptors.add(new PermissionMethodInterceptor(new SpringAnnotationResolver()));
  }
}
五、注解攔截器
import org.apache.shiro.spring.security.interceptor.AuthorizationAttributeSourceAdvisor;
import org.springframework.core.annotation.AnnotationUtils;

import java.lang.reflect.Method;

/**
 * 自定義的Shiro注解攔截器
 * @author BBF
 */

public class ShiroAdvisor extends AuthorizationAttributeSourceAdvisor {
  /**
   * Create a new AuthorizationAttributeSourceAdvisor.
   */
  public ShiroAdvisor() {
    // 這里可以添加多個
    setAdvice(new PermissionAopInterceptor());
  }

  @SuppressWarnings({"unchecked"})
  @Override
  public boolean matches(Method method, Class targetClass) {
    Method m = method;
    if (targetClass != null) {
      try {
        m = targetClass.getMethod(m.getName(), m.getParameterTypes());
        return this.isFrameAnnotation(m);
      } catch (NoSuchMethodException ignored) {
        //default return value is false.  If we can"t find the method, then obviously
        //there is no annotation, so just use the default return value.
      }
    }
    return super.matches(method, targetClass);
  }

  private boolean isFrameAnnotation(Method method) {
    return null != AnnotationUtils.findAnnotation(method, Permissions.class);
  }
}
六、配置shiro

替換AuthorizationAttributeSourceAdvisorShiroAdvisor

  /**
   * 啟用注解攔截方式
   * @return AuthorizationAttributeSourceAdvisor
   */
  @Bean
  public AuthorizationAttributeSourceAdvisor authorizationAttributeSourceAdvisor() {
    AuthorizationAttributeSourceAdvisor advisor = new ShiroAdvisor();
    advisor.setSecurityManager(securityManager());
    return advisor;
  }

文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請注明本文地址:http://systransis.cn/yun/69190.html

相關(guān)文章

  • 基于shiro定義注解的擴展

    摘要:的自身注解的用法。所以自定義注解的作用很廣。但是在這里,我僅僅基于的來實現(xiàn)適用于它的自定義注解。其他的自定義的注解的編寫思路和這個也是類似的。 基于shiro的自定義注解的擴展 根據(jù)我的上一篇文章,權(quán)限設(shè)計的雜談中,涉及到了有關(guān)于前后端分離中,頁面和api接口斷開表與表層面的關(guān)聯(lián),另辟蹊徑從其他角度找到方式進行關(guān)聯(lián)。這里我們主要采取了shiro的自定義注解的方案。本篇文章主要解決以下的...

    YuboonaZhang 評論0 收藏0
  • Shiro【授權(quán)過濾器、與ehcache整合、驗證碼、記住我】

    摘要:為了達到很好的效果,我們使用來對的緩存進行管理配置會話管理器,對會話時間進行控制手動清空緩存由于驗證用戶名和密碼之前,一般需要驗證驗證碼的。 前言 本文主要講解的知識點有以下: Shiro授權(quán)過濾器使用 Shiro緩存 與Ehcache整合 Shiro應(yīng)用->實現(xiàn)驗證碼功能 記住我功能 一、授權(quán)過濾器測試 我們的授權(quán)過濾器使用的是permissionsAuthorization...

    K_B_Z 評論0 收藏0
  • 不用 Spring Security 可否?試試這個小而美的安全框架

    摘要:寫在前面在一款應(yīng)用的整個生命周期,我們都會談及該應(yīng)用的數(shù)據(jù)安全問題。用戶的合法性與數(shù)據(jù)的可見性是數(shù)據(jù)安全中非常重要的一部分。 寫在前面 在一款應(yīng)用的整個生命周期,我們都會談及該應(yīng)用的數(shù)據(jù)安全問題。用戶的合法性與數(shù)據(jù)的可見性是數(shù)據(jù)安全中非常重要的一部分。但是,一方面,不同的應(yīng)用對于數(shù)據(jù)的合法性和可見性要求的維度與粒度都有所區(qū)別;另一方面,以當(dāng)前微服務(wù)、多服務(wù)的架構(gòu)方式,如何共享Sessi...

    toddmark 評論0 收藏0
  • shiro入門筆記

    摘要:當(dāng)前可以是身份,不需要經(jīng)過認證或者在原先的中存在記錄。當(dāng)前必須擁有所有指定的角色時,才能訪問被該注解標(biāo)注的方法。 關(guān)于 Apache Shiro 概念基本都粘自官網(wǎng) http://shiro.apache.org/詳細中文博客 http://wiki.jikexueyuan.com/p...與SpringBoot整合 https://segmentfault.com/a/11... ...

    yagami 評論0 收藏0
  • apache shiro框架

    摘要:框架提供的接口,是的核心,代表安全管理器對象??梢蚤_發(fā)人員編寫,框架也提供一些。在中作為應(yīng)用程序和安全數(shù)據(jù)之間的橋梁或連接器。例如要求中必須同時含有和的權(quán)限才能執(zhí)行方法?!pache shiro框架簡介  Apache Shiro是一個強大而靈活的開源安全框架,它能夠干凈利落地處理身份認證,授權(quán),企業(yè)會話管理和加密。現(xiàn)在,使用Apache Shiro的人越來越多,因為它相當(dāng)簡單,相比比Sp...

    Tecode 評論0 收藏0

發(fā)表評論

0條評論

最新活動
閱讀需要支付1元查看
<