{"id":1056,"date":"2023-03-12T10:12:13","date_gmt":"2023-03-12T02:12:13","guid":{"rendered":"https:\/\/www.appblog.cn\/?p=1056"},"modified":"2023-04-29T11:48:02","modified_gmt":"2023-04-29T03:48:02","slug":"vue-uses-axios-to-achieve-login-verification-interception-and-page-redirection","status":"publish","type":"post","link":"https:\/\/www.appblog.cn\/index.php\/2023\/03\/12\/vue-uses-axios-to-achieve-login-verification-interception-and-page-redirection\/","title":{"rendered":"Vue\u4f7f\u7528axios\u5b9e\u73b0\u767b\u5f55\u9a8c\u8bc1\u62e6\u622a\u53ca\u9875\u9762\u8df3\u8f6c"},"content":{"rendered":"<h3>\u5b9a\u4e49\u8def\u7531\u65f6\u6dfb\u52a0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5b57\u6bb5requireAuth<\/h3>\n<pre><code class=\"language-javascript\">path: &#039;\/repository&#039;,\nname: &#039;repository&#039;,\nmeta: {\n  requireAuth: true,  \/\/ \u6dfb\u52a0\u8be5\u5b57\u6bb5\uff0c\u8868\u793a\u8fdb\u5165\u8fd9\u4e2a\u8def\u7531\u662f\u9700\u8981\u767b\u5f55\u7684\n},\ncomponent: Repository<\/code><\/pre>\n<p><!-- more --><\/p>\n<h3>\u8def\u7531\u8df3\u8f6c\u524d\u6743\u9650\u9a8c\u8bc1<\/h3>\n<pre><code class=\"language-javascript\">router.beforeEach((to, from, next) =&gt; {\n  if (to.meta.requireAuth) {  \/\/ \u5224\u65ad\u8be5\u8def\u7531\u662f\u5426\u9700\u8981\u767b\u5f55\u6743\u9650\n    if (store.state.token) {  \/\/ \u901a\u8fc7vuex state\u83b7\u53d6\u5f53\u524d\u7684token\u662f\u5426\u5b58\u5728\n      next();\n    } else {\n      next({\n        path: &#039;\/login&#039;,\n        query: {redirect: to.fullPath}  \/\/ \u5c06\u8df3\u8f6c\u7684\u8def\u7531path\u4f5c\u4e3a\u53c2\u6570\uff0c\u767b\u5f55\u6210\u529f\u540e\u8df3\u8f6c\u5230\u8be5\u8def\u7531\n      })\n    }\n  } else {\n    next();\n  }\n}<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ea\u662f\u7b80\u5355\u7684\u524d\u7aef\u8def\u7531\u63a7\u5236\u767b\u5f55\u62e6\u622a\uff0c\u5e76\u4e0d\u80fd\u771f\u6b63\u963b\u6b62\u7528\u6237\u8bbf\u95ee\u9700\u8981\u767b\u5f55\u6743\u9650\u7684\u8def\u7531\u3002<\/p>\n<p>\u8fd8\u6709\u4e00\u79cd\u60c5\u51b5\u4fbf\u662f\uff1a\u5f53\u524dtoken\u5931\u6548\uff0c\u4f46\u662ftoken\u4f9d\u7136\u4fdd\u5b58\u5728\u672c\u5730\u3002\u6b64\u65f6\u8bbf\u95ee\u9700\u8981\u767b\u5f55\u6743\u9650\u7684\u8def\u7531\u65f6\uff0c\u5b9e\u9645\u4e0a\u5e94\u8be5\u8ba9\u7528\u6237\u91cd\u65b0\u767b\u5f55\u3002<\/p>\n<p>\u6700\u7ec8\u9700\u8981\u7ed3\u5408 HTTP\u62e6\u622a\u5668 + \u540e\u7aef\u63a5\u53e3\u8fd4\u56de\u7684HTTP\u72b6\u6001\u7801 \u5224\u65ad\u3002<\/p>\n<h3>\u8bf7\u6c42\u4e0e\u54cd\u5e94\u62e6\u622a\u5668<\/h3>\n<p>\u7edf\u4e00\u5904\u7406\u6240\u6709HTTP\u8bf7\u6c42\u548c\u54cd\u5e94\uff0c\u4f7f\u7528axios\u7684\u62e6\u622a\u5668\u3002\u6bcf\u6b21\u8def\u7531\u8df3\u8f6c\uff0c\u90fd\u5148\u8ba9\u540e\u53f0\u9a8c\u8bc1\u4e00\u4e0btoken\u662f\u5426\u6709\u6548\uff0c\u5728http\u5934\u6dfb\u52a0token\uff0c\u5f53\u540e\u7aef\u63a5\u53e3\u8fd4\u56de 403 Unauthorized(\u672a\u6388\u6743) \uff0c\u8ba9\u7528\u6237\u91cd\u65b0\u767b\u5f55\u3002<\/p>\n<pre><code class=\"language-javascript\">\/\/ http request \u62e6\u622a\u5668\naxios.interceptors.request.use(\n  config =&gt; {\n    if (store.state.token) {  \/\/ \u5224\u65ad\u662f\u5426\u5b58\u5728token\uff0c\u5982\u679c\u5b58\u5728\uff0c\u5219\u6bcf\u4e2ahttp header\u90fd\u52a0\u4e0atoken\n      config.headers.Authorization = `token ${store.state.token}`;\n    }\n    return config;\n  },\n  err =&gt; {\n    return Promise.reject(err);\n  });<\/code><\/pre>\n<pre><code class=\"language-javascript\">\/\/ http response \u62e6\u622a\u5668\naxios.interceptors.response.use(\n  response =&gt; {\n    return response;\n  },\n  error =&gt; {\n    if (error.response) {\n      switch (error.response.status) {\n        case 403:\n          \/\/ \u8fd4\u56de 403 \u6e05\u9664token\u4fe1\u606f\u5e76\u8df3\u8f6c\u5230\u767b\u5f55\u9875\u9762\n          store.commit(types.LOGOUT);\n          router.replace({\n            path: &#039;login&#039;,\n            query: {redirect: router.currentRoute.fullPath}\n          })\n          break\n        defualt:\n          break\n      }\n    }\n    return Promise.reject(error.response.data)  \/\/ \u8fd4\u56de\u63a5\u53e3\u8fd4\u56de\u7684\u9519\u8bef\u4fe1\u606f\n  });<\/code><\/pre>\n<p>\u767b\u51fa\u529f\u80fd\u4e5f\u5c31\u5f88\u7b80\u5355\uff0c\u53ea\u9700\u8981\u628a\u5f53\u524dtoken\u6e05\u9664\uff0c\u518d\u8df3\u8f6c\u5230\u9996\u9875\u5373\u53ef\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9a\u4e49\u8def\u7531\u65f6\u6dfb\u52a0\u4e00\u4e2a\u81ea\u5b9a\u4e49\u5b57\u6bb5requireAuth path: &#039;\/repository&#039; [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[35],"tags":[290],"class_list":["post-1056","post","type-post","status-publish","format-standard","hentry","category-vue","tag-axios"],"_links":{"self":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1056","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/comments?post=1056"}],"version-history":[{"count":0,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/posts\/1056\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/media?parent=1056"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/categories?post=1056"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.appblog.cn\/index.php\/wp-json\/wp\/v2\/tags?post=1056"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}