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 ...
随机推荐
- kubernetes 1.14安装部署EFK日志收集系统
简单介绍: EFK 组合插件是k8s项目的一个日志解决方案,它包括三个组件:Elasticsearch, Fluentd, Kibana.相对于ELK这样的架构,k8s官方推行了EFK,可能Fluen ...
- html转图片/html2canvas的使用/星座测试/类似于损友圈的活动
https://try.fishqc.com/Activity/constellation ---成品 电脑上录的gif 有借鉴的链接,很多,下面这个还不错~先别看,尊重下我先~~~~ https:/ ...
- Leet Code 771.宝石与石头
Leet Code编程题 希望能从现在开始,有空就做一些题,自己的编程能力太差了. 771 宝石与石头 简单题 应该用集合来做 给定字符串J 代表石头中宝石的类型,和字符串 S代表你拥有的石头. S ...
- polarssl rsa & aes 加密与解密<转>
上周折腾加密与解密,用了openssl, crypto++, polarssl, cyassl, 说起真的让人很沮丧,只有openssl & polarssl两个库的RSA & AES ...
- form表单文件上传提交且接口回调显示提交成功
前端: <form method="post" enctype="multipart/form-data" id="formSubmit&quo ...
- keepalived vip removed with dhcp renewal【原创】
最近发现公司云平台服务器的vip有丢失的现象,查看keepalived日志 Jun :: lb1 dhclient: DHCPREQUEST of (xid=0x6deab016) Jun :: lb ...
- Apollo的基本使用及常见问题
1. 创建项目 在创建项目页面中填写相关项目信息,最后点击提交即可创建项目. 注意:应用Id必须唯一并且与客户配置的app.id一致. 2. 发布 进入对应项目可通过文本(批量)或者表格模式添加配置, ...
- elementUI el-table合并单元格
合并单元格,如果id列值一致,则合并. <el-table :data="tableData6" :span-method="objectSpanMethod&qu ...
- Opencv拉普拉斯算子做图像增强
Opencv拉普拉斯算子——图像增强 #include <iostream> #include <opencv2/opencv.hpp> using namespace std ...
- kinova roslaunch j2s7s300_moveit_config j2s7s300_demo.launc logs1
luo@luo-ThinkPad-W530:~$ luo@luo-ThinkPad-W530:~$ luo@luo-ThinkPad-W530:~$ luo@luo-ThinkPad-W530:~$ ...