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

3 篇文章 0 订阅
1 篇文章 0 订阅

最近参考开源后台管理系统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);
    }
    

}

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值