做爰高潮a片〈毛片〉,尤物av天堂一区二区在线观看,一本久久A久久精品VR综合,添女人荫蒂全部过程av

最新文章專題視頻專題問答1問答10問答100問答1000問答2000關(guān)鍵字專題1關(guān)鍵字專題50關(guān)鍵字專題500關(guān)鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關(guān)鍵字專題關(guān)鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
當(dāng)前位置: 首頁 - 科技 - 知識百科 - 正文

Vue2.0 axios前后端登陸攔截器(實例講解)

來源:懂視網(wǎng) 責(zé)編:小采 時間:2020-11-27 22:26:44
文檔

Vue2.0 axios前后端登陸攔截器(實例講解)

Vue2.0 axios前后端登陸攔截器(實例講解):vue更新到2.0之后,作者就宣告不再對vue-resource更新,而是推薦使用axios。前段時間第一次在項目里用到vue,關(guān)于登陸問題,這里寫一下心得。 首先后端: import org.springframework.web.servlet.handler.HandlerIntercepto
推薦度:
導(dǎo)讀Vue2.0 axios前后端登陸攔截器(實例講解):vue更新到2.0之后,作者就宣告不再對vue-resource更新,而是推薦使用axios。前段時間第一次在項目里用到vue,關(guān)于登陸問題,這里寫一下心得。 首先后端: import org.springframework.web.servlet.handler.HandlerIntercepto

vue更新到2.0之后,作者就宣告不再對vue-resource更新,而是推薦使用axios。前段時間第一次在項目里用到vue,關(guān)于登陸問題,這里寫一下心得。

首先后端:

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;
 }
}

這里做的處理就是:如果session里面沒有user了,就向前端返回401錯誤。

前端:

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) // 返回接口返回的錯誤信息
 }
);

注意,這里的攔截器寫在main.js中。

//———————————–分割線————————————–//

當(dāng)然,以上的這種方式是把登陸頁面寫在了Vue工程里面,下面介紹一種用獨立的登陸頁面結(jié)合Vue工程的例子。

工程目錄:

其中的static和index.html是webpack打包后的東西。

這里的思想要結(jié)合以前寫的一篇博文: 重寫ajax實現(xiàn)session超時跳轉(zhuǎn)到登陸頁面

大概思想就是:請求任何一個頁面,后端攔截器攔截到請求,查看session里的用戶信息存在與否,如果不存在就跳轉(zhuǎn)到這個login.html;如果存在就正常響應(yīng)數(shù)據(jù)。這里要對前端攔截器稍微改動一下:

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 => {
 //這個判斷是關(guān)鍵,如果返回登陸頁面內(nèi)容了,就刷新當(dāng)前頁面,經(jīng)后端處理就會跳轉(zhuǎn)到登陸頁面了
 var temp = response.data + "";
 if (temp.indexOf('lkdjoifdgjdfgjdfgjfh14546') > -1) {
 router.go(0);
 }
 return response;
 },
 error => {
 if (error.response) {
 //退出登陸的時候就響應(yīng)401即可
 if (error.response.status == 401) {
 router.go(0);
 }
 }
 return Promise.reject(error.response.data) // 返回接口返回的錯誤信息
 }
);

其中,lkdjoifdgjdfgjdfgjfh14546這個是寫在登陸頁面一個hidden域里面的。

后端攔截器:

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){
 // 未登錄跳轉(zhuǎn)到login頁面!");
 response.sendRedirect(contextPath + "/login.html");
 return false;
 }else
 return true;
 }

}

以上這篇Vue2.0 axios前后端登陸攔截器(實例講解)就是小編分享給大家的全部內(nèi)容了,希望能給大家一個參考,也希望大家多多支持腳本之家。

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識,若有侵權(quán)等問題請及時與本網(wǎng)聯(lián)系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文檔

Vue2.0 axios前后端登陸攔截器(實例講解)

Vue2.0 axios前后端登陸攔截器(實例講解):vue更新到2.0之后,作者就宣告不再對vue-resource更新,而是推薦使用axios。前段時間第一次在項目里用到vue,關(guān)于登陸問題,這里寫一下心得。 首先后端: import org.springframework.web.servlet.handler.HandlerIntercepto
推薦度:
標(biāo)簽: VUE 登陸 2.0
  • 熱門焦點

最新推薦

猜你喜歡

熱門推薦

專題
Top
主站蜘蛛池模板: 丹凤县| 桐乡市| 田林县| 宁夏| 嘉祥县| 纳雍县| 周至县| 比如县| 伊春市| 江源县| 托克逊县| 景宁| 赤壁市| 兖州市| 县级市| 佳木斯市| 伊吾县| 江都市| 罗田县| 福泉市| 阿拉善盟| 汉沽区| 洞头县| 诸城市| 永顺县| 广东省| 浠水县| 军事| 康平县| 玉林市| 方山县| 五寨县| 商河县| 冀州市| 安康市| 卢氏县| 乌拉特前旗| 札达县| 汝州市| 喀喇| 南阳市|