一般,我们的web应用都是只有在用户登录之后才允许操作的,也就是说我们不允许非登录认证的用户直接访问某些页面或功能菜单项。对于个别页面来说,可能不需要进行拦截,此时,如果项目采用struts view框架来做的话,我们可以用strut的拦截器做一个intercepter类。

首先在项目中独立一个包出来

  我们写一个拦截器类,让它继承 MethodFilterInterceptor。

 /**
* 登陆拦截器
* @author Administrator
*
*/
public class LoginIntercepter extends MethodFilterInterceptor{ @Override
protected String doIntercept(ActionInvocation invoke) throws Exception {
Map session = invoke.getInvocationContext().getSession(); //從上下文中獲取session會話,結果為MAP集合,裏面是以json格式保存的相關信息
Object user = session.get("user"); //獲取session會話中的會員名稱
if(user != null){
return invoke.invoke(); //對該操作不攔截
}else{
invoke.getInvocationContext().put("tip","您还没有登录,请登陆系统");
return "loginUI"; //返回登陸界面
}
} }

加入配置文件interceoper-struts.xml ,其中 name="intercepter" 作为独立的一个全局的package 其他的配置文件只需加入extends="intercepter"即可。尤其

      <param name="excludeMethods">xx,xxx</param> 这一代码是过滤的方法 ,即不进行拦截的方法。把对应的方法名放入即可,中间用逗号分隔。
      <param name="includeMethods">xx,xxx</param> 这行代码意思,进行拦截的方法,把对应的方法名放入即可,中间用逗号分隔。
 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts>
<package name="intercepter" extends="struts-default">
<interceptors>
<!-- 配置未登录进行操作的拦截器 -->
<interceptor name="loginInterceptor" class="com.chinawaf.intercepter.action.LoginIntercepter" />
<!-- 重新封装一个默认的拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="loginInterceptor" >
<!-- 设置方法名 ,对应的方法不做拦截 -->
<param name="excludeMethods">login,getVerriCode</param>
</interceptor-ref>
<interceptor-ref name="defaultStack" />
</interceptor-stack>
</interceptors>
<!-- 为这个包设置默认的拦截器栈 -->
<default-interceptor-ref name="myStack" /> <global-results>
24 <result name="loginUI">/WEB-INF/jsp/afterend/login.jsp</result>
25 </global-results> </package> </struts>

然后在struts.xml中进行导入:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <!-- 禁用动态方法访问 -->
<constant name="struts.enable.DynamicMethodInvocation" value="false" />
<!-- 配置成开发模式 -->
<constant name="struts.devMode" value="false" />
<!-- 配置拓展名为action -->
<constant name="struts.action.extention" value="action"/>
<!-- 把主题配置成simple -->
<constant name="struts.ui.theme" value="simple" /> <include file="com/chinawaf/intercepter/config/intercepter-struts.xml"></include>
<include file="com/chinawaf/backLogin/conf/backLogin-struts.xml"></include>
<include file="com/chinawaf/home/conf/home-struts.xml"></include>
<include file="com/chinawaf/rtmp/log/conf/rtmpLog-struts.xml"></include>
<include file="com/chinawaf/rtmp/video/conf/rtmpVideo-struts.xml"></include>
<include file="com/chinawaf/rtmp/pvw/conf/rtmpPvw-struts.xml"></include>
<include file="com/chinawaf/rtmp/waf/conf/rtmpWaf-struts.xml"></include>
<include file="com/chinawaf/login/conf/login-struts.xml"></include>
<include file="com/chinawaf/backstage/user/conf/user-struts.xml"></include>
<include file="com/chinawaf/backstage/servicePage/conf/serve-struts.xml"></include>
<include file="com/chinawaf/rtmp/video/conf/rtmpVideo-struts.xml"></include>
<include file="com/chinawaf/backstage/api/conf/api-struts.xml"></include>
<include file="com/chinawaf/rtmp/monitoring/conf/rtmpMonitoring-struts.xml"></include>
<include file="com/chinawaf/cdn/cdn/conf/cdnCdn-struts.xml"></include>
<include file="com/chinawaf/cdn/list/conf/cdnList-struts.xml"></include>
<include file="com/chinawaf/cdn/cache/conf/cdnCache-struts.xml"></include>
<include file="com/chinawaf/cdn/flow/conf/cdnFlow-struts.xml"></include>
<include file="com/chinawaf/cdn/manage/conf/cdnManage-struts.xml"></include>
<include file="com/chinawaf/cdn/waf/conf/cdnWaf-struts.xml"></include>
<include file="com/chinawaf/cdn/visit/conf/cdnVisit-struts.xml"></include>
<include file="com/chinawaf/cdn/visitor/conf/cdnVisitor-struts.xml"></include>
</struts>

其他的配置文件进行继承,例如登录的xml:

 <?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
"http://struts.apache.org/dtds/struts-2.3.dtd"> <struts> <package name="sysLoginAction" namespace="/" extends="intercepter">
<action name="randPic" class="com.chinawaf.login.action.RandomAction" method="getVerriCode">
<result type="stream">
<param name="contentType">image/jpeg</param>
<param name="inputName">inputStream</param>
</result>
</action> <action name="login_*" class="com.chinawaf.login.action.LoginAction" method="{1}">
<result name="loginUI">/WEB-INF/jsp/client/login.jsp</result>
<result name="home" type="redirectAction">
<param name="actionName">home</param>
</result>
</action>
</package> </struts>

