最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

spring aop實現(xiàn)用戶權(quán)限管理的示例

 更新時間:2017年12月07日 09:15:54   作者:whfstudio  
本篇文章主要介紹了spring aop實現(xiàn)用戶權(quán)限管理的示例,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧

AOP 在實際項目中運用的場景主要有 權(quán)限管理(Authority Management)、事務(wù)管理(Transaction Management)、安全管理(Security)、日志管理(Logging)和調(diào)試管理(Debugging) 等。

問題源于項目開發(fā)

最近項目中需要做一個權(quán)限管理模塊,按照之前同事的做法是在controller層的每個接口調(diào)用之前上做邏輯判斷,這樣做也沒有不妥,但是代碼重復率太高,而且是體力勞動,so,便有了如題所說的使用spring aop做一個切點來實現(xiàn)通用功能的權(quán)限管理,這樣也就降低了項目后期開發(fā)的可擴展性。

權(quán)限管理的代碼實現(xiàn)與配置文件

在最小的代碼修改程度上,aop無疑是最理想的選擇。項目中有各種權(quán)限的復合,相對來說邏輯復雜度比較高,所以一步步來。因為權(quán)限涉及到的是后端接口的調(diào)用所以樓主選擇在controller層代碼做切面,而切點就是controller中的各個方法塊,對于通用訪問權(quán)限,我們使用execution表達式進行排除。

只讀管理員權(quán)限的實現(xiàn)及切點選擇

對于實現(xiàn)排除通用的controller,樓主采用的是execution表達式邏輯運算。因為只讀管理員擁有全局讀權(quán)限,而對于增刪改權(quán)限,樓主采用的是使用切點切入是增刪改的方法,so,這個時候規(guī)范的方法命名就很重要了。對于各種與只讀管理員進行復合的各種管理員,我們在代碼中做一下特殊判斷即可。下面是spring aop的配置文件配置方法。

<bean id="usersPermissionsAdvice"
     class="com.thundersoft.metadata.aop.UsersPermissionsAdvice"/>
  <aop:config>
    <!--定義切面 -->
    <aop:aspect id="authAspect" ref="usersPermissionsAdvice">
      <!-- 定義切入點 (配置在com.thundersoft.metadata.web.controller下所有的類在調(diào)用之前都會被攔截) -->
      <aop:pointcut
          expression="(execution(* com.thundersoft.metadata.web.controller.*.add*(..)) or
          execution(* com.thundersoft.metadata.web.controller.*.edit*(..)) or
          execution(* com.thundersoft.metadata.web.controller.*.del*(..)) or
          execution(* com.thundersoft.metadata.web.controller.*.update*(..)) or
          execution(* com.thundersoft.metadata.web.controller.*.insert*(..)) or
          execution(* com.thundersoft.metadata.web.controller.*.modif*(..))) or
          execution(* com.thundersoft.metadata.web.controller.*.down*(..))) and (
          !execution(* com.thundersoft.metadata.web.controller.FindPasswordController.*(..)) and
          !execution(* com.thundersoft.metadata.web.controller.SelfServiceController.*(..)) and
          !execution(* com.thundersoft.metadata.web.controller.HomeController.*(..)) and
          !execution(* com.thundersoft.metadata.web.controller.UserStatusController.*(..)) and
          !execution(* com.thundersoft.metadata.web.controller.DashboardController.*(..)) and
          !execution(* com.thundersoft.metadata.web.controller.MainController.*(..))))"
          id="authPointCut"/>
      <!--方法被調(diào)用之前執(zhí)行的 -->
      <aop:before method="readOnly"
            pointcut-ref="authPointCut"/>
    </aop:aspect>
  </aop:config>

只讀管理員權(quán)限管理代碼實現(xiàn)

上面說了那么多,廢話不多說了,下面是對只讀權(quán)限與各種復合權(quán)限進行控制的切面代碼實現(xiàn)。

