拦截器从字面意思来看就是限制。限制用户訪问某些网页。在Action提出请求之前用拦截器来做权限设置,让符合的用户跳入对应的界面中。近期做的一个商城项目中就用到了自己定义的拦截器,实现了一个简单的session推断。成功就登陆,不成功就跳转到门户。

 

【拦截器工作原理】

Struts2拦截器是在訪问某个Action或Action的某个方法。字段之前或之后实施拦截,而且Struts2拦截器是可插拔的。拦截器是AOP的一种实现。AOP为Aspect Oriented Programming的缩写。意为:面向切面编程。通过预编译方式和执行期动态代理实现程序功能的统一维护的一种技术。

利用AOP能够对业务逻辑的各个部分进行隔离。从而使得业务逻辑各部分之间的耦合度减少,提高程序的可重用性,同一时候提高了开发的效率。

   我的理解是每个action请求都会在拦截器的内部,当发生请求的时候,Struts会找相应的配置文件。实例化相应的拦截器对象,如设置拦截器属性excludeMethods。

非常类似于过滤器的。可是过滤器的范围要大于拦截器。

【创建拦截器】

  第一种方法就是直接实现Interceptor接口。这种话,有个弊端。这里面的init()  destroy()  intercept()  方法都须要实现了。

  另外一种方法是继承自AbstractInterceptor抽象类。实现了Interceptor接口,而且对里面的init()和destroy()方法进行空实现,而把intercept()方法设置为抽象方法,让继承它的子类去实现,这种话。子类仅仅要实现这个intercept()方法就能够了。比上面的简单。



  第三种方法是继承自MethodFilterInterceptor(我以下就是用这种方法实现的),这个类叫做方法过滤拦截器,MethodFilterInterceptor继承自AbstractInterceptor,而且提供了一种机制,即能够指定对Action中某些方法进行拦截或者是不拦截,所谓拦截不拦截。指的就是拦截器中的intercept()方法是否被运行了,若没有运行,就是没有拦截,若运行了,就是拦截了。



  总的来说,相比之前的接口和抽象类,最后一个仅仅须要指定对Action中某些方法进行拦截是最简单的。

是通过includeMethods和excludeMethods这两个參数来设置那个方法须要拦截的,在Struts.xml中进行配置。

【拦截器实现登陆】

1>首先我们要自己定义一个拦截器类:

  1. package cn.itcast.shop.interceptor;
  2. import org.apache.struts2.ServletActionContext;
  3. import cn.itcast.shop.adminuser.vo.AdminUser;
  4. import com.opensymphony.xwork2.ActionInvocation;
  5. import com.opensymphony.xwork2.ActionSupport;
  6. import com.opensymphony.xwork2.interceptor.MethodFilterInterceptor;
  7. /**
  8. * interceptor
  9. * @author Zhouzhou
  10. * @date 2016-1-25
  11. * @time 下午09:48:21
  12. */
  13. /**
  14. * that class want to judge the session is null。it extends the class named : MethodFilterInterceptor,
  15. */
  16. public class PrivilegeInterceptor extends MethodFilterInterceptor{
  17.  
  18. /**
  19. * the method doIntercept is the main method of the MethodFilterInterceptor
  20. */
  21. @Override
  22. protected String doIntercept(ActionInvocation actionInvocation) throws Exception {
  23. //judge login
  24. //
  25. AdminUser adminUser = (AdminUser) ServletActionContext.getRequest()
  26. .getSession().getAttribute("existAdminUser");
  27. if(adminUser != null){
  28. // is logined ,have the userinfo in the session
  29. return actionInvocation.invoke();
  30. }else{
  31. // is not login
  32. ActionSupport support = (ActionSupport) actionInvocation.getAction();
  33. support.addActionError("You are not logged in!No permission to access!");
  34. return ActionSupport.LOGIN;
  35. }
  36. }
  37. }

2>然后在配置文件里去注冊这个拦截器,并在后台登陆的action中利用excludeMethods这个成员变量来绑定login这种方法被拦截。

  1. struts.xml
  2. <!-- Configure: registered place interceptor -->
  3. <interceptors>
  4. <interceptor name="privilegeInterceptor" class="cn.itcast.shop.interceptor.PrivilegeInterceptor"/>
  5. </interceptors>
  6. <!-- 后台登录Action -->
  7. <action name="adminUser_*" class="adminUserAction" method="{1}">
  8. <result name="loginFail">/admin/index.jsp</result>
  9. <result name="loginSuccess" type="redirect">/admin/home.jsp</result>
  10. <interceptor-ref name="privilegeInterceptor">
  11. <param name="excludeMethods">login</param>
  12. </interceptor-ref>
  13. <interceptor-ref name="defaultStack"/>
  14. </action>

3>然后是在login 这种方法被调用的时候,就去验证session是不是空的,来推断是否有这个username和password的匹配,然后放入了session中。

  1. /**
  2. * login method--azz
  3. */
  4. public String login() {
  5. User existUser = userService.login(user);
  6. // use the username and pwd check the user is exist
  7. if (existUser == null) {
  8. //login fail
  9. this.addActionError("error。your user or pwd is error, please login again...");
  10. return LOGIN;
  11. } else {
  12. // login success
  13. // set the user information into the session
  14. ServletActionContext.getRequest().getSession()
  15. .setAttribute("existUser", existUser);
  16. // goto the struts.xml ↓
  17. //<result name="loginSuccess" type="redirect">/admin/home.jsp</result>
  18. return "loginSuccess";
  19. }
  20.  
  21. }

  这样一来就轻松实现了用户登陆时候,session是否为空的推断了。

