• AOP实现接口加密



    使用示例

     	@Secret//加入此注解,获取到的params是解密后的参数
        @PostMapping("upload")
        public JSONObject upload(@RequestBody JSONObject params){
            JSONObject upload = upload(params);//业务代码
            return upload;
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6

    POM文件

    		<!-- AOP切面依赖 -->
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
    
    • 1
    • 2
    • 3
    • 4
    • 5

    自定义注解

    import java.lang.annotation.ElementType;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    /**
     * @classname: Secret
     * @description: 自定义注解,用来标识请求类 或者方法是否使用AOP加密解密
     */
    @Target({ElementType.METHOD})              // 可以作用在类上和方法上
    @Retention(RetentionPolicy.RUNTIME)                               // 运行时起作用
    public @interface Secret {
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    切面方法

    import com.alibaba.fastjson.JSON;
    import com.alibaba.fastjson.JSONObject;
    import lombok.extern.slf4j.Slf4j;
    import org.aspectj.lang.ProceedingJoinPoint;
    import org.aspectj.lang.annotation.Around;
    import org.aspectj.lang.annotation.Aspect;
    import org.aspectj.lang.annotation.Pointcut;
    import org.springframework.beans.factory.annotation.Value;
    import org.springframework.stereotype.Component;
    import javax.annotation.Resource;
    import java.nio.charset.StandardCharsets;
    
    /**
     * @description: 切面加密解密
     **/
    @Aspect
    @Component
    @Slf4j
    public class SecretAOP {
    
        @Value("${security.enable}")
        private String securityEnable;
    
        @Resource
        private AppInfoDao appInfoDao;
    
        // 定义切点,使用了@Secret注解的类 或 使用了@Secret注解的方法
        @Pointcut("@annotation(com.face.annotation.Secret)")
        public void pointcut(){}
    
        // 环绕切面
        @Around("pointcut()")
        public JSONObject around(ProceedingJoinPoint point){
            WrapperResponse result = new WrapperResponse();
            // 获取被代理方法参数
            Object[] args = point.getArgs();
            //获取到请求参数解密
            //此处编写解密代码
           	Object proceed = point.proceed(args);
           	//加密返回参数
           	//此处编写加密代码
            return proceed ;
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
  • 相关阅读:
    企业内部通讯,WorkPlus助您打造高效沟通平台
    AUTOSAR C++14 编码指南(下)
    Spark集成hudi创建表报错
    一文讲明白K8S各核心架构组件
    Python 深拷贝和浅拷贝的区别
    双vip的MySQL高可用集群
    java的ArrayList && LinkedList的操作
    简化数据库操作:探索 Gorm 的约定优于配置原则
    oracle adg切换
    北斗提供关键技术支撑,车辆智能监管将迎来广阔发展前景
  • 原文地址:https://blog.csdn.net/weixin_47053123/article/details/133350931