首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

struts2 源码分析 request -设立setParameters 的值

2014-01-15 
struts2 源码分析 request---设置setParameters 的值?? ? strtus2通过 ServletActionContext.getRequest()

struts2 源码分析 request ---设置setParameters 的值

?

? ? strtus2通过 ServletActionContext.getRequest() 获取Request。

??? 获取的Request对象有可能是MultiPartRequestWrapper也有可能是StrutsRequestWrapper

??? 为了动态像Request设置值,通过源码了解。通过以下方法可以动态获取值。

??? 如果没有用strtus2中获取的Request是原生的Request的话,就直接可以通过

???

package com.dep.aop;import java.util.HashMap;import java.util.Iterator;import java.util.Map;import java.util.Set;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletRequestWrapper;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import com.dep.util.StringUtil;/** * 拦截防止sql注入  * @author wb_zypt * */public class XssHttpServletRequestWrapper extends HttpServletRequestWrapper {HttpServletRequest orgRequest = null;Map newParams = null;private static Logger log = LoggerFactory.getLogger(XssHttpServletRequestWrapper.class);public XssHttpServletRequestWrapper(HttpServletRequest request) {super(request);orgRequest = request;}/*** 覆盖getParameter方法,将参数名和参数值都做xss过滤。<br/>* 如果需要获得原始的值,则通过super.getParameterValues(name)来获取<br/>* getParameterNames,getParameterValues和getParameterMap也可能需要覆盖*/@Overridepublic String getParameter(String name) {String value = super.getParameter(StringUtil.filterDangerString(name));if (value != null) {value = StringUtil.filterDangerString(value);}if(value == null){value = (String)getParameterMap().get(name);}return value;}@Override@SuppressWarnings("unchecked")public Map getParameterMap() {if(newParams !=null){return newParams;}else{newParams = new HashMap();}//Map newParams  = new HashMap();Map params = super.getParameterMap();Set<String> keySet = params.keySet();        for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {            String key = (String) iterator.next();             Object obj =  params.get(key);            if(obj instanceof String){             String str = (String) params.get(key);             newParams.put(key, StringUtil.filterDangerString((String)str));            }else if(obj.getClass() == String[].class){             String[] str = (String[]) params.get(key);             newParams.put(key, xssEncode((String[])str));            }else{             newParams.put(key, obj);            }                                   }/*java.lang.reflect.Field lockedField = null;try {lockedField = params.getClass().getDeclaredField("locked");lockedField.setAccessible(true);lockedField.set(params, false);} catch (Exception e) {log.error(e.getMessage(), e);}Set<String> keySet = params.keySet();        for (Iterator iterator = keySet.iterator(); iterator.hasNext();) {            String key = (String) iterator.next();             Object obj =  params.get(key);            if(obj instanceof String){             String str = (String) params.get(key);             params.put(key, xssEncode((String)str));            }else{             String[] str = (String[]) params.get(key);             params.put(key, xssEncode((String[])str));            }                                   }        if(lockedField!=null){        try {lockedField.set(params, true);} catch (Exception e) {log.error(e.getMessage(), e);}        }*/return newParams;}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] = StringUtil.filterDangerString(values[i]);       }      return encodedValues;    }/*** 覆盖getHeader方法,将参数名和参数值都做xss过滤。<br/>* 如果需要获得原始的值,则通过super.getHeaders(name)来获取<br/>* getHeaderNames 也可能需要覆盖*/@Overridepublic String getHeader(String name) {String value = super.getHeader(StringUtil.filterDangerString(name));if (value != null) {value = StringUtil.filterDangerString(value);}return value;}private static String[] xssEncode(String[] s) {String[] newStr = new String[s.length];for(int i=0;i<s.length;i++){newStr[i]= StringUtil.filterDangerString(s[i]);}return newStr;}/*** 获取最原始的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;}}

?

热点排行