自定义注解切面,实现对方法的拦截

***自定义注解切面,实现对方法的拦截***

1. 自定义一个注解

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Desensitized {

    String[] functionCode() default {};

    String functionName() default "";
}

2. 对注解实现切面拦截

定义一个类:

@Slf4j
@Aspect
@Component
public class DesensitizedAspect {

	//定义切点,注解为Desensitized的都为切入点
    @Pointcut("@annotation(com.aexpec.elp.clientuser.util.annotation.Desensitized)")
    public void anyDesensitizedAspect() {
        //切点
    }

    @Around("anyDesensitizedAspect()")
    public Object isAllowed(ProceedingJoinPoint joinPoint) throws Throwable {
        Method method = ((MethodSignature) joinPoint.getSignature()).getMethod();
		
        Desensitized desensitized = AnnotationUtils.findAnnotation(method, Desensitized.class);
        if (desensitized != null) {
            //解密
            String[] strings = desensitized.functionCode();
            switch (MethodEnum.valueOf(strings[0])) {
                case USER:
                    return DecodeUtil.decodeUser((ClientUserModel) joinPoint.proceed());
                case CARD:
                    return DecodeUtil.decodeCard((ClientUserCardModel) joinPoint.proceed());
                case ALIPAY:
                    return DecodeUtil.decodeAlipayUser((AlipayAppUserModel) joinPoint.proceed());
                case HOLDER:
                    return DecodeUtil.decodeCardHolder((CardholderModel) joinPoint.proceed());
                case CARDS:
                    return DecodeUtil.decodeCardList((List<ClientUserCardModel>) joinPoint.proceed());
                default:
                    break;
            }

        }
        return null;
    }

    private enum MethodEnum {
        USER,
        CARD,
        ALIPAY,
        HOLDER,
        CARDS
    }
}

@Around(“anyDesensitizedAspect()”) 环绕通知:配合使用ProceedingJoinPoint joinPoint
通过joinPoint.proceed() 获取方法的返回值,再将其强转为需要的实体。

3. 对注解使用

在要拦截的方法前面添加代码

@Desensitized(functionCode = {"CARD"}, functionName = "卡解密")
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值