package com.itcast.front.save;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletRequestWrapper;
/**
* XSS请求抓取
* @author yangtong
* XssHttpServletRequestWrapper 一个是request包装器,负责过滤掉非法的字符
*/
public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper{
public XssHttpServletRequestWrapper(HttpServletRequest request) {
super(request);
}
//覆盖getParameterValues方法,将参数值做xss过滤
public String[] getParameterValues(String parameter) {
String[] values = super.getParameterValues(parameter);
if (values==null) {
return null;
}
int count = values.length;
String[] encodedValues = new String[count];
for (int i = 0; i
encodedValues[i] = cleanXSS(values[i]);
}
return encodedValues;
}
//覆盖getParameter方法,将参数做xss过滤
public String getParameter(String parameter) {
String value = super.getParameter(parameter);
if (value == null) {
return null;
}
return cleanXSS(value);
}
//覆盖getHeader方法,将参数名做xss过滤
public String getHeader(String name) {
String value = super.getHeader(name);
if (value == null)
return null;
return cleanXSS(value);
}
//转码 将容易引起xss漏洞的半角字符直接替换成全角字符
private String cleanXSS(String value) {
value = value.replaceAll("", "& gt;");
value = value.replaceAll("\\(", "& #40;").replaceAll("\\)", "& #41;");
value = value.replaceAll("'", "& #39;");
value = value.replaceAll("eval\\((.*)\\)", "");
value = value.replaceAll("[\\\"\\\'][\\s]*javascript:(.*)[\\\"\\\']", "\"\"");
value = value.replaceAll("script", "");
return value;
}
}
在这里十分感谢老大,虽然自己的嘴硬,从来不会说软话,老大杨*帮了我很多,十分感谢,在这里我谢谢老大,没有把我这个菜鸟开了,也感谢老大能带着菜鸟一起飞.菜鸟,飞起来吧!!!