spring拦截器和注解处理日志操作
整体思想:通过拦截器拦截所有的请求,处理含有自定义注解的方法,通过request得到需要的参数。
拦截器代码:
package com.zktx.platform.log2; import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.List; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView; import com.zktx.platform.log.LogLevel;
import com.zktx.platform.shiro.SecurityConstants; public class LogInterceptor implements HandlerInterceptor { /**
* preHandle方法是进行处理器拦截用的,顾名思义,该方法将在Controller处理之前进行调用,
* SpringMVC中的Interceptor拦截器是链式的,可以同时存在
* 多个Interceptor,然后SpringMVC会根据声明的前后顺序一个接一个的执行
* ,而且所有的Interceptor中的preHandle方法都会在
* Controller方法调用之前调用。SpringMVC的这种Interceptor链式结构也是可以进行中断的
* ,这种中断方式是令preHandle的返 回值为false,当preHandle的返回值为false的时候整个请求就结束了。
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
// LogUtil.putRequest(request);
return true;
} /**
* 这个方法只会在当前这个Interceptor的preHandle方法返回值为true的时候才会执行。postHandle是进行处理器拦截用的,
* 它的执行时间是在处理器进行处理之
* 后,也就是在Controller的方法调用之后执行,但是它会在DispatcherServlet进行视图的渲染之前执行
* ,也就是说在这个方法中你可以对ModelAndView进行操
* 作。这个方法的链式结构跟正常访问的方向是相反的,也就是说先声明的Interceptor拦截器该方法反而会后调用
* ,这跟Struts2里面的拦截器的执行过程有点像,
* 只是Struts2里面的intercept方法中要手动的调用ActionInvocation的invoke方法
* ,Struts2中调用ActionInvocation的invoke方法就是调用下一个Interceptor
* 或者是调用action,然后要在Interceptor之前调用的内容都写在调用invoke之前
* ,要在Interceptor之后调用的内容都写在调用invoke方法之后。
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
if (!(handler instanceof HandlerMethod)) {
return;
}
final HandlerMethod handlerMethod = (HandlerMethod) handler;
// 通过handler获取请求的方法
Method method = handlerMethod.getMethod();
// 放回自定义注解
final SystemControllerLog log = method.getAnnotation(SystemControllerLog.class);
// 如果方法包含此注解,处理数据
if (log != null) {
String description = log.description();
LogLevel level = log.level();
// SecurityConstants.LOG_ARGUMENTS为常量字符串
List<String> list = (List<String>) request.getAttribute(SecurityConstants.LOG_ARGUMENTS);
// MessageFormat为一个字符串格式化类:字符串中的{0}{1}{2} 与数组的[1,2,3]对应
MessageFormat mFormat = new MessageFormat(description);
String result = mFormat.format(list.toArray());
System.out.println("result:" + result + ",level:" + level);
} } /**
* 该方法也是需要当前对应的Interceptor的preHandle方法的返回值为true时才会执行。该方法将在整个请求完成之后,
* 也就是DispatcherServlet渲染了视图执行,
* 这个方法的主要作用是用于清理资源的,当然这个方法也只能在当前这个Interceptor的preHandle方法的返回值为true时才会执行。
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
// LogUtil.removeRequest(); } }
自定义注解:
package com.zktx.platform.log2; /**
* 自定义注解,拦截Controller
*/
import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target; import com.zktx.platform.log.LogLevel; @Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.PARAMETER })
@Documented
public @interface SystemControllerLog {
/**
* 日志信息
*
* @return
*/
String description() default ""; /**
*
* 日志记录等级
*
* @return
*/
LogLevel level() default LogLevel.TRACE;
}
日志级别等级:
package com.zktx.platform.log; /**
* 值越大,等级越高。
*
*/
public enum LogLevel { TRACE("TRACE"), DEBUG("DEBUG"), INFO("INFO"), WARN("WARN"), ERROR("ERROR"); private String value; LogLevel(String value) {
this.value = value;
} public String value() {
return this.value;
}
}
调用方式:
// 删除
@RequestMapping("/delete")
@SystemControllerLog(description = "删除了{0}用户")
// 定义的字符串
public @ResponseBody String deleteByPrimaryKey(Integer id, HttpServletRequest request) {
try {
userService.deleteByPrimaryKey(id);
List<String> list = new ArrayList<String>();
// 添加需要的参数
list.add(id + "");
request.setAttribute(SecurityConstants.LOG_ARGUMENTS, list);
} catch (Exception e) {
e.printStackTrace();
return "error";
} return "success";
}
最后,spring配置文件中注册拦截器
<!-- 自定义拦截器 -->
<mvc:interceptors> <mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.zktx.platform.log2.LogInterceptor"></bean>
</mvc:interceptor>
</mvc:interceptors>
spring拦截器和注解处理日志操作的更多相关文章
- 使用spring拦截器实现日志管理
使用HandlerInterceptor拦截器,可以拦截请求,实现通用的日志管理操作 一.添加拦截器类 在"src/main/java"代码文件夹的"org.xs.dem ...
- 运用Spring Aop,一个注解实现日志记录
运用Spring Aop,一个注解实现日志记录 1. 介绍 我们都知道Spring框架的两大特性分别是 IOC (控制反转)和 AOP (面向切面),这个是每一个Spring学习视频里面一开始都会提到 ...
- Spring拦截器中通过request获取到该请求对应Controller中的method对象
背景:项目使用Spring 3.1.0.RELEASE,从dao到Controller层全部是基于注解配置.我的需求是想在自定义的Spring拦截器中通过request获取到该请求对应于Control ...
- spring拦截器中修改响应消息头
问题描述 前后端分离的项目,前端使用Vue,后端使用Spring MVC. 显然,需要解决浏览器跨域访问数据限制的问题,在此使用CROS协议解决. 由于该项目我在中期加入的,主要负责集成shiro框架 ...
- Spring拦截器和过滤器
什么是拦截器 拦截器(Interceptor): 用于在某个方法被访问之前进行拦截,然后在方法执行之前或之后加入某些操作,其实就是AOP的一种实现策略.它通过动态拦截Action调用的对象,允许开发者 ...
- Spring 拦截器——HandlerInterceptor
采用Spring拦截器的方式进行业务处理.HandlerInterceptor拦截器常见的用途有: 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等. 2 ...
- Spring 拦截器实现+后台原理(HandlerInterceptor)
过滤器跟拦截器的区别 spring mvc的拦截器是只拦截controller而不拦截jsp,html 页面文件的.这就用到过滤器filter了,filter是在servlet前执行的,你也可以理解成 ...
- spring 拦截器简介
spring 拦截器简介 常见应用场景 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等.2.权限检查:如登录检测,进入处理器检测检测是否登录,如果没有直 ...
- spring boot 使用拦截器,注解 实现 权限过滤
http://www.cnblogs.com/zhangXingSheng/p/7744997.html spring boot 使用拦截器 实现 用户登录拦截 http://www.cnblogs. ...
随机推荐
- CF830A/831D Office Keys
思路: 问题的关键在于对钥匙按照位置排序之后,最终选择的n个钥匙一定是其中的一个连续的区间. 实现: #include <iostream> #include <cstdio> ...
- (转)50道JavaScript基础面试题(附答案)
https://segmentfault.com/a/1190000015288700 1 介绍JavaScript的基本数据类型 Number.String .Boolean .Null.Undef ...
- iis设置404错误页,返回500状态码
一般在II6下,设置自定义404错误页时,只需要在错误页中选择自定义的页面,做自己的404页面即可.但是在IIS7.0及以上时,设置完404错误页后,会发现状态码返回的是500,并且可能会引起页面乱码 ...
- 【译】x86程序员手册19-6.3.2数据访问的约束
6.3.2 Restricting Access to Data 数据访问的约束 To address operands in memory, an 80386 program must load ...
- java web项目和java项目的区别(看清IDE本质)
想必大家在使用MyEclipse时对这两个概念不去深究.只知道是Java EE类的基本都是Web项目,而Java应用程序就是Java项目.而且很多人都愿意使用MyEclipse作为开发工具,且不说大家 ...
- java_io学习_编码
package io; public class encodingDemo{ public static void main(String[] args) throws Exception{ // T ...
- Dispatch Queues 线程池
Dispatch Queues Dispatch queues are a C-based mechanism for executing custom tasks. A dispatch queue ...
- 【GC】
using 语句适用于清理单个非托管资源的情况,而多个非托管对象的清理最好以 try-finnaly 来实现,因为嵌套的 using 语句可能存在隐藏的 Bug.内层 using 块引发异常时,将不能 ...
- c++ map: 使用struct或者数组做value
Notice 如果是program中有两个map对象,可能你需要两个map iterator,但是注意两个iter object不能命名一样,可以分别为iter1, iter2 Example #in ...
- redis下载安装配置教程
参考 https://www.cnblogs.com/taostaryu/p/9481749.html 上面做完后, 打开客户端 $ redis-cli 以上命令将打开以下终端: redis 127. ...