一、编写一个类实现com.opensymphony.xwork2.interceptor.Interceptor 接口

PermissionInterceptor.java

<pre name="code" class="java">package cn.sky.bookshop.interceptor;

import org.apache.struts2.ServletActionContext;

import cn.sky.bookshop.utils.DateUtil;

import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.Interceptor; public class PermissionInterceptor implements Interceptor { private static final long serialVersionUID = -1908127830415801520L; private String includeUrl; public void destroy() {
} public void init() {
} public String intercept(ActionInvocation invocation) throws Exception {
//会话session中取出uname
Object obj = ServletActionContext.getRequest().getSession().getAttribute("uname");
//获取访问路径
String path = ServletActionContext.getRequest().getServletPath();
//如果存在uname或者访问路径包含在includeUrl中
if (obj != null || includeUrl.contains(path)) {
return invocation.invoke(); //继续调用下一个拦截器,如果没有则执行Action
}
ServletActionContext.getRequest().setAttribute("errorInfo","No permission to operate the page");//保存错误信息
return "disallow";//返回视图
} public String getIncludeUrl() {
return includeUrl;
} public void setIncludeUrl(String includeUrl) {
this.includeUrl = includeUrl;
}
}

二、配置struts.xml 文件



(1)定义一个自己的默认包:

	<package name="my-struts-default" namespace="/" extends="struts-default">

		<interceptors>
<!-- 这里是定义一个拦截器 -->
<interceptor name="permission" class="cn.sky.bookshop.interceptor.PermissionInterceptor">
<!-- 拦截器的初始化注入参数 -->
<param name="includeUrl">/user/user_login.action,/getcode</param>
</interceptor>
<!-- 这里定义一个拦截器栈 -->
<interceptor-stack name="myDefalutStack">
<interceptor-ref name="defaultStack"></interceptor-ref>
<interceptor-ref name="permission"></interceptor-ref>
</interceptor-stack>
</interceptors>
<!--设置默认的拦截器栈(访问Action前默认调用)-->
<default-interceptor-ref name="myDefalutStack"></default-interceptor-ref>
<!-- 全局result -->
<global-results>
<result name="disallow" type="redirect">/login.jsp</result>
</global-results>
<action name="getcode" class="cn.sky.bookshop.action.VerifyCodeAction">
</action> </package>

(2)以后的包都继承上面my-struts-default





注意:这里只能实现对访问Action时实现拦截,对jsp的访问不可能实现拦截。(可以将自定义拦截器栈中的默认拦截器

<pre name="code" class="html">defaultStack注释掉,访问jsp发现根本没执行过此拦截器。可想而知,在拦截器工作之前对jsp的页面请求已经做出响应了。)



												

个人笔记--struts2对Action的权限拦截的更多相关文章

  1. 个人笔记--Servlet之过滤器实现权限拦截

    一.编写一个Java类实现javax.servlet.Filter接口 package cn.edu.sxu.filter; import java.io.IOException; import ja ...

  2. java反射--注解的定义与运用以及权限拦截

    自定义注解类编写的一些规则: 1. Annotation型定义为@interface, 所有的Annotation会自动继承java.lang.Annotation这一接口,并且不能再去继承别的类或是 ...

  3. [原创]java WEB学习笔记74:Struts2 学习之路--自定义拦截器,struts内建的拦截器

    本博客的目的:①总结自己的学习过程,相当于学习笔记 ②将自己的经验分享给大家,相互学习,互相交流,不可商用 内容难免出现问题,欢迎指正,交流,探讨,可以留言,也可以通过以下方式联系. 本人互联网技术爱 ...

  4. Struts2之Action接收请求参数和拦截器

    技术分析之在Struts2框架中使用Servlet的API        1. 在Action类中也可以获取到Servlet一些常用的API        * 需求:提供JSP的表单页面的数据,在Ac ...

  5. Struts2 In Action笔记_页面到动作的数据流入和流出

    因为回答百度知道的一个问题,仔细查看了<Struts2 In Action>,深入细致的看了 “数据转移OGNL 和 构建视图-标签”,很多东西才恍然大悟. 一直觉得国外写的书很浮,不具有 ...

  6. Struts2的Action继承ActionSupport时,利用AOP来拦截Action出现NoSuchMethodException

    参考:http://zhanghua.1199.blog.163.com/blog/static/46449807201111139501298/ 做项目的时候,由于要用到在Struts2的Actio ...

  7. Struts2使用Interceptor实现权限控制的应用实例详解

    Struts2使用Interceptor实现权限控制的应用实例详解 拦截器:是Struts2框架的核心,重点之重.因此,对于我们要向彻底学好Struts2.0.读源码和使用拦截器是必不可少的.少说了. ...

  8. Struts2学习:interceptor(拦截器)的使用

    对于需要登陆验证.权限验证等功能的网站,每一次请求,每一个action都写一段验证的代码,未免显得冗余且不易维护.struts2提供了拦截器interceptor,为这些页面提供一个切面,或者说公共组 ...

  9. Struts2重新学习之自定义拦截器(判断用户是否是登录状态)

    拦截器 一:1:概念:Interceptor拦截器类似于我们学习过的过滤器,是可以再action执行前后执行的代码.是web开发时,常用的技术.比如,权限控制,日志记录. 2:多个拦截器Interce ...

