springMvc的Aop解析并修改参数

        在前后端接口开发过程中,我们常常需要对某些字段进行加解密。以下是使用Aop对接口的get参数做修改的过程:

自定义注解

AesMethod:只能用于方法

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface AesMethod {
}

AesParam:只用用户属性

@Retention(RetentionPolicy.RUNTIME)
@Target(value = ElementType.PARAMETER)
public @interface AesParam {
}

切面

这里必须使用环绕 @Around,这样才能使用转换后参数

@Aspect
@Component
public class AesParamAspect {

   @Around(value = "com.zsc.test.aop.AesParamAspect.aesPointCut()")
    public Object dealAesParam(ProceedingJoinPoint joinPoint) throws Throwable {

        Object[] args = joinPoint.getArgs();
        // 方法签名
        Signature signature = joinPoint.getSignature();
        // 获取代理对象的method方法
        Method proxyMethod = ((MethodSignature)signature).getMethod();
        Annotation[] methodAnnotations = proxyMethod.getDeclaredAnnotations();
        
        //是否是需要解密的方法
        boolean isContainTargetAnnotation = false;
        for(Annotation annotation : methodAnnotations){
            if(annotation.annotationType().getSimpleName().equals("AesParamMethod")){
                isContainTargetAnnotation = true;
            }
        }
        
        if(isContainTargetAnnotation) {
            // 获取目标对象上的method方法(防止有protected或private修饰的方法反射获取失败)
            Method targetMethod = joinPoint.getTarget().getClass().getDeclaredMethod(signature.getName(), proxyMethod.getParameterTypes());

            int num = -1;
            // 取出对应的注解 二维数组,第一维是方法的参数列表,第二维是参数上的注解列表
            Annotation[][] parameterAnnotations = targetMethod.getParameterAnnotations();
            for (Annotation[] annotations : parameterAnnotations) {
                // 获取参数索引位置
                num = num + 1;
                for (Annotation annotation : annotations) {
                    if (annotation.annotationType().getSimpleName().equals("AesParam")) {
                        // 需要做数据解密
                        if (ObjectUtil.isNotEmpty(args[num])) {
                            try {
                                //解密后的数据
                                args[num] += AesUtil.deal(args[num]);
                            } catch (Throwable e) {
                                e.printStackTrace();
                            }
                        }
                    }

                }
            }
        }
        return joinPoint.proceed(args);
    }

    /**
     * 切点
     */
    @Pointcut("execution(* com.zsc.test.controller..*.*(..)) && @annotation(com.zsc.test.annotations.AesMethod)")
    public void aesPointCut(){

    }

测试类

@RestController
@RequestMapping("test")
public class TestController {

    @AesMethod
    @GetMapping("/get")
    public String test(@AesParam @RequestParam("name") String name){
        LOGGER.info("name: {}", name);
        return name;
    }
}

相关推荐

  1. springMvcAop解析修改参数

    2024-01-17 08:36:07       36 阅读
  2. Ubuntu修改MySQLtmpdir参数失败解决方法

    2024-01-17 08:36:07       11 阅读
  3. springMVC获取请求参数方式

    2024-01-17 08:36:07       42 阅读
  4. SpringMVC源码分析(八)--参数解析

    2024-01-17 08:36:07       21 阅读
  5. SpringMVC源码分析(六)--参数名称解析

    2024-01-17 08:36:07       19 阅读
  6. 哈希链接修改参数返回

    2024-01-17 08:36:07       36 阅读
  7. js如何实现修改URL参数不刷新页面

    2024-01-17 08:36:07       14 阅读

最近更新

  1. TCP协议是安全的吗?

    2024-01-17 08:36:07       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-17 08:36:07       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-17 08:36:07       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-17 08:36:07       20 阅读

热门阅读

  1. Python 3 中如何使用 format 方法格式化字符串

    2024-01-17 08:36:07       38 阅读
  2. 高并发场景下底层账务优化方案

    2024-01-17 08:36:07       34 阅读
  3. **FutureTask应用&源码分析**(二)

    2024-01-17 08:36:07       33 阅读
  4. 机器学习:简要介绍及应用案例

    2024-01-17 08:36:07       36 阅读
  5. #Day13 C基础(指针数组、函数)

    2024-01-17 08:36:07       29 阅读
  6. 如何在原型中实现继承和多态

    2024-01-17 08:36:07       32 阅读
  7. 【Qt5】QString的成员函数arg

    2024-01-17 08:36:07       40 阅读
  8. vue 开发规范

    2024-01-17 08:36:07       34 阅读
  9. 【vue】-

    2024-01-17 08:36:07       35 阅读
  10. 二级C语言备考7

    2024-01-17 08:36:07       30 阅读
  11. CentOS中如何让新建用户拥有root权限

    2024-01-17 08:36:07       30 阅读