/**
   * 對只讀管理員以及其復合管理員進行aop攔截判斷.
   * @param joinPoint 切入點.
   * @throws IOException
   */
  public void readOnly(JoinPoint joinPoint) throws IOException {

    /**
     * 獲取被攔截的方法.
     */
    String methodName = joinPoint.getSignature().getName();

    /**
     * 獲取被攔截的對象.
     */
    Object object = joinPoint.getTarget();
    logger.info("權(quán)限管理aop,方法名稱" + methodName);
    HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    HttpServletResponse response =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    String roleFlag = GetLoginUserInfor.getLoginUserRole(request);

    /**
     * 超級管理員
     */
    if (PermissionsLabeled.super_Admin.equals(roleFlag)) {
      return;
    }

    /**
     * 只讀管理員做數(shù)據(jù)更改權(quán)限的判斷
     */
    if (PermissionsLabeled.reader_Admin.equals(roleFlag)) {
      logger.error("只讀管理員無操作權(quán)限!");
      response.sendRedirect(request.getContextPath() + "/auth/readOnly");
    }

    /**
     * 部門管理員,且為只讀管理員,
     */
    if (PermissionsLabeled.dept_reader_Admin.equals(roleFlag)) {
      if (object instanceof DepartmentController) {
        return;
      }
      if (object instanceof UserController) {
        if (methodName.contains("addAdmin")) {
          response.sendRedirect(request.getContextPath() + "/auth/readOnly");
        }
        if (methodName.contains("deleteAdmin")) {
          response.sendRedirect(request.getContextPath() + "/auth/readOnly");
        }
        if (methodName.contains("updateAdmin")) {
          response.sendRedirect(request.getContextPath() + "/auth/readOnly");
        }
        return;
      }
      if (object instanceof GroupController) {
        return;
      }
      logger.error("部門管理員,且為只讀管理員無操作權(quán)限!");
      response.sendRedirect(request.getContextPath() + "/auth/readOnly");
    }

    /**
     * 應(yīng)用管理員,且為只讀管理員
     */
    if (PermissionsLabeled.app_reader_Admin.equals(roleFlag)) {
      if (object instanceof AppController) {
        return;
      }
      if (object instanceof AppPolicyController) {
        return;
      }
      logger.error("應(yīng)用管理員,且為只讀管理員無操作權(quán)限!");
      response.sendRedirect(request.getContextPath() + "/auth/readOnly");
    }

    /**
     * 部門管理員,且為應(yīng)用管理員,且為只讀管理員
     */
    if (PermissionsLabeled.dept_app_reader_Admin.equals(roleFlag)) {
      if (object instanceof DepartmentController) {
        return;
      }
      if (object instanceof UserController) {
        return;
      }
      if (object instanceof GroupController) {
        return;
      }
      if (object instanceof AppController) {
        return;
      }
      if (object instanceof AppPolicyController) {
        return;
      }
      logger.error("部門管理員,且為應(yīng)用管理員,且為只讀管理員無操作權(quán)限");
      response.sendRedirect(request.getContextPath() + "/auth/readOnly");
    }
  }

具有專門功能的管理員權(quán)限控制的切點選擇

因為具有專門的管理員權(quán)限比較特殊,樓主采用的方式除了通用訪問權(quán)限之外的controller全切,特殊情況在代碼邏輯里面做實現(xiàn)即可。配置文件代碼如下:

<aop:config>
    <!--定義切面 -->
    <aop:aspect id="authAspect" ref="usersPermissionsAdvice">
      <!-- 定義切入點 (配置在com.thundersoft.metadata.web.controller下所有的類在調(diào)用之前都會被攔截) -->
      <aop:pointcut
          expression="(execution(* com.thundersoft.metadata.web.controller.*.*(..)) and (
          !execution(* com.thundersoft.metadata.web.controller.FindPasswordController.*(..)) and
          !execution(* com.thundersoft.metadata.web.controller.SelfServiceController.*(..)) and
          !execution(* com.thundersoft.metadata.web.controller.HomeController.*(..)) and
          !execution(* com.thundersoft.metadata.web.controller.UserStatusController.*(..)) and
          !execution(* com.thundersoft.metadata.web.controller.DashboardController.*(..)) and
          !execution(* com.thundersoft.metadata.web.controller.MainController.*(..))))"
          id="appAuthPointCut"/>
      <!--方法被調(diào)用之前執(zhí)行的 -->
      <aop:before method="appDeptAuth"
            pointcut-ref="appAuthPointCut"/>
    </aop:aspect>
  </aop:config>

##權(quán)限管理的切面代碼實現(xiàn)

