欢迎访问我的个人网站==》 jiashubing.cn

JavaWeb过滤器读取并修改POST请求的参数

核心原理就是一个类:javax.servlet.http.HttpServletRequestWrapper

一个简单的例子,当任何请求的参数里有 aaa的时候,把它的值改成 bbb

新增一个类自定义的类 CustomHttpServletRequestWrapper 

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;

public class CustomHttpServletRequestWrapper extends HttpServletRequestWrapper {

    public CustomHttpServletRequestWrapper(HttpServletRequest request) {
        super(request);
    }

    @Override
    public String getParameter(String name) {
        if ("aaa".equals(name)) {
            return "bbb";
        }
        return super.getParameter(name);
    }
}

 

然后在Filter过滤器里使用这个类

@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain filterChain)
      throws IOException, ServletException {
   CustomHttpServletRequestWrapper xssRequest = new CustomHttpServletRequestWrapper((HttpServletRequest) request);
   filterChain.doFilter(xssRequest, response);
}

 

posted @ 2023-05-12 18:43  贾树丙  阅读(454)  评论(0编辑  收藏  举报