一、前言
这边定义XssEncodeUtils安全加密过滤工具类,用于解决XSS跨站攻击脚本安全过滤转码处理(将可能脚本标签转码过滤处理,防止可执行代码作为入参字符串进行传递),详情参见示例说明。
二、示例说明
import javax.servlet.http.HttpServletRequest;@b@import org.apache.commons.lang.StringUtils;@b@@b@public class XssEncodeUtils@b@{@b@ public static String[] getParameterValues(String[] values)@b@ {@b@ if (values == null)@b@ return null;@b@@b@ int count = values.length;@b@ String[] encodedValues = new String[count];@b@@b@ for (int i = 0; i < count; ++i) {@b@ encodedValues[i] = cleanXSS(values[i]);@b@ }@b@@b@ return encodedValues;@b@ }@b@@b@ public static String cleanXSS(String value) {@b@ if (StringUtils.isBlank(value))@b@ return value;@b@ value = value.replaceAll("&", "&");@b@ value = value.replaceAll("<", "<");@b@ value = value.replaceAll(">", ">");@b@ value = value.replaceAll("'", "'");@b@ value = value.replaceAll("\"", """);@b@ value = value.replaceAll("/", "/");@b@ value = value.replaceAll(" ", " ");@b@ return value;@b@ }@b@@b@ public static String getParameter(String parameter, HttpServletRequest request)@b@ {@b@ String value = request.getParameter(parameter);@b@ return cleanXSS(value);@b@ }@b@@b@ public static String getHeader(String name, HttpServletRequest request) {@b@ String value = request.getHeader(name);@b@ return cleanXSS(value);@b@ }@b@@b@ public static String httpResponseSplit(String value) {@b@ if (StringUtils.isBlank(value))@b@ return value;@b@ value = value.replaceAll("%0d", "");@b@ value = value.replaceAll("\r", "");@b@ value = value.replaceAll("%0a", "");@b@ value = value.replaceAll("\n", "");@b@@b@ return value;@b@ }@b@}