一 准备工作

1.1 添加依赖

通过spring boot创建好工程后,添加如下依赖,不然工程中无法使用切面的注解,就无法对制定的方法进行拦截

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

1.2 工程目录结构

其中

MonitorRequest 自定义注解
RequestAspect 切面类
BookController 测试接口请求

二 自定义注解

package com.tinno.word.advice;

import java.lang.annotation.*;
import org.springframework.web.bind.annotation.Mapping; /**
* Created by xingle on 2019/5/15
*/
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Mapping
@Documented
public @interface MonitorRequest { }

注解的作用目标:
  @Target(ElementType.TYPE)                      // 接口、类、枚举、注解
  @Target(ElementType.FIELD)                     // 字段、枚举的常量
  @Target(ElementType.METHOD)                 // 方法
  @Target(ElementType.PARAMETER)            // 方法参数
  @Target(ElementType.CONSTRUCTOR)       // 构造函数
  @Target(ElementType.LOCAL_VARIABLE)   // 局部变量
  @Target(ElementType.ANNOTATION_TYPE) // 注解
  @Target(ElementType.PACKAGE)               // 包

三 切面类

 package com.tinno.word.aspect;

 import com.tinno.word.utils.CodeUtils;
import javax.servlet.http.HttpServletRequest;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes; /**
* Created by xingle on 2019/5/15
* 此类为一个切面类,主要作用就是对接口的请求进行拦截
* 拦截的方式,只需要在指定接口方法上面加上@MonitorRequest注解即可
*/ @Aspect
@Component
public class RequestAspect { private Logger logger = LoggerFactory.getLogger(this.getClass()); /**
* 环绕通知:
* 环绕通知非常强大,可以决定目标方法是否执行,什么时候执行,执行时是否需要替换方法参数,执行完毕是否需要替换返回值。
* 环绕通知第一个参数必须是org.aspectj.lang.ProceedingJoinPoint类型
*/
@Around(value = "@annotation(com.tinno.word.advice.MonitorRequest)")
public Object doBefore(ProceedingJoinPoint joinPoint) { //获取到请求的属性
ServletRequestAttributes attributes =
(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
//获取到请求对象
HttpServletRequest request = attributes.getRequest(); //获取请求的方法,是Get还是Post请求
logger.info("method=" + request.getMethod()); Object[] args = joinPoint.getArgs();
String de_input = "";
try {
logger.debug("请求参数 解密前:" + args[0].toString());
//这里具体解密方法不再列出
de_input = CodeUtils.decrypt(args[0].toString());
logger.debug("请求参数 解密后:" + de_input);
} catch (Exception e) {
e.printStackTrace();
}
args[0] = de_input; Object returnValue = null;
try {
//执行方法,以新的参数(如果不带args就是用原先的参数)
returnValue = joinPoint.proceed(args);
logger.debug("returnValue 加密前:" +returnValue.toString());
} catch (Throwable throwable) {
throwable.printStackTrace();
}
//执行完毕也可以替换目标方法返回值,这里加密替换,也可以不加密原样返回
returnValue = CodeUtils.encrypt(returnValue.toString());
logger.debug("returnValue 加密后:" +returnValue.toString());
return returnValue; } }

四 Controller类

   @MonitorRequest
@RequestMapping(value = "/getBookVersionLs", method = {RequestMethod.GET,RequestMethod.POST},produces={"text/html;charset=UTF-8;","application/json;"})
public String getBookVersionLs(@RequestBody String json){
logger.info("Controller-getBookVersionLs 版本选择书本列表,请求参数:"+json);
JSONObject jsonObject = JSONObject.parseObject(json);
String userId = jsonObject.getString("udid");
String device_id = jsonObject.getString("device_id");
Map<String, Object> data = JsonUtil.jsonText2JsonMap(jsonObject.getString("data")); String rand = data.get("rand").toString();
String version = data.get("version").toString(); if (isEmpty(userId) || isEmpty(device_id)){
ResultBody result = ResultBody.toFailure(ErrorInfoEnum.PARAMS_NO_COMPLETE.getStatus(),ErrorInfoEnum.PARAMS_NO_COMPLETE.getDest());
return new Gson().toJson(result);
}
List<AllBookVersion> allList = bookService.getAllBookVersionLs();
AllBookVersion first = allList.get(0);
if (isEmpty(rand) && isEmpty(version)){
rand = first.getRand();
version = first.getVersionLs().get(0);
}
List<BookVersion> bookList = bookService.getBookVersionLs(device_id,rand,version); JSONObject obj = new JSONObject();
obj.put("bookList",bookList);
obj.put("allList",allList);
ResultBody resultBody = ResultBody.toSussess(obj,"");
return new Gson().toJson(resultBody);
}

四 结果

请求参数:

D1D329BF7D4C02CB97C29CB03DF243D79A280D3DD1C6B0EC89B353394409DE0C950A48E2809E6539DE3A641CBA6A89957296D9E7E1DD4906505C5ADFAF89C0AA0B585B1338A57F1CAD8ABE8538E74B75C7947C0B593E3C6DB66DB2CDE07305B671E4405FFA5701C44590D3DE4973174E7D8FB983E82768A0DE086534494CAA49

返回结果:

加密后的结果

spring boot通过自定义注解和AOP拦截指定的请求的更多相关文章

