ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

spring 加了@Aspect注解没有被执行

2019-10-25 10:36:00  阅读:730  来源: 互联网

标签:zcjd spring system project dept Aspect 注解 import com


最近参考开源后台管理系统Ruoyi做了个JPA版本的系统。在数据过滤的时候用到了@Aspect注解。然后就碰到以下坑:@Aspect没有被执行。

package com.zcjd.framework.aspectj;


import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.stereotype.Component;
import com.zcjd.common.utils.security.ShiroUtils;
import com.zcjd.framework.aspectj.lang.annotation.DSDetpList;
import com.zcjd.framework.aspectj.lang.annotation.DataScope;
import com.zcjd.project.system.dept.dto.qo.DeptQoPg;
import com.zcjd.project.system.dept.dto.qo.DeptQoSh;
import com.zcjd.project.system.dept.entity.Dept;
import com.zcjd.project.system.dept.service.IDeptService;
import com.zcjd.project.system.role.dto.qo.RoleQoPg;
import com.zcjd.project.system.role.dto.qo.RoleQoSh;
import com.zcjd.project.system.role.entity.Role;
import com.zcjd.project.system.user.dto.qo.UserQoPg;
import com.zcjd.project.system.user.dto.qo.UserQoSh;
import com.zcjd.project.system.user.entity.User;

/**
 * 数据过滤处理
 *
 * @author ruoyi
 */
@EnableAspectJAutoProxy(exposeProxy=true)
@Aspect//表示這是一個切面
@Component
public class DataScopeAspect
{

    @Autowired
    private IDeptService deptService;
       
    
    /**
     * 全部数据权限
     */
    public static final String DATA_SCOPE_ALL = "1";

    /**
     * 自定数据权限
     */
    public static final String DATA_SCOPE_CUSTOM = "2";

    /**
     * 部门数据权限
     */
    public static final String DATA_SCOPE_DEPT = "3";

    /**
     * 部门及以下数据权限
     */
    public static final String DATA_SCOPE_DEPT_AND_CHILD = "4";


    // 配置织入点
    @Pointcut("@annotation(com.zcjd.framework.aspectj.lang.annotation.DataScope)")
    public void dataScopePointCut()
    {
        
    }

    @Before("dataScopePointCut()")
    public void doBefore(JoinPoint point) throws Throwable
    {
        handleDataScope(point);
    }

    protected void handleDataScope(final JoinPoint joinPoint)
    {
        // 获得注解
        DataScope controllerDataScope = getAnnotationLog(joinPoint);
        if (controllerDataScope == null)
        {
            return;
        }
        // 获取当前的用户
        User currentUser = ShiroUtils.getSysUser();       
        List<Dept> detps=new ArrayList<>();
        detps=deptService.findDeptsChildByDeptId(currentUser.getDept().getDeptId());//取得用户是是属于哪个部门,及下属部门
        Set<Long> deptSet=new HashSet<>();
        for (Dept dept : detps) {
            deptSet.add(dept.getDeptId());
        }       
        currentUser.setChildDetp(deptSet);//
        if (currentUser != null)
        {
            // 如果是超级管理员,则不过滤数据
            if (!currentUser.isAdmin())
            {
                dataScopeFilter(joinPoint, currentUser,controllerDataScope.tableAlias());
            }
        }
    }

