Java学习者论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

手机号码,快捷登录

恭喜Java学习者论坛(https://www.javaxxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,购买链接:点击进入购买VIP会员
JAVA高级面试进阶视频教程Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程

Go语言视频零基础入门到精通

Java架构师3期(课件+源码)

Java开发全终端实战租房项目视频教程

SpringBoot2.X入门到高级使用教程

大数据培训第六期全套视频教程

深度学习(CNN RNN GAN)算法原理

Java亿级流量电商系统视频教程

互联网架构师视频教程

年薪50万Spark2.0从入门到精通

年薪50万!人工智能学习路线教程

年薪50万!大数据从入门到精通学习路线年薪50万!机器学习入门到精通视频教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程 MySQL入门到精通教程
查看: 258|回复: 0

[struts学习]Struts2中简单的权限检查拦截器

[复制链接]
  • TA的每日心情
    开心
    2021-3-12 23:18
  • 签到天数: 2 天

    [LV.1]初来乍到

    发表于 2014-10-11 04:09:00 | 显示全部楼层 |阅读模式
    如果要开发自己的拦截器,应该实现com.opensymphony.xwork2.interceptor.Interceptor接口,该接口的定义如下:  public interface Interceptor extends Serializable{
        //销毁该拦截器之前的回调方法
        void destroy();

        //初始化该拦截器的回调方法
        void init();

         //拦截器实现拦截的逻辑方法
        String intercept(ActionInvocation invocation) throws Exception;
    }

        Struts 2提供了一个AbstractInterceptor类,该类提供了一个init和destroy方法的空实现,如果我们实现的拦截器不需要申请资源,则可以继承此类。  
      
       
       
         
       

         
       
      
        下面实例应用要求用户登录,且必须为指定用户名才可以查看系统中某个视图资源;否则,系统直接转入登录页面。
    检查用户是否登录,通常都是通过跟踪用户的Session来完成,通过ActionContext即可访问到Session中的属性,拦截器的
    intercepte(ActionInvocation invocation)方法的invocation参数可以很轻易地访问到请求相关的ActionContext实例。

    权限检查拦截器类的代码如下:
    1. package lee;
    2. import com.opensymphony.xwork2.ActionInvocation;
    3. import com.opensymphony.xwork2.ActionSupport;
    4. import com.opensymphony.xwork2.Action;
    5. import com.opensymphony.xwork2.ActionContext;
    6. import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
    7. import java.util.*;

    8. /**
    9. * @author  yeeku.H.lee kongyeeku@163.com
    10. * @version  1.0
    11. * Copyright (C), 2005-2008, yeeku.H.Lee
    12. * This program is protected by copyright laws.
    13. * Program Name:
    14. * Date:
    15. */

    16. public class AuthorityInterceptor extends AbstractInterceptor
    17. {
    18.     public String intercept(ActionInvocation invocation) throws Exception
    19.         {
    20.                 ActionContext ctx = invocation.getInvocationContext();
    21.                 Map session = ctx.getSession();
    22.                 String user = (String)session.get("user");
    23.                 if (user != null && user.equals("scott") )
    24.                 {
    25.                         return invocation.invoke();
    26.                 }
    27.                 ctx.put("tip" , "您还没有登陆,请输入scott,tiger登陆系统");
    28.                 return Action.LOGIN;
    29.     }
    30. }
    复制代码
    它要拦截的动作是viewBook.action,这要在struts.xml中配置

    <?xml version="1.0" encoding="GBK"?>
    <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" "http://struts.apache.org/dtds/struts-2.0.dtd">
    <struts>
       <constant name="struts.custom.i18n.resources" value="globalMessages"/>
       <constant name="struts.i18n.encoding" value="GBK"/>    <package name="lee" extends="struts-default">
        <interceptors>
          <interceptor name="authority" class="lee.AuthorityInterceptor"/>
        </interceptors>    <global-results>
         <result name="login">/login.jsp</result>
       </global-results>    <action name="login" class="lee.LoginAction">
        <result name="error">/error.jsp</result>
        <result name="success">/welcome.jsp</result>
       </action>

       <action name="viewBook">
         <result>/WEB-INF/jsp/viewBook.jsp</result>
         <!-- 拦截器一般配置在result元素之后! -->
        <interceptor-ref name="defaultStack"/>
         <interceptor-ref name="authority"/>
       </action>
    </package>
    </struts> 运行图:
       

      
      
       
       

         
       

         
       
      
    复制代码

    源码下载:http://file.javaxxz.com/2014/10/11/040900343.zip
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|Java学习者论坛 ( 声明:本站资料整理自互联网,用于Java学习者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2025-2-26 07:08 , Processed in 0.301441 second(s), 34 queries .

    Powered by Discuz! X3.4

    © 2001-2017 Comsenz Inc.

    快速回复 返回顶部 返回列表