spring boot通过自定义注解和AOP拦截指定的请求
一 准备工作
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 { }
三 切面类
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拦截指定的请求的更多相关文章
- Spring Boot中自定义注解+AOP实现主备库切换
摘要: 本篇文章的场景是做调度中心和监控中心时的需求,后端使用TDDL实现分表分库,需求:实现关键业务的查询监控,当用Mybatis查询数据时需要从主库切换到备库或者直接连到备库上查询,从而减小主库的 ...
- Spring Boot实现自定义注解
在Spring Boot项目中可以使用AOP实现自定义注解,从而实现统一.侵入性小的自定义功能. 实现自定义注解的过程也比较简单,只需要3步,下面实现一个统一打印日志的自定义注解: 1. 引入AOP依 ...
- Spring Boot Web 自定义注解篇(注解很简单很好用)
自从spring 4.0 开放以后,可以添加很多新特性的注解了.使用系统定义好的注解可以大大方便的提高开发的效率. 下面我贴一段代码来讲解注解: 通过小小的注解我们支持了以下功能: 使 spring. ...
- SpringBoot自定义注解、AOP打印日志
前言 在SpringBoot中使用自定义注解.aop切面打印web请求日志.主要是想把controller的每个request请求日志收集起来,调用接口.执行时间.返回值这几个重要的信息存储到数据库里 ...
- spring自定义注解实现登陆拦截器
1.spring自定义注解实现登陆拦截器 原理:定义一个注解和一个拦截器,拦截器拦截所有方法请求,判断该方法有没有该注解.没有,放行:有,要进行验证.从而实现方法加注解就需要验证是否登陆. 2.自定义 ...
- Spring Boot 声明式事务结合相关拦截器
我这项目的读写分离方式在使用ThreadLocal实现的读写分离在迁移后的偶发错误里提了,我不再说一次了,这次是有要求读写分离与事务部分要完全脱离配置文件,程序员折腾了很久,于是我就查了一下,由于我还 ...
- 如何通过自定义注解实现AOP切点定义
面向切面编程(Aspect Oriented Programming, AOP)是面向对象编程(Object Oriented Programming,OOP)的强大补充,通过横切面注入的方式引入其他 ...
- (32)Spring Boot使用@SpringBootApplication注解,从零开始学Spring Boot
[来也匆匆,去也匆匆,在此留下您的脚印吧,转发点赞评论] 如果看了我之前的文章,这个节你就可以忽略了,这个是针对一些刚入门的选手存在的困惑进行写的一篇文章. 很多Spring Boot开发者总是使用 ...
- Spring Boot2 系列教程(十八)Spring Boot 中自定义 SpringMVC 配置
用过 Spring Boot 的小伙伴都知道,我们只需要在项目中引入 spring-boot-starter-web 依赖,SpringMVC 的一整套东西就会自动给我们配置好,但是,真实的项目环境比 ...
随机推荐
- kubernetes 集群内部访问外部的数据库endpoint
k8s访问集群外独立的服务最好的方式是采用Endpoint方式,以mysql服务为例: 创建mysql-service.yaml apiVersion: v1 kind: Service metada ...
- QT QListWidget去掉滚动条
1.去掉滚动条 设置样式 包含背景色等更改 setStyleSheet("QListWidget{color:gray;font-size:12px;background:#FAFAFD; ...
- elmentUI为table中的单元格添加事件
<el-main> <el-tabs v-model="curTab" type="card"> <!-- tab签 --> ...
- element-ui 中 el-table 相关操作
1.带checkbox 获取所有选择的行. this.$refs.multipleTable.selection 获取选中的单行 this.$refs.roleTable.store.states. ...
- Oracle使用基础
1.Oracle的基本概念: 数据库:存储数据的数据库,Oracle一般只有一个全局数据库 XE,ORCL. EX:Express Edition 速成版 ORCL:企业版 SID:SID是Syste ...
- python 获取导入模块的文件路径
接触到项目上有人写好的模块进行了导入,想查看模块的具体内容是如何实现的,需要找到模块的源文件. 本博文介绍两种查找模块文件路径方法: 方法一: #!/usr/bin/python # -*- codi ...
- Mac下使用Charles抓包https接口
1 官方网站下载,安装好Charles https://www.charlesproxy.com/download/ 2 安装ssl证书 3 信任证书 4 手机iPhone配置 ,获取证书url 5 ...
- springboot升级2.0 fastjson报错? 2.0以上应该怎么整合fastjson?
SpringBoot2.0如何集成fastjson?在网上查了一堆资料,但是各文章的说法不一,有些还是错的,可能只是简单测试一下就认为ok了,最后有没生效都不知道.恰逢公司项目需要将JackSon换成 ...
- ST3 C程序自动补全
参考: http://www.cnblogs.com/heleifz/p/3404600.html http://www.cnblogs.com/By-ruoyu/p/4687196.html htt ...
- 《设计模式之美》 <02>评判代码质量好坏的维度
如何评价代码质量的高低? 实际上,咱们平时嘴中常说的“好”和“烂”,是对代码质量的一种描述.“好”笼统地表示代码质量高,“烂”笼统地表示代码质量低.对于代码质量的描述,除了“好”“烂”这样比较简单粗暴 ...