All Downloads are FREE. Search and download functionalities are using the official Maven repository.

cn.jiangzeyin.common.request.ParameterXssWrapper Maven / Gradle / Ivy

There is a newer version: 1.1.11
Show newest version
package cn.jiangzeyin.common.request;

import cn.jiangzeyin.StringUtil;
import org.springframework.util.Assert;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
import java.io.UnsupportedEncodingException;
import java.util.*;

/**
 * @author jiangzeyin
 * Created by jiangzeyin on 2017/2/4.
 */
public class ParameterXssWrapper extends HttpServletRequestWrapper {
    private final Map parameters;
    private final HttpServletRequest request;

    /**
     * Constructs a request object wrapping the given request.
     *
     * @param request The request to wrap
     */
    ParameterXssWrapper(HttpServletRequest request) {
        super(request);
        //request = this;
        this.request = request;
        this.parameters = doXss(request.getParameterMap(), true);
    }

    @Override
    public Map getParameterMap() {
        return parameters;
    }

    @Override
    public Enumeration getParameterNames() {
        return new Vector<>(parameters.keySet()).elements();
    }

    @Override
    public String getParameter(String name) {
        return request.getParameter(name);
    }

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

    /**
     * 处理xss 问题
     *
     * @param map  map
     * @param utf8 utf8
     * @return 结果
     */
    public static Map doXss(Map map, boolean utf8) {
        Assert.notNull(map);
        Iterator> iterator = map.entrySet().iterator();
        Map valuesMap = new HashMap<>();
        while (iterator.hasNext()) {
            Map.Entry entry = iterator.next();
            String key = entry.getKey();
            String[] values = entry.getValue();
            if (values != null) {
                for (int i = 0; i < values.length; i++) {
                    if (!utf8)
                        values[i] = getUTF8(values[i]);
                    values[i] = StringUtil.filterHTML(values[i]);
                }
                valuesMap.put(key, values);
            }
        }
        return valuesMap;
    }

    public static String getUTF8(String str) {
        if (StringUtil.isEmpty(str))
            return "";
        try {
            return new String(str.getBytes("ISO-8859-1"), "utf-8");
        } catch (UnsupportedEncodingException e) {
            return "";
        }
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy