美文网首页
springboot-aop实现

springboot-aop实现

作者: 小吖么小一郎 | 来源:发表于2019-11-15 17:22 被阅读0次

    引入依赖

            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-aop</artifactId>
            </dependency>
    

    创建注解

    @Target(ElementType.METHOD)
    @Retention(RetentionPolicy.RUNTIME)
    public @interface Auth {
        String value() default "";
    }
    

    编写测试类

    @RestController
    @RequestMapping("/aopTest")
    public class AopTest {
        //使用注解
        @Auth(value = "验证用户名和密码")
        @PostMapping("/login")
        public Object loginTest(@RequestBody Login login){
            return login;
        }
    }
    
    public class Login implements Serializable{
        private String username;
        private String password;
    
        public String getUsername() {
            return username;
        }
    
        public void setUsername(String username) {
            this.username = username;
        }
    
        public String getPassword() {
            return password;
        }
    
        public void setPassword(String password) {
            this.password = password;
        }
    }
    

    aop实现

    @Aspect
    @Component
    public class LogAspect {
        private static final Logger log = LoggerFactory.getLogger(LogAspect.class);
    
        @Pointcut("@annotation(auth)")
        public void annotationPointcut(Auth auth){}
    
        @Around("annotationPointcut(auth)")
        public Object around(ProceedingJoinPoint pjp, Auth auth) throws Throwable {
            String value = auth.value();    // 获取AopTest.getString()上@Auth注解的value值
            //获取请求方法
            Signature sig = pjp.getSignature();
            String method = pjp.getTarget().getClass().getName() + "." + sig.getName();
            //获取请求的参数
            Object[] args = pjp.getArgs();
            //json转换
            String params = JSONObject.toJSONString(args);  // 获取请求参数
            // 序列化为JSON字符串时,会自动带上[],需要首位去掉,否则会报错:
            // com.alibaba.fastjson.JSONException: syntax error, expect {, actual [, pos 0, fastjson-version 1.2.56
            params  = params.substring(1, params.length() - 1);
            Login login = JSON.parseObject(params, Login.class);    // 将Json字符串反序列化为JAVA对象
            String username = login.getUsername();
            String password = login.getPassword();
            log.info("username: " + username + "  password: " + password);
            JSONObject jsonObject = new JSONObject();
            jsonObject.put("code","201");
            //执行方法,获取返回结果
            Object result = pjp.proceed();
            return !username.equalsIgnoreCase("李四") ? jsonObject : result;
        }
    }
    

    测试

    {
        "username":"张三",
        "password":"123456"
    }
    
    {
        "username":"李四",
        "password":"123456"
    }
    

    相关文章

      网友评论

          本文标题:springboot-aop实现

          本文链接:https://www.haomeiwen.com/subject/fgpsictx.html