After vue was updated to 2.0, the author announced that he would no longer update vue-resource, but recommended the use of axios. Some time ago, I used vue for the first time in a project. Regarding the login problem, I will write down my experience here.
First backend:
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import com.lovnx.gateway.po.User; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginInterceptor extends HandlerInterceptorAdapter{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { User user = (User)request.getSession().getAttribute("user"); if(user == null){ response.sendError(401); return false; }else return true; } }
The processing done here is: if there is no user in the session, a 401 error will be returned to the front end.
Front end:
import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-default/index.css' import App from './App' import router from './router' import axios from 'axios' // http response 拦截器 axios.interceptors.response.use( response => { return response; }, error => { if (error.response) { if (error.response.status == 401) { store.commit(types.LOGOUT); router.replace({ path: 'login', query: {redirect: router.currentRoute.fullPath} }) } } return Promise.reject(error.response.data) // 返回接口返回的错误信息 } );
Note that the interceptor here is written in main.js.
//------------Dividing line-------------//
Of course, the above method is to write the login page in the Vue project. Here is an example of using an independent login page combined with the Vue project.
Project Directory:
The static and index.html are things packaged by webpack.
The idea here should be combined with a previously written blog post: Rewrite ajax to implement session timeout and jump to the login page
The general idea is: request any page, the back-end interceptor intercepts the request, checks whether the user information in the session exists, if it does not exist, jump to this login.html; if it exists, respond to the data normally. Here we need to slightly change the front-end interceptor:
import Vue from 'vue' import ElementUI from 'element-ui' import 'element-ui/lib/theme-default/index.css' import App from './App' import router from './router' import axios from 'axios' // http response 拦截器 axios.interceptors.response.use( response => { //这个判断是关键,如果返回登陆页面内容了,就刷新当前页面,经后端处理就会跳转到登陆页面了 var temp = response.data + ""; if (temp.indexOf('lkdjoifdgjdfgjdfgjfh14546') > -1) { router.go(0); } return response; }, error => { if (error.response) { //退出登陆的时候就响应401即可 if (error.response.status == 401) { router.go(0); } } return Promise.reject(error.response.data) // 返回接口返回的错误信息 } );
Among them, lkdjoifdgjdfgjdfgjfh14546 is written in a hidden field on the login page.
Back-end interceptor:
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter; import com.lovnx.gateway.po.User; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginInterceptor extends HandlerInterceptorAdapter{ @Override public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { String requestUri = request.getRequestURI(); String contextPath = request.getContextPath(); if (requestUri.indexOf("/login.html") > -1 || requestUri.indexOf("/system/login") > -1) { return true; } User user = (User)request.getSession().getAttribute("user"); if(user == null){ // 未登录跳转到login页面!"); response.sendRedirect(contextPath + "/login.html"); return false; }else return true; } }
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other php Chinese websites related articles!
The above is the detailed content of Vue2.0+axios implements login interceptor (with code). For more information, please follow other related articles on the PHP Chinese website!