切面注入参数

注解:配合切面使用

package com.fcar.kj.manager.bsj.annotation;

import java.lang.annotation.*;

/**
 * 机构数据权限切面注解
 *
 * @date 2020/7/23 21:21
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface OrgDataScope {

}

切面代码:

package com.fcar.kj.manager.bsj.aspect;

import com.fcar.kj.bsj.common.def.checkorg.IOrgQueryVO;
import com.fcar.kj.common.entities.ShiroAccount;
import com.fcar.kj.common.entities.account.SysRole;
import com.fcar.kj.common.utils.StringUtils;
import com.fcar.kj.manager.bsj.annotation.OrgDataScope;
import com.fcar.kj.manager.dao.CheckOrgDao;
import org.apache.shiro.SecurityUtils;
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.stereotype.Component;

import java.lang.reflect.Method;

import static com.fcar.kj.bsj.common.constant.DataScopeConstant.DataScopeTypeEnum.*;

/**
 * 数据权限切面
 *
 * @date 2020-07-23 21:15
 **/
@Aspect
@Component
public class OrgDataScopeAspect {
    @Autowired
    private CheckOrgDao checkOrgDao;


    /**
     * 配置织入点
     */
    @Pointcut("@annotation(com.fcar.kj.manager.bsj.annotation.OrgDataScope)")
    public void dataScopePointCut() {
    }

//调用之前
    @Before("dataScopePointCut()")
    public void doBefore(JoinPoint point) {
        handleDataScope(point);
    }

    /**
     * 处理数据权限
     *
     * @param joinPoint
     */
    protected void handleDataScope(final JoinPoint joinPoint) {
        // 获得注解
        OrgDataScope controllerDataScope = getAnnotationLog(joinPoint);
        if (controllerDataScope == null) {
            return;
        }
        // 获取当前的用户
        ShiroAccount shiroAccount = (ShiroAccount) SecurityUtils.getSubject().getSession().getAttribute("userLogin");

        // 如果是超级管理员,则不过滤数据
        if (shiroAccount != null && !shiroAccount.isAdminFlag()) {
            dataScopeFilter(joinPoint, shiroAccount);
        }
    }

    /**
     * 数据权限范围过滤
     *
     * @param joinPoint
     * @param user
     */
    public void dataScopeFilter(JoinPoint joinPoint, ShiroAccount user) {
        StringBuilder sqlString = new StringBuilder();
        //获取接口的第一个参数进行牵制转换 然后复制参数进去(难点)
        //实体类
        IOrgQueryVO iOrgQueryVO = (IOrgQueryVO) joinPoint.getArgs()[0];
        String orgIdStr = iOrgQueryVO.getOrgIdStr();
        if (StringUtils.isNotBlank(orgIdStr)) {
            return;
        }

        Integer orgId = user.getOrganizationid();
        for (SysRole role : user.getSysRoleList()) {
            Integer dataScopeType = role.getDataScopeType();
            if (DATA_SCOPE_ALL.getType() == dataScopeType) {
                return;
            } else if (DATA_SCOPE_ORG.getType() == dataScopeType) {
                orgIdStr = sqlString.append(orgId).toString();
                // TODO 权限校验
            } else if (DATA_SCOPE_ORG_AND_CHILD.getType() == dataScopeType) {
                orgIdStr = checkOrgDao.getCurSubCheckOrgId(orgId);
                // TODO 权限校验
            }
        }

        if (StringUtils.isNotBlank(orgIdStr)) {
            iOrgQueryVO.setOrgIdStr(orgIdStr);
        }
    }

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

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

你可能感兴趣的:(切面注入参数)