最后,struts 还可有其他的拦截器类,如:AbstractInterceptor  ,可以继承此类进行url地址的拦截。

关于 struts2 拦截器的详细介绍,可以参考这篇文章:http://wenku.baidu.com/link?url=GPQkMm2UkyFeRPMWH7RMTqJD1xVV2DWU90b5FwuNzDQjuAnx300jMePNb1IVgxMkM3dek3Irixm-kuTOmClbSQ8vdyEjRPwmAdXqZOlG_GK

struts 用拦截器进行用户权限隔离,未登录用户跳到登录界面 *** 最爱那水货的更多相关文章

  1. jsp+js完成用户一定时间未操作就跳到登录页面

    <% String path = request.getContextPath(); String basePath = request.getScheme() + "://" ...

  2. Struts的拦截器

    Struts的拦截器 1.什么是拦截器 Struts的拦截器和Servlet过滤器类似,在执行Action的execute方法之前,Struts会首先执行Struts.xml中引用的拦截器,在执行完所 ...

  3. 【Java EE 学习 75 下】【数据采集系统第七天】【二进制运算实现权限管理】【使用反射初始化权限表】【权限捕获拦截器动态添加权限】

    一.使用反射动态添加权限 在该系统中,我使用struts2的时候非常规范,访问的Action的形式都是"ActionClassName_MethodName.action?参数列表" ...

  4. Struts 2 拦截器

    什么是Struts 2 拦截器  拦截器就是当用户请求后台Action类时在Action的Excute()方法执行前和Result返回魔板试图之后(将页面(数据)发送给浏览器渲染之前)所需要的一些通用 ...

  5. linux普通用户权限设置为超级用户权限方法、sudo不用登陆密码

    以用户zato为例 普通用户权限设置为超级用户权限 进入有超级用户权限的账号 添加文件可写(w)权限 sudo chmod u+x /etc/sudoers 编辑/etc/sudoers文件 添加语句 ...

  6. JavaWeb -- Struts 自定义拦截器, 登录权限拦截

    1. 自定义拦截器, 登录权限拦截 login.jsp 登录JSP <%@ page language="java" contentType="text/html; ...

  7. struts自定义拦截器

    第01步:配置web.xml,启动struts框架 <?xml version="1.0" encoding="UTF-8"?> <web-a ...

  8. Struts2 自定义拦截器实例—登陆权限验证

    实现一个登陆权限验证的功能 message.jsp: <body> message:${message } </body> login.jsp: <% request.g ...

  9. Struts自定义拦截器&拦截器工作原理

    0.拦截器的调用原理: 拦截器是一个继承了序列化接口的普通接口.其工作原理是讲需要被拦截的对象作为参数传到intercept()方法内,在方法内部对此对象进行处理之后再执行原方法.intercept( ...

随机推荐

  1. rabbitmq消息队列——"topic型交换器"

    在之前的章节中我们改进了我们的日志系统,我们使用direct型交换器代替了只能盲目广播消息的fanout型交换器,这使得我们可以有选择性地接收日志. 尽管使用direct型交换器改进了我们的日志系统, ...

  2. WPF入门教程系列十二——依赖属性(二)

    二. 依赖属性的优先级 由于WPF 允许我们可以在多个地方设置依赖属性的值,所以我们就必须要用一个标准来保证值的优先级别.比如下面的例子中,我们在三个地方设置了按钮的背景颜色,那么哪一个设置才会是最终 ...

  3. 在Windows系统搭建.NET Core环境并创建运行ASP.NET网站

    微软于6月27日在红帽DevNation峰会上 正式发布了.NET Core 1.0.ASP.NET 1.0和Entity Framework Core 1.0,其将全部支持Windows.OS X和 ...

  4. codeforces Gargari and Permutations(DAG+BFS)

    /* 题意:求出多个全排列的lcs! 思路:因为是全排列,所以每一行的每一个数字都不会重复,所以如果有每一个全排列的数字 i 都在数字 j的前面,那么i, j建立一条有向边! 最后用bfs遍历整个图, ...

  5. Uvaoj 10048 - Audiophobia(Floyd算法变形)

    1 /* 题目大意: 从一个点到达另一个点有多条路径,求这多条路经中最大噪音值的最小值! . 思路:最多有100个点,然后又是多次查询,想都不用想,Floyd算法走起! */ #include< ...

  6. Java多线程系列--“JUC原子类”04之 AtomicReference原子类

    概要 本章对AtomicReference引用类型的原子类进行介绍.内容包括:AtomicReference介绍和函数列表AtomicReference源码分析(基于JDK1.7.0_40)Atomi ...

  7. 使用bokeh-scala进行数据可视化

    目录 前言 bokeh简介及胡扯 bokeh-scala基本代码 我的封装 总结 一.前言        最近在使用spark集群以及geotrellis框架(相关文章见http://www.cnbl ...

  8. mysql 命令重命名表RENAME TABLE 句法

    mysql 命令重命名表RENAME TABLE 句法 RENAME TABLE tbl_name TO new_tbl_name[, tbl_name2 TO new_tbl_name2,...]更 ...

  9. HTML5第二节

    第二回合:HTML5的新特性 与之前的HTML4.01相比,HTML5增加了非常多的改变: ① 新的语义元素:<article>.<aside>.<figure>. ...

  10. [Node.js] 基于Socket.IO 的私聊

    原文地址:http://www.moye.me/2015/01/02/node_socket-io/ 引子 最近听到这么一个问题:Socket.IO 怎么实现私聊?换个提法:怎么定位到人(端),或者说 ...