简介

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

自定义拦截器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. Mysql插入多条数据测试

    --新建存储过程 create procedure doinsert3() begin declare i int; declare j int; set i = 0; set j = 0; whil ...

  2. Java 读写文件示例

    import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; public class T ...

  3. Intellij IDEA 从入门到上瘾 图文教程

    1. IDEA VS Eclipse 核心术语比较 ​ 由下图可见:两者最大的转变就在于工作空间概念的转变,并且在IDEA当中,Project和 Module是作为两个不同的概念,对项目结构是具有重大 ...

  4. CSS(1)

    使用CSS的注意点: 1.style标签必须写在head标签的开始标签和结束标签之间(也就是必须和title标签是兄弟关系). 2.style标签中的type属性其实可以不用写,默认就是type=&q ...

  5. keepalived vip removed with dhcp renewal【原创】

    最近发现公司云平台服务器的vip有丢失的现象,查看keepalived日志 Jun :: lb1 dhclient: DHCPREQUEST of (xid=0x6deab016) Jun :: lb ...

  6. Intellij IDEA的Facets和Artifacts

    Facets: Facets表述了在Module中使用的各种各样的框架.技术和语言.这些Facets让Intellij IDEA知道怎么对待module内容,并保证与相应的框架和语言保持一致. 使用F ...

  7. Prometheus磁盘监控

    根据挂载目录 (node_filesystem_size_bytes {mountpoint ="/"} - node_filesystem_free_bytes {mountpo ...

  8. TCP为什么会出现 RST

    就目前遇到的情况而言,都是负载设备,或健康检查设备发送的. 为什么会出现 RST 因为具有周期性,我大概猜到了,是 lvs 对我的后端服务的健康检查导致的,联系了网络运营服务客服人员,我把.pcap给 ...

  9. Centos7 安装 weblogic12.2.1.0.0

    下载地址:地址:http://www.oracle.com/technetwork/middleware/weblogic/downloads/wls-main-097127.html 下载最新的we ...

  10. linux 中 scp 命令

    scp命令用于Linux 之间复制文件和目录.如果想在windows 环境中使用需要安装 linux 命令环境,比如 cmder scp是 secure copy的缩写, scp是linux系统下基于 ...