1.struts2版本号:2.5.26 。
2.自定义拦截器(此处,struts2已经对请求数据进行了处理,文件表单数据也可以获取,但处理不了文件)
public class XssInterceptor extends AbstractInterceptor { @Override public String intercept(ActionInvocation invocation) throws Exception { try { ActionContext actionContext = invocation.getInvocationContext(); HttpParameters parameters = actionContext.getParameters(); for (Map.Entry<String,Parameter> entry : parameters.entrySet()) { String key = entry.getValue().getName(); Parameter parameter = entry.getValue(); Object v = null; if(parameter.isMultiple()) {//存在name值相同的表单元素 String[] multipleValues = parameter.getMultipleValues(); //处理值 v = multipleValues; }else{ String value = entry.getValue().getValue(); //处理值 v = value; } Request request = new Parameter.Request(key,v); entry.setValue(request); } return invocation.invoke(); }catch (Exception e) { throw new Exception(); } } }
3.重写默认拦截器
<interceptors> <interceptor name="xssInterceptor" class="com.package.test.xss.XssInterceptor"/> <interceptor-stack name="sessionInterceptorStack"> <interceptor-ref name="exception"/> <interceptor-ref name="alias"/> <interceptor-ref name="servletConfig"/> <interceptor-ref name="i18n"/> <interceptor-ref name="prepare"/> <interceptor-ref name="chain"/> <interceptor-ref name="scopedModelDriven"/> <interceptor-ref name="modelDriven"/> <interceptor-ref name="xssInterceptor"/> <interceptor-ref name="fileUpload"> <param name="allowedExtensions"> .jpg,.png,.JPG,.jpeg,.doc,.docx,.pdf,.xls,.xlsx,.PDF </param> </interceptor-ref> <interceptor-ref name="checkbox"/> <interceptor-ref name="datetime"/> <interceptor-ref name="multiselect"/> <interceptor-ref name="staticParams"/> <interceptor-ref name="actionMappingParams"/> <interceptor-ref name="params"/> <interceptor-ref name="conversionError"/> <interceptor-ref name="validation"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="workflow"> <param name="excludeMethods">input,back,cancel,browse</param> </interceptor-ref> <interceptor-ref name="debugging"/> </interceptor-stack> </interceptors> <default-interceptor-ref name="sessionInterceptorStack" />
原文:https://www.cnblogs.com/shuiyingyuan/p/15217140.html