[转]SSH框架 Struts2 过滤特殊字符,防止xss攻击

原文地址:基于Struts2修复XSS漏洞(博主验证有效)_struts2 xss-CSDN博客

1.编写XssFilter

import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import com.itcast.utils.XssHttpServletRequestWrapper;

/**
* XSS防跨站脚本攻击过滤器
*
*/
public class XssFilter implements Filter {
FilterConfig filterConfig = null;
/**
* Default constructor.
*/
public XssFilter() {
}

public void destroy() {
this.filterConfig = null;
}

public void init(FilterConfig fConfig) throws ServletException {
this.filterConfig = fConfig;
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
chain.doFilter(new XssHttpServletRequestWrapper((HttpServletRequest) request), response);
}


}

 

2.编写XssHttpServletRequestWrapper类

import java.util.Enumeration;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.StrutsRequestWrapper;
import org.springframework.util.CollectionUtils;
public class XssHttpServletRequestWrapper extends StrutsRequestWrapper {

HttpServletRequest orgRequest = null;

public XssHttpServletRequestWrapper(HttpServletRequest servletRequest) {
super(servletRequest);
orgRequest = servletRequest;
}

/**
* 重写getParameterValues方法
* 通过循环取出每一个请求结果
* 再对请求结果进行过滤
* */
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 < count; i++) {
encodedValues[i] = cleanXSS(values[i]);
}
return encodedValues;
}

/**
* 重写getParameter方法
* 对请求结果进行过滤
* */
public String getParameter(String parameter) {
String value = super.getParameter(parameter);
if (value == null) {
return null;
}
return cleanXSS(value);
}

public String getHeader(String name) {
String value = super.getHeader(name);
if (value == null)
return null;
return cleanXSS(value);
}
/**
* 获取最原始的request
*
* @return
*/
public HttpServletRequest getOrgRequest() {
return orgRequest;
}

/**
* 获取最原始的request的静态方法
*
* @return
*/
public static HttpServletRequest getOrgRequest(HttpServletRequest req) {
if (req instanceof XssHttpServletRequestWrapper) {
return ((XssHttpServletRequestWrapper) req).getOrgRequest();
}
return req;
}

@Override
public Enumeration<String> getParameterNames() {
Enumeration<String> names = super.getParameterNames();
while(names.hasMoreElements()){
String name = names.nextElement();
name = cleanXSS(name);
}
return names;
}


@Override
public Map getParameterMap() {

Map paramMap = super.getParameterMap();
if (CollectionUtils.isEmpty(paramMap)) {
return paramMap;
}

for (Object value : paramMap.values()) {
String[] str = (String[])value;
if (str != null) {
for (int i = 0; i < str.length; i++) {

str[i] = cleanXSS(str[i]);
}
}
}
return paramMap;
}

private String cleanXSS(String value) {
value = value.replaceAll("<", "& lt;").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", "");
value = value.replaceAll("alert", "");

return value;
}
}

 

3.注解配置web.xml 过滤器XssFilter

注意:struts-xssFilter一定要在StrutsPrepareAndExecuteFilter之前执行否则无效,具体体现在struts-xssFilter自定义过滤器在StrutsPrepareAndExecuteFilter注解之上。

<filter>
<filter-name>struts-xssFilter</filter-name>
<filter-class>
com.itcast.filter.XssFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts-xssFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

<filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>

<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
4.效果演示

输入富文本

 

后端接收过滤后文本(特殊字符经过转译)

 


————————————————
版权声明:本文为CSDN博主「古大狼」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/weixin_42210904/article/details/107490611

posted @ 2023-11-17 16:30  dirgo  阅读(198)  评论(0编辑  收藏  举报