/**
   * 對應(yīng)用管理員以及部門管理員進行aop攔截判斷.
   * @param joinPoint 切入點.
   * @throws IOException
   */
  public void appDeptAuth(JoinPoint joinPoint) throws IOException {
    /**
     * 獲取被攔截的方法.
     */
    String methodName = joinPoint.getSignature().getName();

    /**
     * 獲取被攔截的對象.
     */
    Object object = joinPoint.getTarget();
    logger.info("權(quán)限管理aop,方法名稱",methodName);
    HttpServletRequest request =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getRequest();
    HttpServletResponse response =((ServletRequestAttributes) RequestContextHolder.getRequestAttributes()).getResponse();
    String roleFlag = GetLoginUserInfor.getLoginUserRole(request);

    /**
     * 超級管理員
     */
    if (PermissionsLabeled.super_Admin.equals(roleFlag)) {
      return;
    }

    /**
     * 應(yīng)用管理員做數(shù)據(jù)更改權(quán)限的判斷
     */
    if (PermissionsLabeled.app_Admin.equals(roleFlag)) {
      if (object instanceof AppController) {
        return;
      }
      if (object instanceof AppPolicyController) {
        return;
      }
      logger.error("應(yīng)用管理員無操作權(quán)限");
      response.sendRedirect(request.getContextPath() + "/auth/readOnly");
    } else if (PermissionsLabeled.dept_Admin.equals(roleFlag)) {
      if (object instanceof DepartmentController) {
        return;
      }
      if (object instanceof UserController) {
        return;
      }

      if (object instanceof GroupController) {
        return;
      }
      if ("getAllDepartments".equals(methodName)) {
        return;
      }
      logger.error("應(yīng)用管理員無操作權(quán)限");
      response.sendRedirect(request.getContextPath() + "/auth/readOnly");
    } else {
      return;
    }
  }

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

  • SpringData JPA中@OneToMany和@ManyToOne的用法詳解

    SpringData JPA中@OneToMany和@ManyToOne的用法詳解

    這篇文章主要介紹了SpringData JPA中@OneToMany和@ManyToOne的用法詳解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-10-10
  • 通過AOP環(huán)繞通知如何實現(xiàn)事務(wù)控制

    通過AOP環(huán)繞通知如何實現(xiàn)事務(wù)控制

    這篇文章主要介紹了通過AOP環(huán)繞通知如何實現(xiàn)事務(wù)控制的操作,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java 實現(xiàn)Comparable接口排序,升序、降序、倒敘

    java 實現(xiàn)Comparable接口排序,升序、降序、倒敘

    這篇文章主要介紹了java 實現(xiàn)Comparable接口排序,升序、降序、倒敘,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-08-08
  • 解析Java并發(fā)Exchanger的使用

    解析Java并發(fā)Exchanger的使用

    Exchanger是java 5引入的并發(fā)類,Exchanger顧名思義就是用來做交換的。這里主要是兩個線程之間交換持有的對象。當Exchanger在一個線程中調(diào)用exchange方法之后,會等待另外的線程調(diào)用同樣的exchange方法。兩個線程都調(diào)用exchange方法之后,傳入的參數(shù)就會交換。
    2021-06-06
  • Java8中Stream流求最大值最小值的實現(xiàn)示例

    Java8中Stream流求最大值最小值的實現(xiàn)示例

    本文主要介紹了Java8中Stream流求最大值最小值的實現(xiàn)示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2023-04-04
  • mybatis中數(shù)據(jù)加密與解密的實現(xiàn)

    mybatis中數(shù)據(jù)加密與解密的實現(xiàn)

    數(shù)據(jù)加解密的實現(xiàn)方式多種多樣,在mybatis環(huán)境中數(shù)據(jù)加解密變得非常簡單易用,本文主要介紹了mybatis中數(shù)據(jù)加密與解密的實現(xiàn),文中通過示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2022-03-03
  • 詳解JVM系列之對象的鎖狀態(tài)和同步

    詳解JVM系列之對象的鎖狀態(tài)和同步

    鎖和同步是java多線程編程中非常常見的使用場景。為了鎖定多線程共享的對象,Java需要提供一定的機制來實現(xiàn)共享對象的鎖定。當?shù)诙€線程進入同一個區(qū)域的時候,必須等待第一個線程解鎖該對象。JVM是怎么做到的呢?快來一起看看吧。
    2021-06-06
  • 教你怎么使用hadoop來提取文件中的指定內(nèi)容

    教你怎么使用hadoop來提取文件中的指定內(nèi)容

    發(fā)現(xiàn)有很多小伙伴不會使用hadoop來提取文件中的指定內(nèi)容,今天特地整理了這篇文章,文中有非常詳細的代碼示例,對不會這個方法的小伙伴們有很好地幫助,需要的朋友可以參考下
    2021-05-05
  • 2020JDK1.8安裝教程詳解(一次就可安裝成功)

    2020JDK1.8安裝教程詳解(一次就可安裝成功)

    這篇文章主要介紹了2020JDK1.8安裝教程詳解(一次就可安裝成功),本文通過圖文并茂的形式分步驟給大家講解的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
    2020-08-08
  • 淺談Java消息隊列總結(jié)篇(ActiveMQ、RabbitMQ、ZeroMQ、Kafka)

    淺談Java消息隊列總結(jié)篇(ActiveMQ、RabbitMQ、ZeroMQ、Kafka)

    這篇文章主要介紹了淺談Java消息隊列總結(jié)篇(ActiveMQ、RabbitMQ、ZeroMQ、Kafka),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2019-05-05

最新評論

会同县| 永新县| 铁岭市| 巩义市| 泸溪县| 新邵县| 舟山市| 卢龙县| 聂荣县| 云梦县| 上虞市| 黄大仙区| 米泉市| 察隅县| 禹城市| 和林格尔县| 佳木斯市| 吴桥县| 大关县| 余姚市| 永靖县| 芜湖市| 江达县| 昌邑市| 称多县| 浦江县| 合江县| 陵川县| 海阳市| 苍溪县| 金秀| 文山县| 卓资县| 浠水县| 勐海县| 阜平县| 永嘉县| 伊川县| 蕉岭县| 琼海市| 绥阳县|