一 准备工作

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. Javascript中的事件二

    <!------------------示例代码一---------------------><!DOCTYPE html PUBLIC "-//W3C//DTD XHTM ...

  2. luogu4777[模板]拓展中国剩余定理题解

    题目链接 https://www.luogu.org/problemnew/show/P4777 分析 扩展\(CRT\)就是解决模数不互质的情况,说是扩展\(CRT\),其实都是扩欧... 先来考虑 ...

  3. windows连接远程服务器报错'SSH' 不是内部或外部命令,也不是可运行的程序 或批处理文件 解决方案

    网上在windows下连接远程服务器的步骤如下: 1.打开cmd命令行窗口 2.输入cd ~/.ssh,进入c盘下的.ssh文件 3.输入“ssh root@远程服务器的ip地址”连接远程服务器, b ...

  4. docker 无法使用vi

    更新来源 apt-get update   安装vim apt-get install -y vim

  5. 【转】Fetch超时设置和终止请求

    原文链接:https://www.cnblogs.com/yfrs/p/fetch.html 1.基本使用 Fetch 是一个新的端获取资源的接口,用于替换笨重繁琐XMLHttpRequest.它有了 ...

  6. LLVM新建全局变量

    在LLVM中,有原生的AST Clone,却没有一个比较好的Stmt copy功能,基于Scout在LLVM上进行的修改,我们实现了自己的Stmt Clone功能. 要进行Stmt Clone,肯定需 ...

  7. 使用openSSL构造一个支持https的nodejs服务器

    首先通过下面的链接下载openSSL https://slproweb.com/products/Win32OpenSSL.html 下载完毕后,执行openssl进入交互式界面: 使用命令生成pri ...

  8. idea控制台乱码修改

    我的idea当前版本是2019.2.2 试了很多,只有这个有效果 工具类→HELP→Edit Custom VM OPtions中加 -Dfile.encoding=utf-8 然后重启IDEA 这个 ...

  9. 【Struts2】简介及入门

    一.概述 二.Struts2 快速入门程序 2.1 开发流程比较 2.2 引入依赖 2.2 创建jsp页面 2.3 在web.xml中配置前端控制器 2.4 创建struts.xml配置文件 2.4 ...

  10. select * 和 select 字段的速度对比

    拿WordPress的数据库做一个对比 SELECT ID,post_title, post_author FROM wp_posts ORDER BY ID LIMIT 100; OK, Time: ...