过滤器和拦截器总结

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 {
    }

}


点击查看工具类
package com.epoint.financeproduct.btdh.util;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
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.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.1.1 web.xml 配置如下:

<!-- bzb通知请求过滤器 --> <filter> <filter-name>bzbnoticeApiRouterFilter</filter-name> <filter-class>com.epoint.financeproduct.util.BZBNoticeApiRouterFilter</filter-class> <async-supported>true</async-supported> </filter> <filter-mapping> <filter-name>bzbnoticeApiRouterFilter</filter-name> <url-pattern>/rest/*</url-pattern> </filter-mapping> <filter-mapping> <filter-name>bzbnoticeApiRouterFilter</filter-name> <url-pattern>/baohannotice</url-pattern> </filter-mapping> <filter-mapping> <filter-name>bzbnoticeApiRouterFilter</filter-name> <url-pattern>/restorenotice</url-pattern> </filter-mapping> <filter-mapping> <filter-name>bzbnoticeApiRouterFilter</filter-name> <url-pattern>/invoicenotice</url-pattern> </filter-mapping> <filter-mapping> <filter-name>bzbnoticeApiRouterFilter</filter-name> <url-pattern>/quitnotice</url-pattern> </filter-mapping>

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) {
            }
        };
    }

}

2.2.1对应的xml配置如下 `
<!-- 解决中文乱码 -->
<mvc:annotation-driven conversion-service="conversionService">
	<mvc:message-converters register-defaults="true">
		<bean class="org.springframework.http.converter.StringHttpMessageConverter">
			<property name="supportedMediaTypes">
				<list>
					<value>text/plain;charset=UTF-8</value>
					<value>text/html;charset=UTF-8</value>
					<value>application/json;charset=UTF-8</value>
				</list>
			</property>
		</bean>
		<!-- 处理responseBody 里面日期类型 -->
		<bean
			class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
			<property name="objectMapper">
				<bean class="com.fasterxml.jackson.databind.ObjectMapper">
					<property name="dateFormat">
						<bean class="java.text.SimpleDateFormat">
							<constructor-arg type="java.lang.String" value="yyyy-MM-dd HH:mm:ss" />
						</bean>
					</property>
					<!-- 时区指定 -->
					<property name="timeZone" value="GMT+8" />
				</bean>
			</property>
		</bean>
	</mvc:message-converters>
</mvc:annotation-driven>


<!-- 针对rest接口,创建日期和map类型的转换器 -->
<bean id="conversionService"
	class="org.springframework.format.support.FormattingConversionServiceFactoryBean">
	<property name="converters">
		<set>
			<ref bean="dateConvert" />
			<ref bean="mapConvert" />
		</set>
	</property>
</bean>
<bean id="dateConvert" class="com.epoint.frame.spring.extend.DateConvert" />
<bean id="mapConvert" class="com.epoint.frame.spring.extend.MapConvert" />

<!-- Spring 上下文环境工具 -->
<bean id="springContextUtil" class="com.epoint.frame.spring.util.SpringContextUtil" />

<bean
	class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping" />

<!-- 支持返回json(避免IE在ajax请求时,返回json出现下载 ) -->
<bean id="utf8Charset" class="java.nio.charset.Charset"
	factory-method="forName">
	<constructor-arg value="UTF-8" />
</bean>
<bean
	class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
	<property name="messageConverters">
		<list>
			<bean
				class="org.springframework.http.converter.StringHttpMessageConverter">
				<constructor-arg ref="utf8Charset" />
			</bean>
			<bean id="mappingJacksonHttpMessageConverter"
				class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
				<property name="supportedMediaTypes">
					<list>
						<value>text/plain;charset=UTF-8</value>
						<value>application/json;charset=UTF-8</value>
					</list>
				</property>
			</bean>
		</list>
	</property>
</bean>
<bean class="org.springframework.web.servlet.mvc.HttpRequestHandlerAdapter" />
<bean
	class="org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter" />

<!-- 处理代理类访问不了public方法的问题 -->
<aop:config proxy-target-class="true"></aop:config>
<!-- 拦截器配置 -->
<mvc:interceptors>
	<bean class="com.epoint.financeproduct.thirdbx.zhejiang.util.ZjApproveInterceptor" />
	<bean class="com.epoint.financeproduct.util.ApprovePromoteInterceptor" />
    <bean class="com.epoint.financeproduct.util.EmulationEnvRouterInterceptor" />
	<mvc:interceptor>
		<mvc:mapping path="/**"/>
		<mvc:exclude-mapping path="/jzjfapi/payapply" />
		<bean class="com.epoint.financeproduct.util.ApproveInterceptor" />
	</mvc:interceptor>
	<mvc:interceptor>
		<mvc:mapping path="/ddapi/**" />
		<mvc:mapping path="/guorenzlapi/**" />
		<mvc:mapping path="/huiyouapi/**" />
		<mvc:mapping path="/renbaontapi/**" />
		<mvc:mapping path="/pinganapi/**" />
		<mvc:mapping path="/ydca/**" />
		<mvc:mapping path="/zijinapi/**" />
		<mvc:mapping path="/yongancxapi/**" />
		<mvc:mapping path="/renbaobzbapi/**" />
		<mvc:mapping path="/changanbxapi/**" />
		<mvc:mapping path="/ydca/**" />
		<mvc:mapping path="/tbaohanhhthird/**" />
		<mvc:mapping path="/tradingcenterapi/**" />
		<mvc:mapping path="/tbaohanlyhaapi/**" />
		<bean class="com.epoint.financeproduct.util.NoticeApiRouterInterceptor" />
	</mvc:interceptor>
	<mvc:interceptor>
		<mvc:mapping path="/tbaohanlyhaapi/**" />
		<bean class="com.epoint.financeproduct.util.HuaAnNoticeApiRouterInterceptor" />
	</mvc:interceptor>
	<!--<mvc:interceptor>
		<mvc:mapping path="/tbaohanhbtdhapi/**" />
		<bean class="com.epoint.financeproduct.btdh.util.BTDHNoticeApiRouterInterceptor" />
	</mvc:interceptor>-->
	<mvc:interceptor>
		<mvc:mapping path="/tbaohanzlapply/submit" />		
		<bean class="com.epoint.financeproduct.tbaohanzl.TBoHanZLFPNoticeApiRouterInterceptor" />
	</mvc:interceptor>
	<!--<mvc:interceptor>
		<mvc:mapping path="/**/baohannotice" />
		<mvc:mapping path="/**/restorenotice" />
		<mvc:mapping path="/**/invoicenotice" />
		<mvc:mapping path="/**/quitnotice" />
		<bean class="com.epoint.financeproduct.util.BZBNoticeApiRouterInterceptor" />
	</mvc:interceptor>-->
</mvc:interceptors>

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

热门相关:首席的独宠新娘   照见星星的她   巡狩万界   霸皇纪   网游之逆天飞扬