基于HttpServletRequestWrapper干预Req

2022-08-20  本文已影响0人  晴天哥_王志

开篇

Tomcat 处理流程

Tomcat 处理流程

Request 分析

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

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

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)
上一篇 下一篇

猜你喜欢

热点阅读