过滤器和拦截器总结(过滤器和拦截器的执行顺序)

  本篇文章为你整理了过滤器和拦截器总结(过滤器和拦截器的执行顺序)的详细内容,包含有过滤器与拦截器 过滤器和拦截器的执行顺序 过滤器和拦截器的使用场景 过滤器和拦截器的作用 过滤器和拦截器总结,希望能帮助你了解 过滤器和拦截器总结。

  1.总结内容参考:https://blog.csdn.net/dhklsl/article/details/127533485
 

  2.下面是本人工作项目中实战到的案例(具体业务的实体没必要关注)
 

  2.1.拦截器使用

  点击查看代码

  

 

 

  import java.lang.invoke.MethodHandles;

  import javax.servlet.http.HttpServletRequest;

  import javax.servlet.http.HttpServletResponse;

  import org.apache.logging.log4j.Logger;

  import org.dom4j.Document;

  import org.springframework.web.servlet.HandlerInterceptor;

  import org.springframework.web.servlet.ModelAndView;

  import com.epoint.core.utils.string.StringUtil;

  import com.epoint.financeproduct.dao.TBaoHanApplyDao;

  import com.epoint.financeproduct.domain.Tbaohanapply;

  import com.epoint.financeproduct.tbaohanbtdh.dao.TBaoHanApplyBTDHDao;

  import com.epoint.financeproduct.util.DynamicLoggerUtil;

   * 通知api路由拦截器

   * @作者 jawel

   * @version [版本号, 2021年3月16日]

   * @see [相关类/方法]

   * @since [产品/模块版本]

  public class BTDHNoticeApiRouterInterceptor implements HandlerInterceptor

   protected Logger logger = DynamicLoggerUtil.getLoggerByClass(MethodHandles.lookup().lookupClass(), DynamicLoggerUtil.LogType.COMMON);

   @Override

   public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)

   throws Exception {

   Document document = BTDHApiRouterInterceptorUtil.preHandle(request, response, handler);

   document.getRootElement().element("body").elementText("OrderNo");

   String ApplyOrderNo = document.getRootElement().element("body").elementText("OrderNo");

   String baohanno = document.getRootElement().element("body").elementText("PolicyNo");

   logger.info("ApplyOrderNo"+ApplyOrderNo);

   logger.info("baohanno"+baohanno);

   //applyno和baohanno均为空,则直接通过,防止误拦截

   if(StringUtil.isBlank(ApplyOrderNo) StringUtil.isBlank(baohanno)){

   return true;

   //获取保函申请数据

   TBaoHanApplyBTDHDao tBaoHanApplyDao = new TBaoHanApplyBTDHDao();

   Tbaohanapply tBaoHanApply = null;

   if (StringUtil.isNotBlank(ApplyOrderNo)) {

   tBaoHanApply = tBaoHanApplyDao.getTBaoHanInfoByApplyorderno(ApplyOrderNo);

   //如果通过applyno获取申请数据为空,则再通过baohanno获取数据

   if (tBaoHanApply == null StringUtil.isNotBlank(baohanno)) {

   tBaoHanApply = tBaoHanApplyDao.getTBaoHanApplyByBaoHanNo(baohanno);

   logger.info("tBaoHanApply"+tBaoHanApply.toString());

   request.setAttribute("tBaoHanApply", tBaoHanApply);

   // 处理拦截请求

   return BTDHApiRouterInterceptorUtil.doInterceptRequest(request, response);

   @Override

   public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,

   ModelAndView modelAndView) throws Exception {

   @Override

   public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex)

   throws Exception {

  
import javax.servlet.http.HttpServletRequest;

  import javax.servlet.http.HttpServletResponse;

  import org.apache.logging.log4j.Logger;

  import org.dom4j.Document;

  import org.springframework.web.bind.annotation.RequestMapping;

  import org.springframework.web.method.HandlerMethod;

  import com.epoint.core.utils.config.ConfigUtil;

  import com.epoint.core.utils.httpclient.HttpClientUtil;

  import com.epoint.core.utils.string.StringUtil;

  import com.epoint.financeproduct.domain.Tbaohanapply;

  import com.epoint.financeproduct.job.DataSync2EpointFinanceProduct_Push_Data_Sec_H_Job;

  import com.epoint.financeproduct.util.Constant.IsOrNot;

  import com.epoint.financeproduct.util.DynamicLoggerUtil;

  import com.epoint.financeproduct.util.IDUtils;

  import com.epoint.financeproduct.util.InsuranceServerInfo;

  import com.epoint.financeproduct.util.InsuranceServerInfoUtil;

  import com.epoint.financeproduct.util.RequestUtils;

  import com.epoint.financeproduct.util.ValueProcessUtils;

  public class BTDHApiRouterInterceptorUtil

   private static Logger logger = DynamicLoggerUtil.getLoggerByClass(MethodHandles.lookup().lookupClass(), DynamicLoggerUtil.LogType.COMMON);

   public static Document preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) {

   //获取RequestMapping注解

   HandlerMethod method = (HandlerMethod) handler;

   RequestMapping controllerRequestMapping = method.getBean().getClass().getAnnotation(RequestMapping.class);

   RequestMapping methodRequestMapping = method.getMethod().getAnnotation(RequestMapping.class);

   //拼接url路径

   StringBuilder sb = new StringBuilder("");

   sb.append(getRequestMappingValue(controllerRequestMapping));

   sb.append(getRequestMappingValue(methodRequestMapping));

   //获取mapping地址

   String mappingUrl = sb.toString();

   // 存入request区域

   logger.info("mappingUrl"+mappingUrl);

   request.setAttribute("mappingUrl", mappingUrl);

   //获取请求参数

   BTBaseService baseService = null;

   try {

   baseService = new BTBaseService(request, response);

   catch (Exception e) {

   logger.error("获取baseService异常", e);

   return baseService.receivexmlString;

   * [处理拦截请求]

   * @param request

   * @param response

   * @return

   * @throws Exception

   public static boolean doInterceptRequest(HttpServletRequest request, HttpServletResponse response) throws Exception {

   // 从request请求域获取保函申请对象

   Tbaohanapply tBaoHanApply = (Tbaohanapply) request.getAttribute("tBaoHanApply");

   // 从request请求域获取mappingUrl

   String mappingUrl = (String) request.getAttribute("mappingUrl");

   logger.info("mappingUrl"+mappingUrl);

   //如果申请数据不存在,则进行转发

   if (tBaoHanApply == null) {

   logger.info("tBaoHanApply==kong");

   // 判断下当前系统是否为119大平台,防止死循环

   String isEpointProduct = ConfigUtil.getConfigValue("isepointproduct");

   if (StringUtil.isNotBlank(isEpointProduct) IsOrNot.是.value.equals(Integer.valueOf(isEpointProduct))) {

   return true;

   //转发请求

   //获取新点产品端web地址

   String epointFinanceProductInterfaceUrl = DataSync2EpointFinanceProduct_Push_Data_Sec_H_Job.getEpointFinanceProductInterfaceUrl();

   //如果未配置“新点金融服务支撑平台(产品端)接口地址”,或者mappingUrl为空,则直接通过

   if(StringUtil.isBlank(epointFinanceProductInterfaceUrl) StringUtil.isBlank(mappingUrl)){

   return true;

   // 请求转发

   doRequestForward(request, response, epointFinanceProductInterfaceUrl, mappingUrl);

   return false;

   else {

   logger.info("tBaoHanApply==getIsPushFinanceInsurance"+tBaoHanApply.getIsPushFinanceInsurance());

   // 保函申请数据存在,根据保函申请表(TBaoHanApply)上的IsPushFinanceInsurance(是否推送金融机构产品端系统)字段和FinanceInsuranceID(金融机构产品端ID)字段来判断是否转发

   // 如果IsPushFinanceInsurance(是否推送金融机构产品端系统)字段的值是“IsOrNot.是.value”,说明是转发的

   if (IsOrNot.是.value.equals(tBaoHanApply.getIsPushFinanceInsurance())) {

   // 根据FinanceInsuranceID(金融机构产品端ID)字段值找到对应的转发服务器信息,将通知接口转发到对应的服务器上

   InsuranceServerInfo insuranceServerInfo = InsuranceServerInfoUtil.getInsuranceServerInfoByID(tBaoHanApply.getPlatformcode(), tBaoHanApply.getProductcode(), tBaoHanApply.getFinanceInsuranceID());

   String financeInsuranceUrl = insuranceServerInfo.getFinanceInsuranceUrl();

   logger.info("financeInsuranceUrl"+financeInsuranceUrl);

   logger.info("mappingUrl"+mappingUrl);

   // 转发

   doRequestForward(request, response, financeInsuranceUrl, mappingUrl);

   return false;

   return true;

   * [转发]

   * @param request

   * @param response

   * @param financeProductInterfaceUrl

   * @param mappingUrl

   * @throws Exception

   public static void doRequestForward(HttpServletRequest request, HttpServletResponse response, String financeProductInterfaceUrl, String mappingUrl) throws Exception {

   InputStream in = (ByteArrayInputStream) request.getAttribute("inputstream");

   //重置流读取位置

   in.reset();

   String params = ValueProcessUtils.convertInputStream2String(in);

   String url = financeProductInterfaceUrl + mappingUrl;

   String reqID = IDUtils.getID("REQ", 5);

   logger.info("reqID=" + reqID + ",NoticeApiRouterInterceptor.url=" + url);

   String resultStr = RequestUtils.httpsPostUTF8Xml(url, params);

   logger.info("reqID=" + reqID + ",NoticeApiRouterInterceptor.postBody=" + resultStr);

   //响应返回值

   response.setHeader("Content-Type", "text/xml;charset=UTF-8");

   // response.setHeader("Content-Type", "application/json;charset=utf-8");

   response.getWriter().write(resultStr);

   * 获取RequestMapping的value值

   * @param requestMapping

   * @return

   * @exception/throws [违例类型] [违例说明]

   * @see [类、类#方法、类#成员]

   private static String getRequestMappingValue(RequestMapping requestMapping) {

   String value = "";

   if (requestMapping != null requestMapping.value() != null requestMapping.value().length 0) {

   value = requestMapping.value()[0];

   return value;

  

 

  2.2 过滤器的使用

  点击查看代码

  

package com.epoint.financeproduct.btdh.util;

 

  import java.io.BufferedReader;

  import java.io.ByteArrayInputStream;

  import java.io.IOException;

  import java.io.InputStream;

  import java.io.InputStreamReader;

  import java.lang.invoke.MethodHandles;

  import javax.servlet.Filter;

  import javax.servlet.FilterChain;

  import javax.servlet.FilterConfig;

  import javax.servlet.ReadListener;

  import javax.servlet.ServletException;

  import javax.servlet.ServletInputStream;

  import javax.servlet.ServletRequest;

  import javax.servlet.ServletResponse;

  import javax.servlet.http.HttpServletRequest;

  import javax.servlet.http.HttpServletRequestWrapper;

  import javax.servlet.http.HttpServletResponse;

  import org.apache.logging.log4j.Logger;

  import org.dom4j.Document;

  import com.epoint.core.utils.string.StringUtil;

  import com.epoint.financeproduct.dao.FinancePlatformProductSettingsDao;

  import com.epoint.financeproduct.dao.FinanceProductDao;

  import com.epoint.financeproduct.domain.FinancePlatformProductSettings;

  import com.epoint.financeproduct.domain.Financeproduct;

  import com.epoint.financeproduct.domain.Tbaohanapply;

  import com.epoint.financeproduct.tbaohanbtdh.dao.TBaoHanApplyBTDHDao;

  import com.epoint.financeproduct.util.Constant.IsOrNot;

  import com.epoint.financeproduct.util.DynamicLoggerUtil;

  import com.epoint.financeproduct.util.FileManage;

  import com.epoint.financeproduct.util.IDUtils;

  import com.epoint.financeproduct.util.RequestUtils;

  import com.epoint.financeproduct.util.ValueProcessUtils;

   * 标准版通知api路由拦截器

   * @作者 jawel

   * @version [版本号, 2021年5月27日]

   * @see [相关类/方法]

   * @since [产品/模块版本]

  public class BTDHNoticeApiRouterFilter implements Filter

   private static final Logger logger = DynamicLoggerUtil

   .getLogger(MethodHandles.lookup().lookupClass().getSimpleName(), DynamicLoggerUtil.LogType.COMMON);

   @Override

   public void init(FilterConfig filterConfig) throws ServletException {

   @Override

   public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)

   throws IOException, ServletException {

   HttpServletRequest requestWrapper = null;

   out: if (request instanceof HttpServletRequest) {

   try {

   //requestWrapper = new BufferedServletRequestWrapper((HttpServletRequest) request);

   requestWrapper = (HttpServletRequest) request;

   BTBaseService baseService = new BTBaseService((HttpServletRequest) request, (HttpServletResponse) response);

   Document document = baseService.receivexmlString;

   document.getRootElement().element("body").elementText("OrderNo");

   String ApplyOrderNo = document.getRootElement().element("body").elementText("OrderNo");

   String baohanno = document.getRootElement().element("body").elementText("PolicyNo");

   //appkey为空,则直接通过,防止误拦截

   if (StringUtil.isBlank(ApplyOrderNo)) {

   break out;

   // applyNo为空,则直接通过,防止误拦截

   if (StringUtil.isBlank(baohanno)) {

   break out;

  
TBaoHanApplyBTDHDao tBaoHanApplyDao = new TBaoHanApplyBTDHDao();

   Tbaohanapply tBaoHanApply = null;

   if (StringUtil.isNotBlank(ApplyOrderNo)) {

   tBaoHanApply = tBaoHanApplyDao.getTBaoHanInfoByApplyorderno(ApplyOrderNo);

   if (tBaoHanApply == null) {

   break out;

   FinanceProductDao financeProductDao = new FinanceProductDao();

   Financeproduct productInfo = financeProductDao.getProductInfoByProductCode(tBaoHanApply.getProductcode());

   //productInfo为空,则直接通过,防止误拦截

   if (productInfo == null) {

   break out;

   // 获取平台产品配置信息

   FinancePlatformProductSettings settings = new FinancePlatformProductSettingsDao()

   .getSettingsByPlatformCodeAndProductCode(tBaoHanApply.getPlatformcode(),

   productInfo.getProductcode());

   //如果产品上配置转发到开发环境,则进行转发

   if (IsOrNot.是.value.toString()

   .equals(ValueProcessUtils.getValueNoException(settings, "isredirectdev"))) {

   String requestURI = requestWrapper.getRequestURI();

   //转发请求

   //获取转发地址

   String routerDevUrl = ValueProcessUtils.getValueNoException(settings, "devrouterurl");

   //如果未配置“新点金融服务支撑平台(产品端)接口地址”,或者requestURI为空,则直接通过

   if (StringUtil.isBlank(routerDevUrl) StringUtil.isBlank(requestURI)) {

   break out;

   String url = routerDevUrl + requestURI;

   InputStream in = requestWrapper.getInputStream();

   //获取原始报文参数

   String params = "";

   try {

   params = ValueProcessUtils.convertInputStream2String(in);

   catch (Exception e) {

   logger.error("url=" + url + " InputStream转String失败error", e);

   String reqID = IDUtils.getID("REQ", 5);

   logger.info("BTDHNoticeApiRouterFilterreqID=" + reqID + ",NoticeApiRouterInterceptor.url=" + url);

   String resultStr = RequestUtils.httpsPostUTF8Xml(url, params);

   logger.info("BTDHNoticeApiRouterFilterreqID=" + reqID + ",NoticeApiRouterInterceptor.postBody=" + resultStr);

   //响应返回值

   HttpServletResponse newResponse = (HttpServletResponse) response;

   newResponse.setHeader("Content-Type", "text/xml;charset=UTF-8");

   //newResponse.setHeader("Content-Type", "application/json;charset=utf-8");

   newResponse.getWriter().write(resultStr);

   return;

   catch (Exception e) {

   logger.error("BTDHNoticeApiRouterFilter.doFilter error", e);

   if (requestWrapper == null) {

   chain.doFilter(request, response);

   else {

   chain.doFilter(requestWrapper, response);

   @Override

   public void destroy() {

  class BufferedServletRequestWrapper extends HttpServletRequestWrapper

   private final byte[] body; // 报文

   public BufferedServletRequestWrapper(HttpServletRequest request) throws IOException {

   super(request);

   body = FileManage.fileToByte(request.getInputStream());

   @Override

   public BufferedReader getReader() throws IOException {

   return new BufferedReader(new InputStreamReader(getInputStream()));

   @Override

   public ServletInputStream getInputStream() throws IOException {

   final ByteArrayInputStream bais = new ByteArrayInputStream(body);

   return new ServletInputStream()

   @Override

   public int read() throws IOException {

   return bais.read();

   @Override

   public boolean isFinished() {

   return false;

   @Override

   public boolean isReady() {

   return false;

   @Override

   public void setReadListener(ReadListener readListener) {

  

 

  3.使用的心得:写这个类的目的主要是将请求到测试的环境的请求,转发到本地环境。方便自己的调式代码。

  以上就是过滤器和拦截器总结(过滤器和拦截器的执行顺序)的详细内容,想要了解更多 过滤器和拦截器总结的内容,请持续关注盛行IT软件开发工作室。

郑重声明:本文由网友发布,不代表盛行IT的观点,版权归原作者所有,仅为传播更多信息之目的,如有侵权请联系,我们将第一时间修改或删除,多谢。

留言与评论(共有 条评论)
   
验证码: