首页

定义XssEncodeUtils工具类用户对XSS跨站攻击脚本字符串参数进行过滤处理

标签:XssEncodeUtils,XSS工具类,XSS跨站攻击,安全过滤     发布时间:2018-11-18   

一、前言

这边定义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("&", "&amp;");@b@    value = value.replaceAll("<", "&lt;");@b@    value = value.replaceAll(">", "&gt;");@b@    value = value.replaceAll("'", "&#39;");@b@    value = value.replaceAll("\"", "&quot;");@b@    value = value.replaceAll("/", "&#x2F;");@b@    value = value.replaceAll(" ", "&nbsp;");@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@}
  • ◆ 相关内容