随机推荐

  1. Foundation: NSNotificationCenter

    一个NSNotificationCenter对象(通知中心)提供了在程序中广播消息的机制,它实质上就是一个通知分发表.这个分发表负责维护为各个通知注册的观察者,并在通知到达时,去查找相应的观察者,将通 ...

  2. 集合练习——List部分

    利用ArrayList 1.存储多个员工信息,包括工号,姓名,年龄,入职时间,逐条打印所有员工姓名,并输出员工个数. package CollectionPart; import java.util. ...

  3. [置顶] asp.net(c#)中相对路径(虚拟路径)和物理磁盘路径的转换

    物理路径就是磁盘路径,也就是说是在磁盘上的位置,虚拟路径也就是web页面上的路径,是相对于应用程序而言的 /// <summary> /// 将物理路径转换成相对路径 /// </s ...

  4. 图解I/O的五种模型

    1.1 五种I/O模型 1)阻塞I/O 2)非阻塞I/O 3)I/O复用 4)事件(信号)驱动I/O 5)异步I/O 1.2 为什么要发起系统调用? 因为进程想要获取磁盘中的数据,而能和磁盘打交道的只 ...

  5. Cygwin下安装vim后,vim中退格键无法正常使用

    问题描述: 在Cygwin中安装完vim后 进入vim,发现上下左右键和退格键都无法正常使用 问题分析: 首先考虑到的就是缺少vim的配置文件,首先查看/etc路径下是否有vim的配置文件 admin ...

  6. Orcle11g用户密码恢复

    1.当安装Orcle11g后,很久不用,忘记了用户名和密码.可以通过以下方法重置: 如上图及重置用户sys,system密码为123

  7. acmer -- 最美的情书

    2014-09-29 20:51:45 POJ 2482 Fleeting time does not blur my memory of you. Can it really be 4 years ...

  8. SMTP ERROR: Password command failed: 535 Incorrect authentication data

    在处理一个使用PHPMailer来发送电邮,我在本地使用我的163邮箱来做测试发送电邮,能够成功的发送电邮:当上传到正式平台时,出现了,类似这样的错误信息 SMTP ERROR: Password c ...

  9. POJ3974 Palindrome

    本文版权归ljh2000和博客园共有,欢迎转载,但须保留此声明,并给出原文链接,谢谢合作. 本文作者:ljh2000 作者博客:http://www.cnblogs.com/ljh2000-jump/ ...

  10. ibatis配置xml文件中CDATA的用法

    ibatis作为一种半自动化的OR Mapping工具,其灵活性日益体现出来,越来越多的人都倾向于在项目中使用.由于Sql中经常有与xml规范相冲突的字符对xml映射文件的合法性造成影响.许多人都知道 ...