首页 > 其他分享 >annotation

annotation

时间:2022-09-30 15:58:22浏览次数:40  
标签:Object BaseCreateByAndUpdateBy args joinPoint import annotation

package com.springsecuritydemo.aspect;

import com.springsecuritydemo.domain.base.BaseCreateByAndUpdateBy;
import com.springsecuritydemo.service.security.impl.UserDetailsImpl;
import com.springsecuritydemo.util.MyReflectionUtils;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Component;
import org.springframework.util.ReflectionUtils;

import java.lang.reflect.Field;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;

//Arbor 2022/9/29
@Aspect
@Slf4j
@Component
public class MethodExporterAspect {

    private BaseCreateByAndUpdateBy baseCreateByAndUpdateBy;

    /**
     * 获取登录用户ID
     * @return
     */
    public String getUserDetailsImplId(){
        UserDetailsImpl principal = (UserDetailsImpl)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
        return principal.getSysAdmin().id;
    }

    public static String getActualType(Object o,int index) {
        Type clazz = o.getClass().getGenericSuperclass();
        ParameterizedType pt = (ParameterizedType)clazz;
        return pt.getActualTypeArguments()[index].toString();
    }

    /**
     * 方法参数校验
     * 1,必须继承BaseCreateByAndUpdateBy
     * 2,必须是单个参数(JPA已经检验)
     * 3,必须是继承BaseCreateByAndUpdateBy的对象
     * 或实现Iterable接口的并泛型对象为<继承BaseCreateByAndUpdateBy的对象(单个,JPA已经检验)>
     */
    public void methodArgsCheckout(ProceedingJoinPoint joinPoint) throws InstantiationException, IllegalAccessException {
        Object[] args = joinPoint.getArgs();
        Class<?> aClass = args[0].getClass();

    }


    /**
     * 数据设置修改人添加人
     */
    @Around("@annotation(com.springsecuritydemo.annotation.CreateBy)")
    public Object methodExporterDataCreateBy (ProceedingJoinPoint joinPoint) throws Throwable {
        methodArgsCheckout(joinPoint);
        Object[] args = joinPoint.getArgs();
        if(args.length>0) {
            throw new RuntimeException("Aspect传参异常");
        }
        Object object = args[0];
        Class<?> aClass = args[0].getClass();
        if(!MyReflectionUtils.IsExtends(aClass,BaseCreateByAndUpdateBy.class)){
            throw new RuntimeException("未继承BaseCreateByAndUpdateBy");
        }
        Field createBy = aClass.getField("createBy");
        ReflectionUtils.makeAccessible(createBy);
        createBy.set(object,getUserDetailsImplId());
        Object proceed = joinPoint.proceed();
        return proceed;
    }


    /**
     * 数据设置修改人添加人
     */
    @Around("@annotation(com.springsecuritydemo.annotation.UpdateBy)")
    public Object methodExporterDataUpdateBy (ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] args = joinPoint.getArgs();
        if(args.length>0) {
            throw new RuntimeException("Aspect传参异常");
        }
        Object object = args[0];
        Class<?> aClass = args[0].getClass();
        if(!MyReflectionUtils.IsExtends(aClass,BaseCreateByAndUpdateBy.class)){
            throw new RuntimeException("未继承BaseCreateByAndUpdateBy");
        }
        Field updateBy = aClass.getField("updateBy");
        ReflectionUtils.makeAccessible(updateBy);
        updateBy.set(object,getUserDetailsImplId());
        Object proceed = joinPoint.proceed();
        return proceed;
    }

}

标签:Object,BaseCreateByAndUpdateBy,args,joinPoint,import,annotation
From: https://www.cnblogs.com/Arborblog/p/16745146.html

相关文章