  1. Spring Boot中自定义注解+AOP实现主备库切换

    摘要: 本篇文章的场景是做调度中心和监控中心时的需求,后端使用TDDL实现分表分库,需求:实现关键业务的查询监控,当用Mybatis查询数据时需要从主库切换到备库或者直接连到备库上查询,从而减小主库的 ...

  2. Spring Boot实现自定义注解

    在Spring Boot项目中可以使用AOP实现自定义注解,从而实现统一.侵入性小的自定义功能. 实现自定义注解的过程也比较简单,只需要3步,下面实现一个统一打印日志的自定义注解: 1. 引入AOP依 ...

  3. Spring Boot Web 自定义注解篇(注解很简单很好用)

    自从spring 4.0 开放以后,可以添加很多新特性的注解了.使用系统定义好的注解可以大大方便的提高开发的效率. 下面我贴一段代码来讲解注解: 通过小小的注解我们支持了以下功能: 使 spring. ...

  4. SpringBoot自定义注解、AOP打印日志

    前言 在SpringBoot中使用自定义注解.aop切面打印web请求日志.主要是想把controller的每个request请求日志收集起来,调用接口.执行时间.返回值这几个重要的信息存储到数据库里 ...

  5. spring自定义注解实现登陆拦截器

    1.spring自定义注解实现登陆拦截器 原理:定义一个注解和一个拦截器,拦截器拦截所有方法请求,判断该方法有没有该注解.没有,放行:有,要进行验证.从而实现方法加注解就需要验证是否登陆. 2.自定义 ...

  6. Spring Boot 声明式事务结合相关拦截器

    我这项目的读写分离方式在使用ThreadLocal实现的读写分离在迁移后的偶发错误里提了,我不再说一次了,这次是有要求读写分离与事务部分要完全脱离配置文件,程序员折腾了很久,于是我就查了一下,由于我还 ...

  7. 如何通过自定义注解实现AOP切点定义

    面向切面编程(Aspect Oriented Programming, AOP)是面向对象编程(Object Oriented Programming,OOP)的强大补充,通过横切面注入的方式引入其他 ...

  8. (32)Spring Boot使用@SpringBootApplication注解,从零开始学Spring Boot

    [来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论] 如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章. 很多Spring Boot开发者总是使用 ...

  9. Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置

    用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...

随机推荐

  1. Socket的神秘面纱

    Tcp/IP协议是目前世界上使用最为广泛的协议,是以Tcp/IP为基础多个层次上的协议的集合.也称Tcp/IP协议族或Tcp/IP协议栈. TCP: Transmission Control Prot ...

  2. php 如何将image图片转化为字符串(GD库操作及imagick两种实现方式)

    前两天研究php中的 imagick 扩展的时候,突发奇想实现的一个小功能感觉挺有意思,在这里记录一下: 将一张image图片转化为字符串的形式,先上一张效果图.(运行笔记中的代码需要先安装 php_ ...

  3. Spring之Redis访问(Spring-data-redis)

    Spring-data-redis,是spring-data框架中,比较常用的,基于key-value键值对的数据持久层框架.Spring-data-redis,是一个基于Template模板开发的数 ...

  4. (十四)Android NDK混淆

    1.ollvm下载编译 我的是macbook环境. 参考obfuscator官网:https://github.com/obfuscator-llvm/obfuscator/wiki 执行下面的命令下 ...

  5. Girls Like You--Maroon 5

    Girls Like You Spent 24 hours, I need more hours with you (24小时过去 还想和你 相处更久) You spent the weekend g ...

  6. django 发送邮件功能

    setting.py # 邮件配置 EMAIL_BACKEND = 'django.core.mail.backends.smtp.EmailBackend' EMAIL_HOST = 'smtp.e ...

  7. Spring 注解无效 空指针异常

    对于Java开发,现在Spring已成为一种生态,使用Spring成为一种享受,Spring的使用让开发变得更加便捷. 而Spring好用归好用,若不清楚里面的工作原理,使用的时候难免会踩到一些坑. ...

  8. linux (wsl) npm 无法安装包

    错误代码如下 request to https://registry.npm.taobao.org/yrm failed, reason: getaddrinfo EAI_AGAIN registry ...

  9. SpringBoot + Maven + Hibernate ( 简单实现CRUD功能 )

    工具:idea.mariadb数据库 创建一个项目 ( student ) ........(使用idea创建一个springboot项目,这里我就不多说了) Maven 中的依赖 <?xml ...

  10. python中else与finally的总结

    1.else的用法 对try...except的补充: else子句的使用比在子句中添加其他代码更好,try因为它避免了意外捕获由try... except语句保护的代码未引发的异常. for arg ...