java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > HttpServletRequestWrapper干预Request

HttpServletRequestWrapper干预Request处理流程解析

作者:晴天哥_王志

这篇文章主要分析在 Tomcat的处理 http 请求的流程中干预 Request对象, 通过基于HttpServletRequestWrapper和 Filter组合进行干预,有需要的朋友可以借鉴参考下,希望能够有所帮助

开篇

Tomcat 处理 http 请求的过程中 Request 对象是RequestFacade对象(可以通过 springBoot 中跟踪tomcat-embed-core 的包进行跟踪),干预后会设配成HttpServletRequestWrapper的子类对象进行传递。

HttpServletRequestWrapper和RequestFacade都实现 HttpServletRequest 接口。

Tomcat 处理流程

Request 分析

Request

package org.apache.catalina.connector;
public class Request implements org.apache.catalina.servlet4preview.http.HttpServletRequest {
}
------------------------------
package org.apache.catalina.servlet4preview.http;
public interface HttpServletRequest extends javax.servlet.http.HttpServletRequest {
    public ServletMapping getServletMapping();
    public PushBuilder newPushBuilder();
}
------------------------------
package javax.servlet.http;
public interface HttpServletRequest extends ServletRequest {
}
----------------------------------------
package javax.servlet;
public interface ServletRequest {
}

RequestFacade

package org.apache.catalina.connector;
public class RequestFacade implements HttpServletRequest {
    public RequestFacade(Request request) {
        this.request = request;
    }
    protected Request request = null;
}
-------------------------------------
package org.apache.catalina.servlet4preview.http;
public interface HttpServletRequest extends javax.servlet.http.HttpServletRequest {
    public ServletMapping getServletMapping();
    public PushBuilder newPushBuilder();
}
-------------------------------------
package javax.servlet.http;
public interface HttpServletRequest extends ServletRequest {
}
----------------------------------------
package javax.servlet;
public interface ServletRequest {
}

HttpServletRequestWrapper

package javax.servlet.http;
public class HttpServletRequestWrapper extends ServletRequestWrapper implements
        HttpServletRequest {
    public HttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }
}
----------------------------------------
package javax.servlet;
public class ServletRequestWrapper implements ServletRequest {
    private ServletRequest request;
    public ServletRequestWrapper(ServletRequest request) {
        if (request == null) {
            throw new IllegalArgumentException("Request cannot be null");
        }
        this.request = request;
    }
}
----------------------------------------
package javax.servlet;
public interface ServletRequest {
}

Request 改写 Demo

import org.springframework.stereotype.Component;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.IOException;
@Component
public class WrapperFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {
        System.out.println("wrapper filter init");
    }
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        WrapperDemo wrapperDemo = new WrapperDemo((HttpServletRequest)servletRequest);
        filterChain.doFilter(wrapperDemo, servletResponse);
    }
    @Override
    public void destroy() {
        System.out.println("wrapper filter init");
    }
}
---------------------------------
class WrapperDemo extends HttpServletRequestWrapper {
    public WrapperDemo(HttpServletRequest request) {
        super(request);
    }
}

ServletWebRequest参数获取

at org.apache.catalina.connector.Request.getParameter(Request.java:1140)
at org.apache.catalina.connector.RequestFacade.getParameter(RequestFacade.java:381)
at javax.servlet.ServletRequestWrapper.getParameter(ServletRequestWrapper.java:153)
at org.springframework.web.context.request.ServletWebRequest.getParameter(ServletWebRequest.java:154)

以上就是HttpServletRequestWrapper干预Request处理流程解析的详细内容,更多关于HttpServletRequestWrapper干预Request的资料请关注脚本之家其它相关文章!

您可能感兴趣的文章:
阅读全文