SpringBoot整合Aop

1.依赖

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-aop</artifactId>
    </dependency>
package com.jczx.aoptest;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * @author 于振华
 * @create 2020-01-19 18:24
 * 目标类
 */
@Aspect//声明当前组件是一个切面组件
@Component//声明这是一个组件
@Order(1)/*切面执行的顺序*/
public class MayAspect {
    /**
     * 1.定义切入点,以供后面使用
     * execution:在方法执行时触发
     * public :省略 作用域
     * *:返回值
     * com.jczx.aoptest.AspectT:切入点所对应的的方法所属类'
     * *(..):任意方法
     * (..):任意参数
     */
   //注解 @Pointcut("@annotation(com.example.demo.aop.History)")  
   @Pointcut("execution(* com.jczx.aoptest.AspectT.*(..))")
    public void log04(){

    }

    /***
     * 定义额外功能
     */
    @Before("log04()")
    public void save(JoinPoint joinPoint) {
        String name = joinPoint.getSignature().getName();
        System.out.println("前绕你好"+name);
    }

   @After("log04()")
    public void update1() {
        System.out.println("后绕更新");
    }

    @Around("log04()")
    public void around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
        System.out.println("开始环绕");
        proceedingJoinPoint.proceed();
        System.out.println("尾部环绕");
    }



}

package com.charsming.common.annotation;

import java.lang.annotation.*;

/**
 * 数据权限过滤注解
 *
 * @author ruoyi
 */
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface DataScope {

    /**
     * 字段名
     */
    public String fieldName() default "";

    /**
     * 权限类别
     */
    public String authType() default "";


}

package com.charsming.common.aspect;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.charsming.common.annotation.DataScope;
import com.charsming.common.utils.ShiroUtils;
import com.charsming.common.utils.StringUtils;
import com.charsming.system.domain.UserDO;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * 数据过滤处理
 *
 * @author ruoyi
 */
@Aspect
@Component
public class DataScopeAspect {


    /**
     * 数据权限过滤关键字
     */
    public static final String DATA_SCOPE = "dataScope";
    //党组织 数据权限
    public static final String AUTH_TYPE_D_TEAM = "1";

    @Before("@annotation(controllerDataScope)")
    public void doBefore(JoinPoint point, DataScope controllerDataScope) throws Throwable {
        clearDataScope(point);
        handleDataScope(point, controllerDataScope);
    }

    protected void handleDataScope(final JoinPoint joinPoint, DataScope controllerDataScope) {
        // 获取当前的用户

        UserDO loginUser = ShiroUtils.getUser();

        dataScopeFilter(joinPoint, loginUser, controllerDataScope.fieldName(), controllerDataScope.authType());

    }

    /**
     * 数据范围过滤
     *
     * @param joinPoint 切点
     * @param user      用户
     * @param fieldName 字段名
     * @param
     */
    public static void dataScopeFilter(JoinPoint joinPoint, UserDO user, String fieldName, String authType) {
        StringBuilder sqlString = new StringBuilder();

        //用户数据权限, auth 如果没有赋予权限 默认 是 in (" ") 查不到任何数据

        StringJoiner authJoiner = new StringJoiner("','", "'", "'");
        //权限数据
        String auth = "";
        //党组织权限
        // TODO: 2023-04-06  拿到党组织
        if (AUTH_TYPE_D_TEAM.equals(authType)) {
            auth = "2";
        } else {
            auth = user.getAuth();//社区小区权限

        }
        String[] authSplit = auth.split(",");
        for (int i = 0; i < authSplit.length; i++) {
            authJoiner.add(authSplit[i]);
        }
        //拼接查询条件
        sqlString.append(StringUtils.format(
                "{} IN ({}) ", fieldName,
                authJoiner.toString()));
        if (StrUtil.isNotBlank(sqlString.toString())) {
            Object params = joinPoint.getArgs()[0];
            if (ObjectUtil.isNotNull(params) && params instanceof Map) {
                HashMap baseEntity = (HashMap) params;
                baseEntity.put(DATA_SCOPE, " AND " + sqlString);
            }
        }


    }

    /**
     * 拼接权限sql前先清空params.dataScope参数防止注入
     */
    private void clearDataScope(final JoinPoint joinPoint) {
        Object params = joinPoint.getArgs()[0];
        if (StringUtils.isNotNull(params) && params instanceof Map) {
            HashMap baseEntity = (HashMap) params;
            baseEntity.put(DATA_SCOPE, "");
        }
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

yzhSWJ

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值