解决方案:SSM+vue+webpack+vue-resource+vue-route路由项目验证session过期跳转至登陆界面
2017.9.16修改一下,因为觉得之前写的有点不准确,仔细想了想需要session验证的只有两处就行了,不是之前说的三处。
登录:登陆页面单独出来,和vue项目分开,login.jsp页面登陆成功后重定向到vue项目的首页路由地址。
到Vue后台项目后有两处会用到session验证处理:通过webpacke+路由的方式构建的后台项目,排除蒙对了静态页面的地址哈,这个自己想办法也好解决。
1、路由之间跳转(vue项目页面之间跳转)
解决:此时的跳转请求没有进入后台,需要在vue-route的全局钩子中跳转到每个路由之前进行session验证请求
router.beforeEach((to, from, next) => { _vue.$http.post(_vue.getUrl()+"/login/checkSession.do" , {emulateJSON:true}) .then((response)=>{ if(response.body.code=="500"){//session过期 window.location.href= _vue.getUrl()+"/admin/"; }else{ next(); } }, (response)=> { this.$Message.error("路由校验session失败!", 3); }); });
2、每次js异步请求时校验session
解决:自己写个springmvc 的拦截器配置到springmvc的拦截器栈,拦截请求,判断session,过期直接响应状态码不再传递请求
<mvc:interceptors > <!--过滤ajax .do请求,检查session是否过期--> <mvc:interceptor> <mvc:mapping path="/**/*.do"/> <bean class="com.demon.interceptor.CheckSessionInterceptor"></bean> </mvc:interceptor> </mvc:interceptors >
main.js中使用vue-resource的拦截器(因为异步请求用的是vue-resource的方法),这个拦截器会在响应到达每个路由之前进行拦截,在此统一判断状态码进行跳转到登录页面
Vue.http.interceptors.push((request, next) => { next((response) => { if(response.body.code!=null&&response.body.code=="500"){//登录超时 window.location.href= "localhost:8080/SSM/admin/login.jsp"; }else{ return response; } }) })
结束。
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。
- 上一篇: Vue中保存用户登录状态
- 下一篇: 设置session有效期的几种方法