尝试在Filter中更改请求,以便它反映在@RequestParam中

时间:2017-04-27 09:29:15

标签: java spring annotations servlet-filters http-request-parameters

我正在尝试读取请求属性并使用Filter更改它们,以便在Controller中我将根据需要接收更改的值。但我在Filter中所做的更改未反映在@RequestParam

我的控制器:

public Response getDetails(HttpServletRequest req, @RequestParam(required = false) String userId) {
        System.out.println("attrib in request:"+req.getParameter("userId"));
       System.out.println("attrib in RequestParam:"+userId);
}

我的过滤器:

@Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
            throws IOException, ServletException {
        try {
            System.out.println("inside filter---------------------------");
            HtmlRequestWrapper req = new HtmlRequestWrapper(request);           
            chain.doFilter(req, response);  
        } catch (Exception ex) {
            request.setAttribute("errorMessage", ex);
            // request.getRequestDispatcher("/WEB-INF/views/jsp/error.jsp").forward(request,
            // response);
        }
    }

我的HttpServletRequestWrapper(HtmlRequestWrapper):

public class HtmlRequestWrapper extends HttpServletRequestWrapper {

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

    public String sanitize(String input) {
        String result = "";
        result = input + "bla bla";
        return result;
    }

    public String getParameter(String paramName) {
        String value = super.getParameter(paramName);
        value = sanitize(value);
        return value;
    }

    public String[] getParameterValues() {
        String values[] = null;
        // String values[] = request.getParameterValues(paramName);
        for (Enumeration<String> en = super.getParameterNames(); en.hasMoreElements();) {
            String paramName = (String) en.nextElement();
            values = super.getParameterValues(paramName);
            int n = values.length;
            for (int i = 0; i < n; i++) {
                System.out.println("param val before: " + values[i]);
                values[i] = sanitize(values[i]);
                System.out.println("param val after: " + values[i]);
            }
        }
        return values;
    }

    public Map<String, String[]> getParameterMap() {
        Map<String, String[]> paramMap = new HashMap<>();
        String values[] = null;
        for (Enumeration<String> en = super.getParameterNames(); en.hasMoreElements();) {
            String paramName = (String) en.nextElement();
            values = super.getParameterValues(paramName);
            int n = values.length;
            for (int i = 0; i < n; i++) {
                System.out.println("param val before in map: " + values[i]);
                values[i] = sanitize(values[i]);
                System.out.println("param val after  in map: " + values[i]);
            }
            paramMap.put(paramName, values);
            this.getRequest().setAttribute(paramName, values);
        }
        return Collections.unmodifiableMap(paramMap);
    }
}

正如您所看到的,我只是想将一些文本附加到属性值。在真实场景中,将清理并添加一些逻辑。

注意:在我的控制器中,我在req.getParameter("userId")中获得了更改的值,但@RequestParam(required = false) String userId包含从实际请求传递的相同旧值。

我希望@RequestParam(required = false) String userId能够反映req.getParameter("userId")中的变化。

更新(添加WebConfig类):

@Override
public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new ServiceInterceptor());
}
@Bean
public Filter htmlFilter() {
    return new HtmlFilter();
}

0 个答案:

没有答案