http拦截器-HandlerInterceptor
简介
拦截器我想大家都并不陌生,最常用的登录拦截、权限校验、防重复提交、记录日志等等,总之可以去做很多的事情。
自定义拦截器HandlerInterceptorAdapter
我们以记录日志为例,介绍拦截器
1. preHandle:在业务处理器处理请求之前被调用,调用controller之前调用。预处理,可以进行编码、安全控制、权限校验等处理;
2. postHandle:在业务处理器处理请求执行完成后,生成视图之前执行。后处理(调用了Service并返回ModelAndView,但未进行页面渲染),有机会修改ModelAndView ;
3. afterCompletion:在DispatcherServlet完全处理完请求后被调用,可用于清理资源等。import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;
import com.alibaba.fastjson.JSON;
import com.example.demo.util.RespUtil; @Component
public class ReqInterceptor extends HandlerInterceptorAdapter { /**
* 在方法被调用前执行。在该方法中可以做类似校验的功能。如果返回true,
* 则继续运行下去。如果返回false,则中断执行。
*/
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
log.info("进入调单方法controller层之前");
String insuranceId = httpServletRequest.getParameter("insuranceId");
try {
FanHuaBack fanHuaBackOld = insureListService.getinsure(insuranceId);
String oldData = JSON.toJSONString(fanHuaBackOld);
httpServletRequest.setAttribute("dataBefore", oldData);
super.preHandle(httpServletRequest,httpServletResponse,o);
} catch (Exception e) {
log.error("车险投保信息查询异常", e.getMessage());
} finally {
return true;
} }
/**
* 在方法执行后调用。
*/
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView)
throws Exception {
log.info("进入调单方法controller层之后-----------日志记录开始");
InsuranceOperatLog insuranceOperatLog = new InsuranceOperatLog();//日志类
int status = 0;
try { //从request请求中获取信息
String requestURI = httpServletRequest.getRequestURI();
String insuranceId = httpServletRequest.getParameter("insuranceId");
String empNo = httpServletRequest.getParameter("empNo");
String empName = httpServletRequest.getParameter("empName");
int opeStatus = (int) httpServletRequest.getAttribute("status");
FanHuaBack fanHuaBackNew = insureListService.getinsure(insuranceId);
String newData = JSON.toJSONString(fanHuaBackNew);
String oldData = (String) httpServletRequest.getAttribute("dataBefore");
Map<String, String> map = new HashMap<>();
map.put("empNum", empNo);
map.put("empName", empName);
map.put("insuranceId", insuranceId);
String reqObj = JSON.toJSONString(map);
insuranceOperatLog.setInsuranceId(insuranceId);
insuranceOperatLog.setCreateTime(DateUtil.format(new Date(), DateUtil.Formatter.yyyyMMddHHmmss));
insuranceOperatLog.setDataBefore(oldData);
insuranceOperatLog.setDataAfter(newData); insuranceOperatLog.setReq_json(reqObj);
insuranceOperatLog.setReq_url(requestURI);
insuranceOperatLog.setOperatorType("调单");
insuranceOperatLog.setStatus(opeStatus == 1 ? "成功" : "失败");
SysUser sysUser = (SysUser) SecurityUtils.getSubject().getPrincipal();
if (sysUser != null) {
insuranceOperatLog.setOperatorName(sysUser.getLoginName());
insuranceOperatLog.setOperatorNo(sysUser.getUserNo());
}
insuranceOperatRepository.save(insuranceOperatLog);
super.postHandle(httpServletRequest,httpServletResponse,o,modelAndView);
} catch (Exception ex) {
log.error("调单操作异常日志保存异常{}", ex.getMessage());
}
log.info("调单操作日志记录结束{}"); }
/**
* 在整个请求处理完毕后进行回调,也就是说视图渲染完毕或者调用方已经拿到响应。
*/
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)
throws Exception {
System.out.println("在整个请求处理完毕后进行回调");
super.afterCompletion(request, response, handler, ex);
} }
http拦截器-HandlerInterceptor的更多相关文章
- SpringMVC 学习-拦截器 HandlerInterceptor 类
一.拦截器 HandlerInterceptor 类的作用 SpringMVC 的拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理. 二.怎么使用呢? 1. ...
- 014-Spring Boot web【三】拦截器HandlerInterceptor、异常处理页面,全局异常处理ControllerAdvice
一.拦截器HandlerInterceptor 1.1.HandlerInterceptor接口说明 preHandle,congtroller执行前,如果返回false请求终端 postHandle ...
- 【Java Web开发学习】Spring MVC 拦截器HandlerInterceptor
[Java Web开发学习]Spring MVC 拦截器HandlerInterceptor 转载:https://www.cnblogs.com/yangchongxing/p/9324119.ht ...
- springmvc(3)拦截器HandlerInterceptor源码的简单解析
其实拦截器就是我们的AOP编程.拦截器在我们的实际项目中实用性比较大的,比如:日志记录,权限过滤,身份验证,性能监控等等.下面就简单的来研究一下拦截器: public interface Handle ...
- springboot拦截器HandlerInterceptor详解
Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截器). HandlerInterceptor 的功能跟过滤器类似,但 ...
- 【Spring】7、拦截器HandlerInterceptor
处理器拦截器简介 Spring Web MVC的处理器拦截器(如无特殊说明,下文所说的拦截器即处理器拦截器) 类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理. 常见 ...
- spring mvc拦截器HandlerInterceptor
本文主要介绍springmvc中的拦截器,包括拦截器定义和的配置,然后演示了一个链式拦截的测试示例,最后通过一个登录认证的例子展示了拦截器的应用 拦截定义 定义拦截器,实现HandlerInterce ...
- Spring 拦截器——HandlerInterceptor
采用Spring拦截器的方式进行业务处理.HandlerInterceptor拦截器常见的用途有: 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等. 2 ...
- 22. Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
转:http://blog.csdn.net/linxingliang/article/details/52069495 上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API ...
- (22)Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】
上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API,与Spring 没什么关系. Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Sprin ...
随机推荐
- 模板 - 数学 - 数论 - Miller-Rabin算法
使用Fermat小定理(Fermat's little theorem)的原理进行测试,不满足 \(2^{n-1}\;\mod\;n\;=\;1\) 的n一定不是质数:如果满足的话则多半是质数,满足上 ...
- 和小哥哥一起刷洛谷(5) 图论之深度优先搜索DFS
关于dfs dfs伪代码: void dfs(s){ for(int i=0;i<s的出度;i++){ if(used[i]为真) continue; used[i]=1; dfs(i); } ...
- 大数据|linux权限chmod和chown
一.基础概念 1)rwx含义 示例如下 r:读权限read 4 w:写权限write 2 x:操作权限execute 1 -:无权限 2)drwxr - xr -x 与 - rw - r - - r ...
- C格式字符串转为二叉树
最近在LeetCode做题,二叉树出现错误时不好排查,于是自己写了一个函数,将前序遍历格式字串转换成二叉树. 形如 "AB#D##C##" 的字符串,"#"表示 ...
- Install LEDE on a BT Home Hub 5 / Plusnet One Router
Overview / Purpose of this guide These instructions are for aimed at users of Windows but a lot of t ...
- nodejs搜索包的流程
执行npm install后,如果打包成功,会在当前目录下生成一个node_modules的文件夹,里面存放着我们所需的依赖包. 当需要引用时,例如: var math = require(" ...
- nginx中的超时配置
nginx.conf配置文件中timeout超时时间设置 client_header_timeout 语法 client_header_timeout time默认值 60s上下文 http serv ...
- linux服务之dns
安装dig工具 [root@cu-app-107 ~]# cat /etc/redhat-releaseCentOS Linux release 7.5.1804 (Core) [root@cu-ap ...
- Flutter响应式编程 - RxDart
import 'package:flutter/material.dart'; import 'package:rxdart/rxdart.dart'; import 'dart:async'; cl ...
- libfacedetection 人臉識別
计算相似度,然后比对 QVector<cv::Point> vec_point1 = facedetect_frontal_surveillance4(face_img.clone()); ...