簡介
攔截器我想大家都並不陌生,最常用的登錄攔截、權限校驗、防重復提交、記錄日志等等,總之可以去做很多的事情。
自定義攔截器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); } }