    /**
     * 数据范围过滤
     *
     * @param joinPoint 切点
     * @param user 用户
     * @param alias 注解
     */
    public static void dataScopeFilter(JoinPoint joinPoint, User user,String alias)
    {
        List<Long> deptList=new ArrayList<>();
        List<Dept> depts=new ArrayList<>();
        for (Role role : user.getRoles())//一个用户可能对应多个角色,用user.getRoles()方法获取当前用户的角色所拥有角色,循环数据权限标示sys_role中的data_scope
        {                            
            String dataScope = role.getDataScope();//取得该角色的权限标示,自定义?本部门?部门及以下?
            if (DATA_SCOPE_ALL.equals(dataScope))
            {
                deptList = null;
                break;
            }
            else if (DATA_SCOPE_CUSTOM.equals(dataScope))
            {
                depts.addAll(role.getDepts());//自定义权限,如果为空,则没有过滤条件,就会导致全部的信息都查询出来,所以加ID为0L的部门
                if(depts==null||depts.isEmpty()) {
                    Dept dept=new Dept();
                    dept.setDeptId(0L);
                    depts.add(dept);
                }
            }
            else if (DATA_SCOPE_DEPT.equals(dataScope))
            {
                depts.add(user.getDept());
            }
            else if (DATA_SCOPE_DEPT_AND_CHILD.equals(dataScope))
            {                  
                for (Long l : user.getChildDetp()) {
                    Dept d=new Dept();
                    d.setDeptId(l);
                    depts.add(d);
                }                
            }
        }
        for (Dept d : depts) {
            deptList.add(d.getDeptId());
        }                          

        DSDetpList dsDetpList=(DSDetpList) joinPoint.getArgs()[0];
        dsDetpList.setDeptList(deptList);
    }
    
    

    

    /**
     * 是否存在注解,如果存在就获取
     */
    private DataScope getAnnotationLog(JoinPoint joinPoint)
    {
        Signature signature = joinPoint.getSignature();
        MethodSignature methodSignature = (MethodSignature) signature;
        Method method = methodSignature.getMethod();

        if (method != null)
        {
            return method.getAnnotation(DataScope.class);
        }
        return null;
    }
       
}

 

 

package com.zcjd.project.system.dept.service.impl;

import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import com.zcjd.common.constent.UserConstants;
import com.zcjd.common.exception.BusinessException;
import com.zcjd.common.utils.StringUtils;
import com.zcjd.common.utils.security.ShiroUtils;
import com.zcjd.common.utils.spring.SpringUtils;
import com.zcjd.framework.aspectj.lang.annotation.DSDetpList;
import com.zcjd.framework.aspectj.lang.annotation.DataScope;
import com.zcjd.framework.web.entity.Ztree;
import com.zcjd.project.system.dept.dto.qo.DeptQoPg;
import com.zcjd.project.system.dept.dto.qo.DeptQoSh;
import com.zcjd.project.system.dept.entity.Dept;
import com.zcjd.project.system.dept.repository.DeptJpaRepository;
import com.zcjd.project.system.dept.service.IDeptService;
import com.zcjd.project.system.role.entity.Role;

@Service
public class DeptServiceImpl implements IDeptService {
    
    
    /**
     * 正常
     */
    public static final String NORMAL = "0";

    /**
     * 已删除
     */
    public static final String DELETE = "2";
    

    @Autowired
    private DeptJpaRepository deptJpaRepository;

    //坑1:切入点的方法必须是public。
    @DataScope(tableAlias = "d")
    public List<Long> dataFilter(DSDetpList DSDlist){          
        return  DSDlist.getDeptList();
    }
        

    /**
     * 查询部门管理数据
     *
     * @param deptQo 部门信息查询条件
     * @return 所有部门信息
     */
    @Transactional
    @Override
    public List<Dept> findAll(DeptQoSh deptQoSh) {
        List<Long> deptList=SpringUtils.getAopProxy(this).dataFilter(new DSDetpList());

      //坑2:这样也是不执行的,必须用AOP代理方式
        //List<Long> deptList=dataFilter(new DSDetpList());
        deptQoSh.setPropertyName("orderNum");
        deptQoSh.setInDeptId(deptList);
        deptQoSh.setEqualDelFlag(NORMAL);
        return deptJpaRepository.findAll(deptQoSh);
    }
    

}

 

 

标签:zcjd,spring,system,project,dept,Aspect,注解,import,com
来源: https://blog.csdn.net/FengDaWoBaoShu/article/details/102737216

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有