• 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
  • 相关阅读:
    Shell外壳的简易制作
    计算机毕业论文java毕业设计选题源代码基于SSM的会议室预约系统
    利用Nacos作为配置中心动态修改线程池
    基于遥感大数据的信息提取技术综述
    Android FFmpeg系列——C多线程使用
    Win11找不到组策略编辑器(gpedit.msc)解决
    一文掌握GitHub Actions基本概念与配置
    Linux-实操篇8-shell脚本编写
    对于程序员什么最重要
    零基础学前端(一) 前端开发环境:vscode、chrome 完成最基础的代码编写和运行
  • 原文地址:https://blog.csdn.net/weixin_47053123/article/details/133350931