使用HttpServletRequestWrapper重写Request请求参数

目的: 改变请求参数的值,满足项目需求(如:过滤请求中 lang != zh 的请求)

1.使用 HttpServletRequestWrapper重写

public class ChangeRequestWrapper extends HttpServletRequestWrapper {
    private Map<String, String[]> parameterMap; // 所有参数的Map集合

    public ChangeRequestWrapper(HttpServletRequest request) {
        super(request);
        parameterMap = request.getParameterMap();
    }

    // 重写几个HttpServletRequestWrapper中的方法

    /**
     * 获取所有参数名
     *
     * @return 返回所有参数名
     */
    @Override
    public Enumeration<String> getParameterNames() {
        Vector<String> vector = new Vector<String>(parameterMap.keySet());
        return vector.elements();
    }

    /**
     * 获取指定参数名的值,如果有重复的参数名,则返回第一个的值 接收一般变量 ,如text类型
     *
     * @param name 指定参数名
     * @return 指定参数名的值
     */
    @Override
    public String getParameter(String name) {
        String[] results = parameterMap.get(name);
        return results[0];
    }


    /**
     * 获取指定参数名的所有值的数组,如:checkbox的所有数据
     * 接收数组变量 ,如checkobx类型
     */
    @Override
    public String[] getParameterValues(String name) {
        return parameterMap.get(name);
    }

    @Override
    public Map<String, String[]> getParameterMap() {
        return parameterMap;
    }

    public void setParameterMap(Map<String, String[]> parameterMap) {
        this.parameterMap = parameterMap;
    }
}

2.新增Filter

public class LangFilter implements Filter {
    @Override
    public void init(FilterConfig filterConfig) throws ServletException {

    }

    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {

        ChangeRequestWrapper changeRequestWrapper = new ChangeRequestWrapper((HttpServletRequest) servletRequest);

        Map<String, String[]> parameterMap = new HashMap<>(changeRequestWrapper.getParameterMap());

        String[] strings = parameterMap.get("lang");           //逻辑代码给定默认的参数值,如果参数不为中文,则直接返回异常
        if (strings == null || strings.length == 0) {
            strings = new String[1];
            strings[0] = "zh";
            parameterMap.put("lang", strings);
            changeRequestWrapper.setParameterMap(parameterMap);
        }else{
            String language = strings[0];
            if (!language.equals("zh")) {
                Map<String,String> error = new HashMap<>();
                error.put("code","500");

                ServletOutputStream outputStream = null;
                try {
                    outputStream = servletResponse.getOutputStream();
                    outputStream.write(JSONUtils.obj2Byte(error));
                } finally {
                    if (outputStream != null) {
                        outputStream.flush();
                        outputStream.close();
                    }
                }

                return;
            }
        }
        //使用复写后的wrapper
        filterChain.doFilter(changeRequestWrapper, servletResponse);
    }

    @Override
    public void destroy() {

    }
}

 3.web.xml增加一个过滤器处理

<filter>
        <filter-name>languageFilter</filter-name>
        <filter-class>com.intercepor.LangFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
        <init-param>
            <param-name>forceEncoding</param-name>
            <param-value>true</param-value>
        </init-param>
    </filter>

    <filter-mapping>
        <filter-name>languageFilter</filter-name>
        <url-pattern>*.do</url-pattern>
    </filter-mapping>

 

posted @ 2020-10-10 12:22  albert飞的博客  阅读(3189)  评论(0编辑  收藏  举报