JSP Struts过滤xss攻击的解决办法

所属分类: 网络编程 / JSP编程 阅读数: 594
收藏 0 赞 0 分享

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();
  }
}

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

更多精彩内容其他人还在看

使用JavaBean创建您的网上日历本(2)

使用JavaBean创建您的网上日历本(2)
收藏 0 赞 0 分享

如何用Jsp读取Mysql数据库

如何用Jsp读取Mysql数据库
收藏 0 赞 0 分享

打开页面就是全屏的方法

打开页面就是全屏的方法
收藏 0 赞 0 分享

如何在Jsp中使用JDBC来联结MySql

如何在Jsp中使用JDBC来联结MySql
收藏 0 赞 0 分享

在linux上建jsp環境

这篇文章主要介绍了在linux上建jsp環境
收藏 0 赞 0 分享

如何使用JSP访问MySQL数据库

如何使用JSP访问MySQL数据库
收藏 0 赞 0 分享

JSP数据库操作例程(Use Bean)

JSP数据库操作例程(Use Bean)
收藏 0 赞 0 分享

如何使用JSP+MySQL创建留言本(一)

如何使用JSP+MySQL创建留言本(一)
收藏 0 赞 0 分享

如何使用JSP+MySQL创建留言本(二)

如何使用JSP+MySQL创建留言本(二)
收藏 0 赞 0 分享

如何使用JSP连接DB2数据库

如何使用JSP连接DB2数据库
收藏 0 赞 0 分享
查看更多