java 使用 HTTPServletRequestWrapper 包装请求参数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16848180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 00:09:35  来源:igfitidea点击:

Wrapping request parameters using HTTPServletRequestWrapper

javaservletstomcat7wrapperservlet-filters

提问by user2289898

I have a filter that authenticates/authorizes REST calls. This filters needs to access the request parameters so I have written a custom HTTPServletRequestWrapper for this.

我有一个过滤器来验证/授权 REST 调用。这个过滤器需要访问请求参数,所以我为此编写了一个自定义的 HTTPServletRequestWrapper。

import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

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

public class WrapperRequest extends HttpServletRequestWrapper {
    private Map<String, String[]> requestParams = null;

    public WrapperRequest(final ServletRequest request) {
        super((HttpServletRequest) request);

    }

    @Override
    public String getParameter(final String name) {
        if (getParameterMap().get(name) != null) {
            return getParameterMap().get(name)[0];
        } else {
            getParameterMap().get(name)[0] = super.getParameter(name);
            requestParams.put(name, getParameterMap().get(name));
            return requestParams.get(name)[0];
        }

    }

    @Override
    public Map<String, String[]> getParameterMap() {
        if (requestParams == null) {
            requestParams = new HashMap<String, String[]>();
            requestParams.putAll(super.getParameterMap());
        }
        return Collections.unmodifiableMap(requestParams);
    }

    @Override
    public Enumeration<String> getParameterNames() {
        return Collections.enumeration(getParameterMap().keySet());
    }

    @Override
    public String[] getParameterValues(final String name) {
        return getParameterMap().get(name);
    }
}

In my filter doFilter method:

在我的过滤器 doFilter 方法中:

public void doFilter(ServletRequest request, ServletResponse response,
        FilterChain chain) throws IOException, ServletException {


    final WrapperRequest wrappedRequest = new WrapperRequest(request);
    Map<String, String[]> paramMap = wrappedRequest.getParameterMap();
    chain.doFilter(wrappedRequest, response);

But I am getting the below warning

但我收到以下警告

WARNING: A servlet request, to the URI , contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.

警告:对 URI 的 servlet 请求在请求正文中包含表单参数,但请求正文已被 servlet 或访问请求参数的 servlet 过滤器使用。只有使用@FormParam 的资源方法才能按预期工作。通过其他方式使用请求正文的资源方法将无法按预期工作。

I am deploying this in Tomcat. Help!

我正在 Tomcat 中部署它。帮助!

采纳答案by stringy05

I take it you are using Jersey for your REST framework?

我认为您正在将 Jersey 用于您的 REST 框架?

I think this is basically saying that since the Servlet has now constructed the Request object, Jersey now cant tell the difference between form parameters and query string params.

我认为这基本上是说由于 Servlet 现在已经构造了 Request 对象,Jersey 现在无法区分表单参数和查询字符串参数之间的区别。

See this for some details: https://issues.apache.org/jira/browse/STANBOL-437

有关详细信息,请参阅:https: //issues.apache.org/jira/browse/STANBOL-437

This begs the question - is this actually causing you an issue or are you just worried about the warning message?

这就引出了一个问题 - 这实际上是给您带来了问题,还是您只是担心警告消息?

回答by facundofarias

Right. So I've been suffering this issue, and I've been trying to solve it on different ways, but I did't want to change my web.xml settings, just because if I was testing my application with Postman it worked perfect, but when it was being integrated with the webapp it fails with the mentioned issue (A servlet request to the URI {MY_URI} contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.)

对。所以我一直在困扰这个问题,我一直在尝试以不同的方式解决它,但我不想改变我的 web.xml 设置,只是因为如果我用 Postman 测试我的应用程序它工作得很好,但是当它与 webapp 集成时,它因上述问题而失败 ( A servlet request to the URI {MY_URI} contains form parameters in the request body but the request body has been consumed by the servlet or a servlet filter accessing the request parameters. Only resource methods using @FormParam will work as expected. Resource methods consuming the request body by other means will not work as expected.)

So as @clijkmentioned, you only have to set your headers as:

正如@clijk提到的,您只需将标题设置为:

"Content-Type": "application/json" 
"charset": "UTF-8" 

and voilá, the warning it's gone.

瞧,警告已经消失了。

Thanks

谢谢