struts2 拦截器验证是否登录
2014-03-11 11:26
344 查看
前几天做了一个项目,需要在调用action之前进行验证是否未登录用户。写了一个拦截器记录一下:
登录后把用户的信息存入session当中
写拦截器LoginInteceptor 继承 AbstractInterceptor 代码如下:
struts.xml文件中修改配置文件中的配置信息,声明一个拦截器,然后再需要拦截的action中标明我们在执行之前要过一下这个拦截器,看看是否用户登录如果登录了才能执行下一步,否则跳转到login
写一个记录一下,大神们可以拍砖。
登录后把用户的信息存入session当中
if (checkUser.getPassword().equals(tqUser.getPassword())) { HttpSession session = ServletActionContext.getRequest().getSession(true); session.setAttribute("UserInfo", checkUser); return SUCCESS; }
写拦截器LoginInteceptor 继承 AbstractInterceptor 代码如下:
package com.taoqu.inteceptor; import java.util.Map; import com.opensymphony.xwork2.Action; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.interceptor.AbstractInterceptor; public class LoginInteceptor extends AbstractInterceptor{ @Override public String intercept(ActionInvocation invocation) throws Exception { // TODO Auto-generated method stub ActionContext ctx = invocation.getInvocationContext(); Map session = ctx.getSession(); Object user = session.get("UserInfo"); if(user != null){ return invocation.invoke(); } ctx.put("tip", "您还没有登录,请登录或注册先亲。。"); return "login"; } }
struts.xml文件中修改配置文件中的配置信息,声明一个拦截器,然后再需要拦截的action中标明我们在执行之前要过一下这个拦截器,看看是否用户登录如果登录了才能执行下一步,否则跳转到login
<package name="intrest" extends="struts-default"> <interceptors> <interceptor name="authority" class="com.taoqu.inteceptor.LoginInteceptor" /> </interceptors> <!--设置全局的跳转 --> <global-results> <result name="login">/login.jsp</result> </global-results> <!-- 跳转到发布兴趣 --> <action name="hrefPublishIntrest" class="IntrestAction"> <interceptor-ref name="authority"/> <result>/build.jsp</result> </action> <!-- 发布兴趣 --> <action name="publishIntrest" class="IntrestAction" method="publishIntrest"> <!-- <interceptor-ref name="defaultStack"/> --> <interceptor-ref name="authority"/> <result name="input">/error.jsp</result> <result name="success" type="redirect">/findcreatins.action</result> </action> ...............
写一个记录一下,大神们可以拍砖。
相关文章推荐
- Struts2自定义拦截器案例:验证用户是否登录拦截器
- 用Struts2的拦截器验证用户是否登录
- Struts2+Spring+Hibernate step by step 11 ssh整合之拦截器验证用户是否登录
- spring 拦截器过滤登录url 不验证是否登录其他的都拦截验证是否登录才能访问url
- struts2拦截器配置;拦截器栈;配置默认拦截器;拦截方法的拦截器MethodFilterInterceptor;完成登录验证
- Struts2重新学习之自定义拦截器(判断用户是否是登录状态)
- struts2定义拦截器(Interceptor)检查用户是否已经登录
- Struts2自定义登录验证拦截器
- Struts2拦截器登录验证实例
- struts2获取当前方法的Action的Url 登录验证拦截器实现
- struts2学习笔记--拦截器(Interceptor)和登录权限验证Demo
- Struts2 拦截器的使用,验证是否已经登陆
- Struts2拦截器登录验证
- Struts2的拦截器特性实现登录验证
- SSH学习——Struts2拦截器实现登录权限验证
- Struts2拦截器登录验证
- struts2拦截器-简单实现非法登录验证
- 拦截器验证用户是否登录
- Struts2拦截器登录验证
- 拦截器实现对用户是否登录及登陆超时的验证