struts 用拦截器进行用户权限隔离,未登录用户跳到登录界面 *** 最爱那水货
一般,我们的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 用拦截器进行用户权限隔离,未登录用户跳到登录界面 *** 最爱那水货的更多相关文章
- jsp+js完成用户一定时间未操作就跳到登录页面
<% String path = request.getContextPath(); String basePath = request.getScheme() + "://" ...
- Struts的拦截器
Struts的拦截器 1.什么是拦截器 Struts的拦截器和Servlet过滤器类似,在执行Action的execute方法之前,Struts会首先执行Struts.xml中引用的拦截器,在执行完所 ...
- 【Java EE 学习 75 下】【数据采集系统第七天】【二进制运算实现权限管理】【使用反射初始化权限表】【权限捕获拦截器动态添加权限】
一.使用反射动态添加权限 在该系统中,我使用struts2的时候非常规范,访问的Action的形式都是"ActionClassName_MethodName.action?参数列表" ...
- Struts 2 拦截器
什么是Struts 2 拦截器 拦截器就是当用户请求后台Action类时在Action的Excute()方法执行前和Result返回魔板试图之后(将页面(数据)发送给浏览器渲染之前)所需要的一些通用 ...
- linux普通用户权限设置为超级用户权限方法、sudo不用登陆密码
以用户zato为例 普通用户权限设置为超级用户权限 进入有超级用户权限的账号 添加文件可写(w)权限 sudo chmod u+x /etc/sudoers 编辑/etc/sudoers文件 添加语句 ...
- JavaWeb -- Struts 自定义拦截器, 登录权限拦截
1. 自定义拦截器, 登录权限拦截 login.jsp 登录JSP <%@ page language="java" contentType="text/html; ...
- struts自定义拦截器
第01步:配置web.xml,启动struts框架 <?xml version="1.0" encoding="UTF-8"?> <web-a ...
- Struts2 自定义拦截器实例—登陆权限验证
实现一个登陆权限验证的功能 message.jsp: <body> message:${message } </body> login.jsp: <% request.g ...
- Struts自定义拦截器&拦截器工作原理
0.拦截器的调用原理: 拦截器是一个继承了序列化接口的普通接口.其工作原理是讲需要被拦截的对象作为参数传到intercept()方法内,在方法内部对此对象进行处理之后再执行原方法.intercept( ...
随机推荐
- Storm 实战:构建大数据实时计算
Storm 实战:构建大数据实时计算(阿里巴巴集团技术丛书,大数据丛书.大型互联网公司大数据实时处理干货分享!来自淘宝一线技术团队的丰富实践,快速掌握Storm技术精髓!) 阿里巴巴集团数据平台事业部 ...
- GridView和DATAGRID前后台查询用法的比较
Grideview前台: <DIV class="mainDiv" id="GridWidth"> <ASP:GridView id=&quo ...
- Python中的字符串与字符编码
本节内容: 前言 相关概念 Python中的默认编码 Python2与Python3中对字符串的支持 字符编码转换 一.前言 Python中的字符编码是个老生常谈的话题,同行们都写过很多这方面的文章. ...
- python--基础学习(一)开发环境搭建,体验HelloWorld
python学习之前 最近想用python写爬虫,由于之前没接触过,所以从零开始,找了技术博文大概了解下基础. 印象比较深的是"python你不去认识它,可能没什么,一旦你认识了它,你就会爱 ...
- php基础教程-输出Hello World
<!DOCTYPE html> <!--!文档类型,一个文档类型标记是一种标准通用标记语言的文档类型声明, 它的目的是要告诉标准通用标记语言解析器,它应该使用什么样的文档类型定义(D ...
- Spark入门实战系列--2.Spark编译与部署(下)--Spark编译安装
[注]该系列文章以及使用到安装包/测试数据 可以在<倾情大奉送--Spark入门实战系列>获取 .编译Spark .时间不一样,SBT是白天编译,Maven是深夜进行的,获取依赖包速度不同 ...
- RHEL6 某业务用户ulimit -a命令找不到
最终确定是shell环境问题,临时改为/bin/bash即可查看. 1.问题现象 # su - jingyu $ id uid=(jingyu) gid=(jingyu) (jingyu) $ uli ...
- Codrops 优秀教程:实现效果精美的多层推拉菜单
Codrops 给我们分享了一个多层菜单的实现教程.他们试图探索创建一个嵌套的多级菜单,是非常有用的东西,可以有很多的内容,如网上商店的导航菜单. 这个 Push Menu 效果理论上可以包含无限嵌套 ...
- Entity Framework想说爱你不容易,这么多的报错,这么多的限制,该如何解决?
首先看一下采用MODEL FIRST的方式设计的实体模型对象关系图: 注意:EntityOne中有导航属性:EntityTwo 在如下代码中的几种情况进行新增操作,均会报错,新增都不会成功: stat ...
- 从零开始学习jQuery (一) 入门篇
本系列文章导航 从零开始学习jQuery (一) 入门篇 一.摘要 本系列文章将带您进入jQuery的精彩世界, 其中有很多作者具体的使用经验和解决方案, 即使你会使用jQuery也能在阅读中发现些 ...