简体   繁体   English

使用Servlet过滤器修改HttpServletRequest参数似乎不起作用

[英]Modifying HttpServletRequest parameters with a Servlet Filter doesn't seem to work

I need to remove/modify some of HttpServletRequest parameters. 我需要删除/修改一些HttpServletRequest参数。 I'm trying to do so by using a Filter based on the question I posted a few days ago. 我试图通过使用基于我几天前发布的问题Filter这样做。

In the Filter , I'm trying to wrap the HttpServletRequest by inheriting the HttpServletRequestWrapper class as follows. Filter ,我试图通过继承HttpServletRequestWrapper类来包装HttpServletRequest ,如下所示。

private final static class FilteredRequest extends HttpServletRequestWrapper {

    public FilteredRequest(ServletRequest request) {
        super((HttpServletRequest) request);
    }

    @Override
    public String getParameter(String paramName) {
        return super.getParameter(paramName);
    }

    @Override
    public String[] getParameterValues(String paramName) {
        return super.getParameterValues(paramName);
    }

    @Override
    public Map getParameterMap() {
        Map<Object, Object> parameterMap = new HashMap<Object, Object>();
        Map originalParameterMap = super.getParameterMap();

        for (Object o : originalParameterMap.entrySet()) {
            Map.Entry<Object, Object> pairs = (Entry<Object, Object>) o;
            parameterMap.put(pairs.getKey(), pairs.getValue());
        }
        return parameterMap;  //Returning a modifiable ParameterMap.
    }
}

It's an inner class within the Filter class. 它是Filter类中的内部类。 In the doFilter() method, doFilter()方法中,

chain.doFilter(new FilteredRequest(request), (HttpServletResponse)response);

the constructor of the above class is being invoked (that wraps the request). 正在调用上述类的构造函数(包装请求)。


Now, I expect any of request parameters to modify/unset in my Spring MVC controller class . 现在,我希望在我的Spring MVC控制器类中修改/取消设置任何请求参数。 I'm trying to remove the parameter in the controller class as follows. 我正在尝试删除控制器类中的参数,如下所示。

Map requestMap=request.getParameterMap();
requestMap.remove("txt_country_name");

Alternatively, 或者,

requestMap.put("txt_country_name", null);

Accordingly, the request parameter txt_country_name should be removed from the HttpServletRequest but it isn't removed (nor it throws any exception like " No modifications are allowed to a locked ParameterMap "). 因此,请求参数txt_country_name应该从HttpServletRequest删除,但不会被删除(也不会引发任何异常,例如“ 不允许对锁定的ParameterMap进行修改 ”)。 What am I missing here? 我在这里错过了什么? Am I following a wrong way? 我是否采取了错误的方式?

By the way, Making/using a request attribute all the time through out the entire application doesn't seem to be the best solution . 顺便说一句, 在整个应用程序中始终制作/使用请求属性似乎不是最佳解决方案

[I need to remove/modify request parameter within the Spring MVC controller class and not within the Filter itself] [我需要删除/修改Spring MVC控制器类中的请求参数而不是在Filter本身内]

Your remove method modifies the mutable map returned by getParameterMap() . 您的remove方法修改getParameterMap()返回的可变映射。 But the next call to getParameterMap() reconstructs a new mutable map containing all the parameters of the wrapped query. 但是下一次调用getParameterMap()重新构建一个包含所有包装查询参数的可变映射。

The mutable map should be constructed when the FilteredRequest is constructed, and it should be stored in an instance field. 构造FilteredRequest时应构造可变映射,并将其存储在实例字段中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM