成人无码视频,亚洲精品久久久久av无码,午夜精品久久久久久毛片,亚洲 中文字幕 日韩 无码

資訊專欄INFORMATION COLUMN

自定義Shiro注解

褰辯話 / 3330人閱讀

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

自定義Shiro注解 順序

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

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

AOP攔截器,繼承AopAllianceAnnotationsAuthorizingMethodInterceptor

方法攔截器,繼承AuthorizingAnnotationMethodInterceptor

權(quán)限處理器,繼承AuthorizingAnnotationHandler,校驗(yàn)權(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;
      }
      // 多個(gè)權(quán)限,有一個(gè)就通過
      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 {
    // 驗(yàn)證權(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() {
    // 這里可以添加多個(gè)
    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)允許請(qǐng)勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。

轉(zhuǎn)載請(qǐng)注明本文地址:http://m.hztianpu.com/yun/69190.html

相關(guān)文章

  • 基于shiro定義注解的擴(kuò)展

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

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

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

    K_B_Z 評(píng)論0 收藏0
  • 不用 Spring Security 可否?試試這個(gè)小而美的安全框架

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

    toddmark 評(píng)論0 收藏0
  • shiro入門筆記

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

    yagami 評(píng)論0 收藏0
  • apache shiro框架

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

    Tecode 評(píng)論0 收藏0

發(fā)表評(píng)論

0條評(píng)論

閱讀需要支付1元查看
<