简介

  拦截器我想大家都并不陌生,最常用的登录拦截权限校验防重复提交记录日志等等,总之可以去做很多的事情。

自定义拦截器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的更多相关文章

  1. SpringMVC 学习-拦截器 HandlerInterceptor 类

    一.拦截器 HandlerInterceptor 类的作用 SpringMVC 的拦截器类似于 Servlet 开发中的过滤器 Filter,用于对处理器进行预处理和后处理. 二.怎么使用呢? 1. ...

  2. 014-Spring Boot web【三】拦截器HandlerInterceptor、异常处理页面,全局异常处理ControllerAdvice

    一.拦截器HandlerInterceptor 1.1.HandlerInterceptor接口说明 preHandle,congtroller执行前,如果返回false请求终端 postHandle ...

  3. 【Java Web开发学习】Spring MVC 拦截器HandlerInterceptor

    [Java Web开发学习]Spring MVC 拦截器HandlerInterceptor 转载:https://www.cnblogs.com/yangchongxing/p/9324119.ht ...

  4. springmvc(3)拦截器HandlerInterceptor源码的简单解析

    其实拦截器就是我们的AOP编程.拦截器在我们的实际项目中实用性比较大的,比如:日志记录,权限过滤,身份验证,性能监控等等.下面就简单的来研究一下拦截器: public interface Handle ...

  5. springboot拦截器HandlerInterceptor详解

    Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Spring提供的HandlerInterceptor(拦截器). HandlerInterceptor 的功能跟过滤器类似,但 ...

  6. 【Spring】7、拦截器HandlerInterceptor

    处理器拦截器简介 Spring Web MVC的处理器拦截器(如无特殊说明,下文所说的拦截器即处理器拦截器) 类似于Servlet开发中的过滤器Filter,用于对处理器进行预处理和后处理.   常见 ...

  7. spring mvc拦截器HandlerInterceptor

    本文主要介绍springmvc中的拦截器,包括拦截器定义和的配置,然后演示了一个链式拦截的测试示例,最后通过一个登录认证的例子展示了拦截器的应用 拦截定义 定义拦截器,实现HandlerInterce ...

  8. Spring 拦截器——HandlerInterceptor

    采用Spring拦截器的方式进行业务处理.HandlerInterceptor拦截器常见的用途有: 1.日志记录:记录请求信息的日志,以便进行信息监控.信息统计.计算PV(Page View)等. 2 ...

  9. 22. Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】

    转:http://blog.csdn.net/linxingliang/article/details/52069495 上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API ...

  10. (22)Spring Boot 拦截器HandlerInterceptor【从零开始学Spring Boot】

    上一篇对过滤器的定义做了说明,也比较简单.过滤器属于Servlet范畴的API,与Spring 没什么关系.     Web开发中,我们除了使用 Filter 来过滤请web求外,还可以使用Sprin ...

随机推荐

  1. python 设计模式学习代码记录

    @工厂模式class Beijing: def printreslut(self): print("ok") class Shanghai: def printreslut(sel ...

  2. 小福bbs-冲刺日志(第二天)

    [小福bbs-冲刺日志(第二天)] 这个作业属于哪个课程 班级链接 这个作业要求在哪里 作业要求的链接 团队名称 小福bbs 这个作业的目标 UI重构完成 作业的正文 小福bbs-冲刺日志(第二天) ...

  3. Java垃圾回收(java GC)

    一.GC的阶段 对每个对象而言,垃圾回收分为两个阶段:finalization和reclamation. finalization: 指运行这个对象的finalize的方法. reclamation: ...

  4. html5中time元素详解

    html5中time元素详解 一.总结 一句话总结: time的使用的话主要是将时间放在datetime属性里面:<time datetime="2015-10-22"> ...

  5. vbs msgbox提示信息最前面显示

    msgbox strContent, vbOKOnly or vbExclamation or vbSystemModal,strTitle 提示框类型列表: 常数 值 描述 vbOKOnly 0 只 ...

  6. pyqt5界面

    用pyqt5做了一个小程序,保留一下这个固定格式: import sys from PyQt5 import uic, QtGui from PyQt5.QtGui import QWindow fr ...

  7. 测试一下windowsLiveWriter

    一个是看看这个东西能不能发布出博客,还有一个就是准备开始写博客了,所以随便写个作为开始吧,我不想多说什么目标啊,什么的,所以就这一句简单的一句话就够了.

  8. xml文档操作

    /** * */package com.gootrip.util; import java.io.ByteArrayOutputStream;import java.io.File;import ja ...

  9. RabbitMQ 入门教程(PHP版) 第五部分:通过主题进行消息分发(Topics)

    对于 Message 的 routing_key 字符串格式是有限制的:以点号"."分割的字符表,如php.laravel,并且长度不能超过 255 个字节. 对于 routing ...

  10. k8s记录-etcd集群部署(三)

    https://blog.csdn.net/fy_long/article/details/86542872 1)程序准备tar xvf etcd-v3.3.11-linux-amd64.tar.gz ...