简介

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

自定义拦截器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. element ui的table的头部自定义

    <el-table-column label="级别" min-width="120" prop="clueLevel" align= ...

  2. FCN笔记

    FCN.py tensorflow命令行参数 FLAGS = tf.flags.FLAGS tf.flags.DEFINE_integer("batch_size", " ...

  3. Linux ps -ef vs. ps aux(ps -aux)

    ps aux.ps -aux.ps -ef之间的区别 - wynter_的博客 - CSDN博客 https://blog.csdn.net/wynter_/article/details/73825 ...

  4. Android开发三步骤

    产品经理给需求,UI给图片 开发 *写布局文件 *写Java代码 测试

  5. (待续)【转载】 DeepMind发Nature子刊:通过元强化学习重新理解多巴胺

    原文地址: http://www.dataguru.cn/article-13548-1.html -------------------------------------------------- ...

  6. Python3基础 函数 多值参数 元组与字典形式(使用星号对列表与字典进行拆包)

             Python : 3.7.3          OS : Ubuntu 18.04.2 LTS         IDE : pycharm-community-2019.1.3    ...

  7. 图片缩放——利用layui的滑块

    @layui官网文档.@参考博客 参考博客中能实现,但是效果差强人意,在前辈的基础上进行了改造,并支持了动态多图列表 <%@ page language="java" con ...

  8. 【pod无法删除 总是处于terminate状态】强行删除pod

    加参数 --force --grace-period=0,grace-period表示过渡存活期,默认30s,在删除POD之前允许POD慢慢终止其上的容器进程,从而优雅退出,0表示立即终止POD ku ...

  9. echarts移动端demo

    说明:建议移动端使用的时候自己定制需要的东西,详情看官网 ECharts   效果图: 代码: <!DOCTYPE html> <html style="height: 1 ...

  10. MultiDesk远程桌面连接

    MultiDesk 是一个选项卡(TAB标签)方式的远程桌面连接 (Terminal Services Client),可以管理组远程桌面连接,更改连接端口. 功能特性 绿色软件,只有一个很小的可执行 ...