Browse Source

拦截token过期 进行重新登录

yuhongqi 2 days ago
parent
commit
04d46f4d48
2 changed files with 46 additions and 13 deletions
  1. 4 3
      src/permission.js
  2. 42 10
      src/utils/request.js

+ 4 - 3
src/permission.js

@@ -27,9 +27,10 @@ router.beforeEach((to, from, next) => {
             next({ ...to, replace: true }) // hack方法 确保addRoutes已完成
           })
         }).catch(err => {
-          store.dispatch('LogOut').then(() => {
-            Message.error(err)
-            next({ path: '/' })
+          // token 无效时 logout 接口也可能失败,使用前端登出并跳登录页
+          store.dispatch('FedLogOut').then(() => {
+            Message.error(err || '登录状态已失效,请重新登录')
+            next(`/login?redirect=${to.fullPath}`)
           })
         })
       } else {

+ 42 - 10
src/utils/request.js

@@ -62,16 +62,8 @@ service.interceptors.response.use(res => {
     // 获取错误信息 - 优先使用后端返回的 message 或 msg,兼容两种字段名
     const msg = res.data.message || res.data.msg || errorCode[code] || errorCode['default']
     if (code === 401) {
-      MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
-          confirmButtonText: '重新登录',
-          cancelButtonText: '取消',
-          type: 'warning'
-        }
-      ).then(() => {
-        store.dispatch('LogOut').then(() => {
-          location.href = '/index';
-        })
-      })
+      handleAuthExpired()
+      return Promise.reject('无效的会话,或者会话已过期,请重新登录。')
     } else if (code === 500) {
       Notification.error({
         title: msg
@@ -88,6 +80,13 @@ service.interceptors.response.use(res => {
   },
   error => {
     console.log('err' + error)
+    const status = error.response && error.response.status
+    const respMsg = (error.response && error.response.data && (error.response.data.message || error.response.data.msg)) || ''
+    // HTTP 401,或 JWT 签名/鉴权失败被后端打成 500 时,清理登录态并跳转登录
+    if (status === 401 || isAuthInvalidMessage(respMsg) || isAuthInvalidMessage(error.message)) {
+      handleAuthExpired()
+      return Promise.reject(error)
+    }
     let { message } = error;
     if (message == "Network Error") {
       message = "后端接口连接异常";
@@ -107,4 +106,37 @@ service.interceptors.response.use(res => {
   }
 )
 
+let isReloginShowing = false
+
+function isAuthInvalidMessage(msg) {
+  if (!msg) {
+    return false
+  }
+  const text = String(msg)
+  return text.indexOf('JWT signature') !== -1
+    || text.indexOf('JWT validity') !== -1
+    || text.indexOf('认证失败') !== -1
+    || text.indexOf('Token') !== -1 && text.indexOf('过期') !== -1
+    || text.indexOf('登录状态已过期') !== -1
+}
+
+function handleAuthExpired() {
+  if (isReloginShowing) {
+    return
+  }
+  isReloginShowing = true
+  MessageBox.confirm('登录状态已过期,您可以继续留在该页面,或者重新登录', '系统提示', {
+    confirmButtonText: '重新登录',
+    cancelButtonText: '取消',
+    type: 'warning'
+  }).then(() => {
+    isReloginShowing = false
+    store.dispatch('FedLogOut').then(() => {
+      location.href = '/login'
+    })
+  }).catch(() => {
+    isReloginShowing = false
+  })
+}
+
 export default service