AOP终止原方法的执行,并修改参数

AOP终止原方法的执行,并修改参数

定义注解

package com.cyboil.Annotation;


import java.lang.annotation.*;


@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface CheckAuthByCardNo {

}

定义切面

package com.cyboil.Annotation;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.cyboil.common.baseframe.core.plm.beans.ResultBean;
import com.cyboil.mapper.CustSelf_ICCustMapper;
import com.cyboil.model.CustSelf_ICCRUserCard;
import com.cyboil.service.CustSelf_CustService;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
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.core.LocalVariableTableParameterNameDiscoverer;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;
import org.springframework.stereotype.Component;

import java.lang.annotation.Annotation;
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import java.util.Arrays;

@Aspect
@Component
@Slf4j
public class CheckAuthByCardNoAspect {


    @Autowired
    private CustSelf_ICCustMapper _ICCustMapper;
    @Autowired
    private CustSelf_CustService custService;

//    @Before("@annotation(com.cyboil.Annotation.CheckAuthByCardNo)")
    @SneakyThrows
    @Around("@annotation(com.cyboil.Annotation.CheckAuthByCardNo)")
    public Object Around(ProceedingJoinPoint joinPoint) {

        /*AOP终止原方法的执行*/
//        ResultBean resultBean = new ResultBean();
//        //获取接口的参数
//        Object[] args = joinPoint.getArgs();
//        String argsJsonStr = JSONObject.toJSONString(args);
//        JSONArray argsJson = JSONObject.parseObject(argsJsonStr, JSONArray.class);
//        log.info(argsJson.toJSONString());
//        String cardSerialNo = null;
//        for (int i = 0; i < argsJson.size(); i++) {
//            JSONObject jsonObject = argsJson.getJSONObject(i);
//            if (jsonObject.containsKey("Data") &&  jsonObject.getJSONObject("Data").containsKey("Param")) {
//                cardSerialNo = jsonObject.getJSONObject("Data").getJSONObject("Param").getString("CardSerialNo");
//            }
//        }
//        CustSelf_ICCRUserCard userCard = _ICCustMapper.getCustNo(cardSerialNo, null);
//        if (userCard != null) {
//            /*校验成功,继续执行方法*/
//            return joinPoint.proceed();
//        } else {
//            /*校验失败,可以返回你失败的信息也可以直接抛出校验失败的异常*/
//            resultBean.setResult(1);
//            resultBean.setMsg("未授权");
//            return resultBean;
//        }





        /*修改接口参数*/
        try {
            Object[] args = joinPoint.getArgs();
            for (int i = 0; i < args.length; i++) {
                JSONObject argJson = (JSONObject) args[0];
                String token = argJson.getString("Token");
                argJson.put("Token", "------");
            }
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("Data", new JSONObject());

            Object[] objects = new Object[1];
            objects[0] = jsonObject;
            /*objects是Controller接收参数*/
            Object ret = joinPoint.proceed(objects);
            /*ret是CON*/
            return ret;
        } catch (Throwable e) {
            e.printStackTrace();
        }
        return null;



    }



    @SneakyThrows
//    @Before("@annotation(com.cyboil.Annotation.CheckAuthByCardNo)")
    public void Before(JoinPoint point, CheckAuthByCardNo cleanerTimeCheck) {
    }


}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值