JSP Struts过滤xss攻击的解决办法
JSP Struts过滤xss攻击的解决办法
本方案采用struts2的拦截器过滤,将提交上来的参数转码来解决。
配置struts.xml
<package name="default" namespace="/" extends="struts-default, json-default"> <!-- 配置拦截器 --> <interceptors> <!-- 定义xss拦截器 --> <interceptor name="xssInterceptor" class="...此处填写拦截器类名"></interceptor> <!-- 定义一个包含xss拦截的拦截栈 --> <interceptor-stack name="myDefault"> <interceptor-ref name="xssInterceptor"></interceptor-ref> <interceptor-ref name="defaultStack"></interceptor-ref> </interceptor-stack> </interceptors> <!-- 这个必须配置,否则拦截器不生效 --> <default-interceptor-ref name="myDefault"></default-interceptor-ref> <action> ...此处省略n个action </action> </package>
Java代码,拦截器实现类
import java.util.Map; import org.apache.commons.lang3.StringEscapeUtils; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class XssInterceptor extends AbstractInterceptor{ @Override public String intercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub ActionContext actionContext = invocation.getInvocationContext(); Map<String, Object> map = actionContext.getParameters(); for (Map.Entry<String, Object> entry : map.entrySet()) { String value = ((String[])(entry.getValue()))[0]; entry.setValue(StringEscapeUtils.escapeHtml4(value));//将提交上来的字符串进行转码 //System.out.println((entry.getValue())); } return invocation.invoke(); } }
感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!
相关推荐
sswqycbailong 2020-07-28
csxiaoqiang 2020-07-26
码农成长记 2020-07-19
layloge 2020-07-05
layloge 2020-06-26
liangjielaoshi 2020-06-25
csxiaoqiang 2020-06-16
某先生 2020-06-13
ItBJLan 2020-06-11
layloge 2020-06-07
csxiaoqiang 2020-06-03
sswqycbailong 2020-06-01
layloge 2020-05-30
码农成长记 2020-05-28
qidu 2020-05-26
zhuangnet 2020-05-20
zhuangnet 2020-05-19
xiaoemo0 2020-05-16
码农成长记 2020-05-10