您的位置:首页 > 其它

物流管理系统(七)4实现用户未登录自动跳转到登录页面

2018-03-27 13:00 891 查看
需要继承struts2框架的MethodFilterInterceptor

只需在包里建一个自定义的拦截器类,这个拦截器类需要继承struts2框架的MethodFilterInterceptor

但是他需要获得session中的user对象,其他方法有可能也有所以为了提高系统的复用性,建立一个BOSUtils来获取session中的对象。

BOSUtils

public class BOSUtils {
//获取session对象
public static HttpSession getSession(){
return ServletActionContext.getRequest().getSession();
}
//获取登录用户对象
public static User getLoginUser(){
return (User) getSession().getAttribute("loginUser");
}
}


自定义拦截器类

/**
* 自定义的拦截器,实现用户未登录自动跳转到登录页面
* @author zhaoqx
*
*/
public class BOSLoginInterceptor extends MethodFilterInterceptor{
//拦截方法
protected String doIntercept(ActionInvocation invocation) throws Exception {
//从session中获取用户对象
User user = BOSUtils.getLoginUser();
if(user == null){
//没有登录,跳转到登录页面
return "login";
}
//放行
return invocation.invoke();
}
}


配置拦截器

<interceptors>
<!-- 注册自定义拦截器 -->
<interceptor name="bosLoginInterceptor" class="comlxs.bos.web.interceptor.BOSLoginInterceptor">
<!-- 指定哪些方法不需要拦截 -->
<param name="exclude
97f0
Methods">login</param>
</interceptor>
<!-- 定义拦截器栈 -->
<interceptor-stack name="myStack">
<interceptor-ref name="bosLoginInterceptor"></interceptor-ref>
<interceptor-ref name="defaultStack"></interceptor-ref>
</interceptor-stack>
</interceptors>
<default-interceptor-ref name="myStack"/>

<!-- 全局结果集定义 -->
<global-results>
<result name="login">/login.jsp</result>
</global-results>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