使用struts2自定义拦截实现统一的身份验证模式
来源:互联网 发布:59网络语言是什么梗 编辑:程序博客网 时间:2024/05/24 04:49
最近在项目开发中需要进行身份的统一认证,之前.NET的思想就是通过父页面(基类)进行控制,现在使用struts2进行开发的话,当然还是使用拦截器比较方便,而且比较省事,网上提供了很多实现的例子,但是都觉得对于新手而言都有一点不清楚如何使用起来。现在我把我的整个实现方式分享出来。
首先我的项目使用的是spring+struts2+mybatis,数据库端使用的pgsql ,现在的需求是我们需要在用户登录后,将用户信息存储在session中,在后续的操作中,需要在每次请求中都判断session是否失效,失效后就提示过期重新登录。
本次就使用拦截器来实现,首先我们为验证建立一个包,取名com.tc.auth,在包下建立action包和资源文件 source.struts,文件结构大概如下图所示
接着我们来编写AuthInterceptor.java文件的code 吧:
package com.tc.auth.action;import com.opensymphony.xwork2.ActionInvocation;import com.opensymphony.xwork2.interceptor.Interceptor;import com.tc.blacktea.common.TCSysConst;import com.tc.blacktea.util.SessionUtil;public class AuthInterceptor implements Interceptor {private static final long serialVersionUID = 5582842221490358379L;public void destroy() {}/** * 在服务启动的时候执行 */public void init() {}public String intercept(ActionInvocation actionInvocation) throws Exception {System.out.println("<<<用户认证检测拦截器初始化...");// sessionUser 如果session中不存在用户在返回index视图if (SessionUtil.GetSession("USER_SESSION_KEY") == null) {//这里我使用了自己写的session封装类return "autherror";}String result = actionInvocation.invoke();//这里是继续原先请求的invoke,不进行其他操作。return result;}}
接着我们来看下拦截器的配置是如何编写的:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><!-- default (全局)包配置 --><package name="userAuth" extends="struts-default" namespace="/"><interceptors><interceptor name="AuthInterceptor"class="com.tc.auth.action.AuthInterceptor"></interceptor><!-- 默认的必须放在最下面! --><interceptor-stack name="userAuthStack"><interceptor-ref name="AuthInterceptor"></interceptor-ref><interceptor-ref name="defaultStack"></interceptor-ref></interceptor-stack></interceptors><!-- 默认拦截器,此包下所有的ACTION将都被拦截。如果ACTION再定义了拦截器,则失效 --><default-interceptor-ref name="userAuthStack"></default-interceptor-ref><global-results><!-- 首页 --><result name="index">/jsp/index.jsp</result><result name="adminLoginPage">/jsp/index.jsp</result><result name="autherror">/jsp/Error.jsp</result></global-results><!-- 用户退出登录 --><!--<action name="existLogin_*" class="existLoginAction" method="{1}"> <result name="index">/jsp/Error.jsp</result> </action> --></package></struts>上面的code需要注意几点:
1、package包的namespace如果是”/“的话,就表示是全局的一个拦截器,你在struts的配置文件中任意位置都可以调用。
2、interceptors/interceptor的class属性是刚刚上面写的action的类访问路径。
3、interceptors/interceptor-stack的默认拦截器defaultStack必须写在最后面,否则失效。
4、default-interceptor-ref 是定义所有的action都默认先执行为name属性的拦截器名称,完了再执行请求上下文的拦截器。
这几个的联系看下图:
现在拦截器都定义好了后,我们接下来就是使用,
首先我们在user的包下的struts-user.xml文件写了如下的xml配置信息:
<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"><struts><package name="struts-user" extends="userAuth" namespace="/user"><!-- 定义Action(login.action) --><action name="userManage" class="com.tc.user.action.UserAction"><interceptor-ref name="userAuthStack" /><!-- 定义转发路径对应的字符串名 --><result name="error">/jsp/Error.jsp</result><result name="success">/jsp/user/userlist.jsp</result><result name="userinfo">/jsp/user/userinfo.jsp</result></action></package></struts>1、注意package报的extends属性必须继承我们自定义的拦截器的package的名称
2、在我们的action中添加interceptor-ref节点,name属性指定我们的拦截器名称即可。
这样配置完了后,我们就可以在任何需要进行身份验证的action中进行如上的配置就可以实现信息的验证和过滤操作。只要会用了这些类似的原理后,我们就可以进行如身份验证、数据过滤、访问统计、页面PR等都是可以通过这种类似的手法达到效果,好了,不多说了,赶快动手试试吧!
转载请注明: http://blog.csdn.net/qq415734794/article/details/7710585
- 使用struts2自定义拦截实现统一的身份验证模式
- Struts2自定义拦截器的实现
- struts2自定义拦截器的使用
- struts2--使用自定义拦截器
- 使用Struts2的拦截器和自定义注解方式实现权限控制
- struts2的拦截器自定义,使用默认拦截器,组合拦截器堆 的案例
- struts2拦截器使用(用户session失效后的统一页面指向)
- struts2自定义拦截器实现的分析说明附带代码
- Struts2的自定义拦截器
- Struts2的自定义拦截器
- struts2的自定义拦截器
- Struts2的自定义拦截器
- Struts2的自定义拦截器
- struts2 自定义拦截器实现身份认证
- Struts2自定义拦截器,实现登录检查。
- struts2中拦截器的全局拦截和自定义拦截
- Struts2的拦截器、自定义拦截器、Struts2 验证机制
- Struts2 自定义拦截器配置和使用
- 关于static,const,static const , const static
- linux动态加载动态链接库
- 随笔二十八:invalid instruction suffix for push'
- android 监听HOME键
- Android 4.0 ICS SystemUI浅析——StatusBar加载流程分析
- 使用struts2自定义拦截实现统一的身份验证模式
- ORA-01031: insufficient privileges 解决方案
- GridView每几行增加一空行的方法
- 在VS2010中使用wxWidgets 2.9.3
- 在VS2010内使用MFC建立动画
- 处理图片的工具类
- qt 段错误,简单调试方法
- oracle数据泵Data Pump
- U盘安装 Centos6.2