【总结】

 1.拦截器定义了一种机制,能够让开发定义一些代码在action运行之前&后运行。



 2.提高了代码的复用。想想我们开发asp.net的时候总是写上if(session["username"]==null)之类的话在if(!Page。

ispostback)里面。

每一个页面都有去推断session。

在我们有了这个拦截器之后呢,我们仅仅须要在每一个action配置里面写上:

  1. <interceptor-ref name="拦截器名称"/>
  2. <interceptor-ref name="defaultStack"/>

这样就攻克了全部页面的权限验证问题了。

 

 3.当中includeMethods和excludeMethods是来决定这个action里面的方法是否须要被拦截。

SSH进阶(2)——用Struts拦截器实现登陆限制的更多相关文章

  1. [置顶] 使用struts拦截器+注解实现网络安全要求中的日志审计功能

    J2EE项目中出于安全的角度考虑,用户行为审计日志功能必不可少,通过本demo可以实现如下功能: 1.项目中记录审计日志的方法. 2.struts拦截器的基本配置和使用方法. 3.struts拦截器中 ...

  2. 利用Struts拦截器限制上传图片的格式和大小

    在这之前 Struts的一个核心功能就是大量的拦截器,既然是框架,那么自然也就贴心地为我们准备好了各种常用的功能,比如这里即将讨论的如何限制上传图片的格式和大小.那么既然是使用Struts已经写好的拦 ...

  3. Struts拦截器设置完的值为什么在页面取不到

    Struts拦截器设置完的值为什么在页面取不到. ActionContext ac = (ActionContext) invocation.getInvocationContext(); ac.pu ...

  4. struts 拦截器 Interceptor

         拦截器是AOP中的概念,它本身是一段代码,可以通过定义“织入点”,来指定拦截器的代码在“织入点”的前后执行,从而起到拦截的作用.正如上面 Struts2的Reference中讲述的,Stru ...

  5. struts拦截器实现原理

    图1: 上1来源于Struts2官方站点,是Struts 2 的整体结构. 一个请求在Struts2框架中的处理大概分为以下几个步骤 1 客户端初始化一个指向Servlet容器(例如Tomcat)的请 ...

  6. struts拦截器

    struts中的拦截器相当于过滤器的作用 一在struts.xml中配置拦截器或拦截器栈 <interceptors>!--全部的拦截器 <interceptor name=&quo ...

  7. (转)Struts 拦截器

    一.拦截器是怎么实现: 实际上它是用Java中的动态代理来实现的 二.拦截器在Struts2中的应用 对于Struts2框架而言,正是大量的内置拦截器完成了大部分操作.像params拦截器将http请 ...

  8. struts——拦截器

    什么是拦截器 拦截器(Interceptor)是Struts 2的一个强有力的工具,有许多功能都是构建于它之上,如国际化(前两篇博客介绍过).转换器,校验等. 拦截器是动态拦截Action调用的对象. ...

  9. Struts拦截器使用

    创建拦截器java程序 package cn.itcast.oa.util; import com.opensymphony.xwork2.ActionInvocation; import com.o ...

随机推荐

  1. 记一次 Apache HUE 优化之因使用 Python 魔术方法而遇到的坑

    最近的工作是基于 Apache HUE 做二次开发.刚接手 HUE 的代码的时候,内心是崩溃的:开源的代码,风格很多种, 代码比较杂乱; 虽是基于 Django 开发的,但是项目的结构改变很大; 很多 ...

  2. Python的主要库

    本文在Creative Commons许可证下发布 市面上的分析工具大致分为两大类,菜单式的工具和命令行式的工具.前者适合于初学入门,类似于跟团旅游,提供了固定的路线.分析套路比较固定化,点几下鼠标就 ...

  3. WPF中Image控件的Source属性

    原文:WPF中Image控件的Source属性 imgBook 是一个Image控件,在后台代码中我想给它指定Source的属性.我先如下方式进行: Uri uri = new Uri(strImag ...

  4. 单向链表 golang

    package main import "fmt" type Object interface {} //节点 type Node struct { data Object nex ...

  5. C++中父类的虚函数必需要实现吗?

    一.情景 C++中父类的虚函数必需要实现吗? class Vir{ public: virtual void tryVirtual(); }; class CVir:public Vir{ publi ...

  6. iOS项目开发实战——学会使用TableView列表控件(二)

    要在iOS开发中使用TableView列表控件,不仅能够直接使用TableViewController作为整个主界面,并且还能够使用TableView控件来实现.使用TableView能够进行很多其它 ...

  7. crontab FAQ

    1.crontab变量问题 crontab中的脚本须要引入系统变量才干找到,否则crontab中的命令找不到系统变量,或者都写绝对路径. 2.1分钟运行一次 */1 * * * * /etc/keep ...

  8. OJ刷题---猜算式

    题目要求: 输入代码: #include<iostream> using namespace std; void Calc(); int main() { Calc(); return 0 ...

  9. 在jsp页面中导入BootStrap中的文件

    BootStrap应该放在项目的根目录下面 然后因为我的jsp页面跟html页面是写在HTML文件夹中,所以我路径的导入应该像下面的图一样,退回到上级目录再写路径.

  10. PopupMenu-使用实例跟监听事件

    今天需要给一个控件添加弹出菜单功能.就顺便学习了下popupMenu的使用,记录下来. 它的使用其实也非常的简单,看如下代码 popupMenu = new PopupMenu(MainActivit ...