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

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

本方案采用struts2的拦截器过滤,将提交上来的参数转码来解决。

配置struts.xml


    
    
      
      
      
      
        
        
      
    
    
    
    
    ...此处省略n个action
    
  

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 map = actionContext.getParameters();
    for (Map.Entry entry : map.entrySet()) {
      String value = ((String[])(entry.getValue()))[0];
      entry.setValue(StringEscapeUtils.escapeHtml4(value));//将提交上来的字符串进行转码
      //System.out.println((entry.getValue()));
    }
    return invocation.invoke();
  }
}

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

你可能感兴趣的:(JSP Struts过滤xss攻击的解决办法)