• 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
  • 相关阅读:
    创建一个新的IDEA插件项目
    【Nginx】Nginx双机热备
    【C语言经典100题#4】判断三角形
    设计模式(12)状态模式
    YOLOv5改进实战 | 更换主干网络Backbone(四)之轻量化模型MobileNetV3
    【能效管理】安科瑞新能源充电桩收费运维管理云平台应用分析
    【万字解析、学习参考资料】MySQL数据库常见面试题
    vim习惯养成记
    【开源】基于Vue和SpringBoot的创意工坊双创管理系统
    小白也可以玩转CMake之常用必备
  • 原文地址:https://blog.csdn.net/weixin_47053123/article/details/133350931