`
demon3780
  • 浏览: 82797 次
  • 性别: Icon_minigender_1
  • 来自: 武汉
社区版块
存档分类
最新评论

Struts1拦截器插件使用

阅读更多
最近处理以前项目的一些漏洞需要对from中的String字段做一下过滤再显示到页面,每一个action都做修改是不现实的,原本想写个servlet来处理但是考虑有些麻烦,因此搜了一下拦截器发现有人实现了struts1的拦截器,用这个比较方便,感谢这些奉献的人!

关于struts1的拦截器,一般是通过struts插件进行注册,网络上有2个开源组件的实现。
saif-0[1].1.jar和saif-spring.jar(该包依赖spring-webmvc-struts.jar包),不知2者是什么关系,本人根据比较后使用的是前者。下面详细介绍这2个组件:

现实一个action
/**
*
*/
package com.test;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
* @author Administrator
*
*/
public class TestAction extends Action {

@Override
public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response)
throws Exception {

System.out.println("testt ......");
return mapping.findForward("index");
}

}

struts配置:
  <action-mappings>
    <action  path="/browser" type="com.test.TestAction">
<forward name="index" path="/index.jsp" />
<forward name="index0" path="/index0.jsp" />
</action>
  </action-mappings>

1、saif
a.实现拦截器
package com.test;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionMapping;
public class DisplayInterceptor implements net.sf.struts.saif.ActionInterceptor {
public void afterAction(Action arg0, ActionMapping arg1, ActionForm arg2,
HttpServletRequest arg3, HttpServletResponse arg4)
throws IOException, ServletException {
System.out.println(&quot;after interceptor......&quot;);
}
public void beforeAction(Action arg0, ActionMapping arg1, ActionForm arg2,
HttpServletRequest arg3, HttpServletResponse arg4)
throws IOException, ServletException {
System.out.println(&quot;before interceptor......&quot;);
}
}
这里也可以继承自ComponentInterceptor类。
b.拦截器配置文件interceptor-config.xml

<?xml version="1.0" encoding="UTF-8"?>
<interceptor-config>
    <interceptor name="displayInterceptor" type="com.test.DisplayInterceptor"/>

<default-interceptors>
  <interceptor name="displayInterceptor"/>
</default-interceptors>
</interceptor-config>

c.struts配置文件中配上该插件
   <plug-in className="net.sf.struts.saif.SAIFPlugin">
      <set-property property="interceptor-config" value="/WEB-INF/interceptor-config.xml"/>
    </plug-in>
d.部署完毕有即可测试。


2、saif-spring.jar+spring-webmvc-struts.jar


a.实现拦截器


package com.test;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class DisplaySpringInterceptor implements net.sf.struts.saif.ActionHaveForwardInterceptor  {
public ActionForward afterAction(Action arg0, ActionMapping arg1,
ActionForm arg2, HttpServletRequest arg3, HttpServletResponse arg4)
throws IOException, ServletException {
System.out.println(&quot;after Spring interceptor......&quot;);
return arg1.findForward(&quot;index0&quot;);
}

public ActionForward beforeAction(Action arg0, ActionMapping arg1,
ActionForm arg2, HttpServletRequest arg3, HttpServletResponse arg4)
throws IOException, ServletException {
System.out.println(&quot;before Spring interceptor......&quot;);
return arg1.findForward(&quot;index0&quot;);
}
}

b.拦截器配置文件interceptor-config1.xml
<?xml version="1.0" encoding="UTF-8"?>
<interceptor-config>
    <interceptor name="displayInterceptor1" type="com.test.DisplaySpringInterceptor"/>

   <action type="/browser">
          <interceptor name="displayInterceptor1" />
    </action>
<!--
<default-interceptors>
  <interceptor name="displayInterceptor1"/>
</default-interceptors>-->
</interceptor-config>
c.struts配置文件中配上该插件
   <plug-in className="net.sf.struts.saif.SAIFSpringPlugin">
      <set-property property="interceptor-config" value="/WEB-INF/interceptor-config1.xml"/>
    </plug-in>
d.部署完毕有即可测试。

注意:测试时要屏蔽一个插件,不能同时使用。

区别:1)前者不能针对某个action进行拦截而后者可以,但不能拦截到具体的方法;因此前者配置为默认拦截,后者可以默认也可以指定action,<action type="/browser"> 这里是action的名称。
2)前者依次执行完 beforeAction 、action和afterAction后跳转到action的ActionForward路径。后者执行beforeAction后如果该方法返回的ActionForward不为null就会执行跳转因此action将不会执行,如果为null则继续执行action;同样action的ActionForward不为null也执行跳转,afterAction则不会执行;否则就执行并跳转到afterAction的ActionForward路径。

以上执行逻辑与反编译后的代码是一致的,代码如下:
saif.jar ->net.sf.struts.saif.SAIFRequestProcessor


/*    */   protected ActionForward processActionPerform(HttpServletRequest request, HttpServletResponse response, Action action, ActionForm form, ActionMapping mapping)
/*    */     throws IOException, ServletException
/*    */   {
/* 89 */     this.helper.beforeAction(request, response, action, form, mapping);
/*    */
/* 91 */     ActionForward forward = super.processActionPerform(request, response, action, form, mapping);
/*    */
/* 94 */     this.helper.afterAction(request, response, action, form, mapping);
/*    */
/* 96 */     return forward;
/*    */   }
/*    */ }



saif-spring.jar ->net.sf.struts.saif.SAIFSpringRequestProcessor

protected ActionForward processActionPerform(HttpServletRequest request, HttpServletResponse response, Action action, ActionForm form, ActionMapping mapping)
/*    */     throws IOException, ServletException
/*    */   {
/* 58 */     ActionForward forward = this.helper.beforeAction(request, response, action, form, mapping);
/* 59 */     if (forward != null) {
/* 60 */       return forward;
/*    */     }
/* 62 */     forward = super.processActionPerform(request, response, action, form, mapping);
/* 63 */     if (forward != null) {
/* 64 */       return forward;
/*    */     }
/* 66 */     forward = this.helper.afterAction(request, response, action, form, mapping);
/*    */
/* 68 */     return forward;
/*    */   }
/*    */ }


这2个实现都继承org.apache.struts.action.RequestProcessor。

完整的工程在附件中。
分享到:
评论

相关推荐

    struts1.2拦截器使用插件

    struts1.2拦截器使用插件

    Struts1 拦截器(SAIF)的使用

    完整可运行起来的SSH项目!中使用了SAIF 插件 实现拦截器的效果!并且有sql语句 !我使用的是mysql数据库,如果你使用别的数据库请改配置文件!运行成功后注意控制台的输出

    saif(struts1 interceptor)

    struts1 拦截器功能的实现。struts2 中的interceptor很方便。但在struts1中没有这个功能,用这个插件很容易实现。

    Struts2 in action中文版

    9.2.2 使用自动连线将依赖注入到动作、拦截器和结果 205 9.3 为什么在Struts 2中使用JPA 207 9.3.1 使用JPA和Hibernate建立项目 208 9.3.2 基于Spring管理JPA编写代码 212 9.4 小结 216 第10章 探索验证框架 217 ...

    struts2.0.jar

    · 引入拦截器: Struts 2为拦截器(interceptor)提供了全面支持。拦截器可在Action类执行前后加以执行。拦截器经配置后,可以把工作流程或者验证等常见功能作用到请求上。所有请求通过一组拦截器传送,之后再发送到...

    深入浅出Struts2(附源码)

    18.3 使用DataSourceInjectorInterceptor拦截器 269 18.4 小结 273 第19章定制结果类型 274 19.1 概述 274 19.2 编写一个自定义的结果类型 274 19.3 使用新的结果类型 277 19.4 小结 279 第20章 Velocity 280...

    深入浅出struts2

    开发人员还可以通过拦截器(可以自定义拦截器或者使用Struts2提供的拦截器)来对请求进行预处理和后处理,这样一来,处理请求就变得更加模块化,从而进一步减小耦合度。模块化是一个通用的主题——可以通过插件机制...

    自定义拦截器(实现异常处理+细颗粒权限控制).zip

    struts2的拦截器实现细颗粒度权限控制;struts2的拦截器实现异常处理;源代码;页面;教程

    struts项目学习笔记

    基于AOP(面向切面编程)思想的拦截器机制,更易扩展(不修改源代码的条件下,增强代码功能) 更强大、更易用输入校验功能 整合Ajax支持:json插件 Struts2的今生前世: 1.早期开发模型Servlet+JSP+JavaBean显得...

    整合struts2和spring源代码(可以直接在tomcat中运行)

    可以直接运行,并对整合spring和struts2步骤及需要注意的事项进行类总结 整合spring和struts2总结 1.将struts2和spring中的库文件复制...— 如果没有使用Spring ObjectFactory,提供了2个拦截器来自动装配action。

    深入浅出Struts 2 .pdf(原书扫描版) part 1

    18.3 使用DataSourceInjectorInterceptor拦截器 269 18.4 小结 273 第19章 定制结果类型 274 19.1 概述 274 19.2 编写一个自定义的结果类型 274 19.3 使用新的结果类型 277 19.4 小结 279 第20章 Velocity 280 ...

    Struts2技术手册-Struts2精华教程-电子书

    .....XWork拦截器 .....XWork转换器 .....XWork校验器_使用 .....XWork校验器_定义 .....标签库_简介与OGNL .....标签库_主题与模板 .....标签库_非表单标签 .....标签库_表单标签 .....国际化 .....token令牌 ........

    struts2教程ppt

    struts2教程,一共有八个ppt。内容丰富。有:1、struts2配置与运行。2、类型转换器。3、 数据较验。4、拦截器。5、文件上传与下载。6、struts2国际化。7、 标签介绍。8、Struts2的插件。

    Struts2的使用-实验报告.docx

    框架的配置主要包括创建 struts.xml 配置文件,其中定义了各个组件的映射关系、拦截器等。通过配置文件,我们可以指定请求的处理流程,以及请求到达时执行的操作。这为开发者提供了更大的灵活性,能够根据业务需求...

    Struts2开发最简单学生后台管理系统

    使用了Struts2.3.24版本,做了个简单的增删改查,登录拦截器,防止URL输入。适合基础的同学学习交流。 运行环境 jdk7(8)+tomcat7(8)+mysql+IntelliJ IDEA 项目技术(必填) struts2+jdbc+struts-tags+拦截器

    jcaptcha4struts2:从 code.google.compjcaptcha4struts2 自动导出

    通过在您的操作(处理验证码输入)中扩展提供的基本操作类,或通过配置拦截器来启用 JCaptcha 支持。 ##2。 在您的应用程序中启用 JCaptcha 支持 有两种方法可以让您的应用程序使用此插件使用 JCaptcha。 从提供的 ...

Global site tag (gtag.js) - Google Analytics