https://www.cnblogs.com/chentianwei/p/10156459.html

之前博客:建立了一个app。使用loacal storage 来管理tokens(通过client-side anuthentication 生成)。

问题是这种方法有些麻烦。

每次要检验用户是否通过验证,都需要先储存auth token和user信息进localStorage,然后在从中取出来进行各种各样的条件判断,boring。

            this.$http.post(url, {
//...略
})
.then(response => {
localStorage.setItem('user', JSON.stringify(response.data.user))
localStorage.setItem('jwt', response.data.token) if (localStorage.getItem('jwt') != null) {
//...略
}
})//

可以改用Vuex。

使用store来管理authentication state, 然后只需使用少许代码即可检查我们的application.


main.js引进axios

import Axios from 'axios'

Vue.prototype.$http = Axios
const token = localStorage.getItem('token')
if (token) {
Vue.prototype.$http.defaults.headers.common['Authorization'] = token
}

We also set the Authorization on axios header to our token, so our requests can be processed if a token is required. This way, we do not have to set token anytime we want to make a request.

默认设置,所有请求都会配置到:

给axios的headers赋值token 。这样,无需在每一次想要发出请求时,都设置token。

见axios Defaults

store.js解释

state存储

  • authentication status, 用于当使用mutation时,status数据储存验证过程的状态:pending,success, error。

  • jwt token

  • user信息

actions中的函数

login函数:

    login({commit}, user) {
return new Promise((resolve, reject) => {
commit('auth_request')
// 这是其中一种类似config的写法,也可以使用axios.post('', {...})
axios({url: 'http://localhost:3000/login', data:user, method: 'POST'})
.then(resp => {
const token = resp.data.token
const user = resp.data.user
localStorage.setItem('token', token)
      //main.js中设置了Vue.prototype.$http = Axios
axios.defualts.headers.common['Authorization'] = token
commit('auth_success', token, user)
// 返回一个状态由resp决定的Promise对象。
resolve(resp)
})
.catch(err => {
commit('auth_error')
localStorage.removeItem('token')
// 返回一个状态为失败的Promise对象
reject(err)
})
})
},

这里使用new Promise构造器返回一个promise对象。

login action用于验证一个user,并commit用户的credentials到vuex store。

  1. 使用axios调用服务器的login路由,并返回需要的data。
  2. 然后储存token到localStorage,
  3. 然后传递token和user的信息到auth_success mutation来更新store.state中的属性。
  4. 同时,也设置axios的headers。
     axios.defaults.headers.common['Authorization'] = token
⚠️

token不要储存在vuex store中,一旦离开程序,所有的Vuex.store中的data都会消失。

因此,为了确保用户返回到程序后,只要token处于验证期内, 就无需让user再登陆一次。

需要把token储存再localStorage, 即本地设备中。

最后返回promise对象, 以便在login完成后,能够返回一个response给user。



知识扩展:

Promise对象及构造函数

Promise对象用于表示一个异步操作的最终状态(完成或失败),以及返回的值。

new Promise产生的构造函数用于包装还未支持promises的函数。

const myFirstPromise = new Promise((resolve, reject) => {
// ?做一些异步操作,最终会调用下面两者之一:
//
// resolve(someValue); // fulfilled
// ?或
// reject("failure reason"); // rejected
});

例子:

var promisel = new Promise(function(resolve, reject) {
//当异步代码执行成功后,调用resolve(),当异步代码执行失败后,调用reject()
//本例子使用setTimout()来模拟异步代码
//真实环境,通常使用HTMl5 API,或者如XHR。
setTimeout(function() {
resolve('Success!') //Yes! Everything went well!
}, 300);
}) promisel.then(function(value) {
console.log(value)
//控制台输出的是"Success"
})

描述:

Promise 对象是一个代理对象(代理一个值), 被代理的值在Promise对象创建时可能是未知的。

Promise is a proxy for a value not necessarily known when the promise is created。

它允许你为异步操作的成功和失败分别绑定相应的处理方法(handlers)。

It allows you to associate handlers with an asynchronous action's success value or failure reason.

这让异步方法可以像同步方法那样返回值,但并不是立即返回最终执行结果,而是一个能代表未来出现的结果的promise对象。

一个Promise有3种状态:

  • pending, 之后会导致成功或失败:

    • fulfilled
    • rejected
Promise.reject(reason)
返回一个状态为失败的Promise对象,并将给定的失败信息传递给对应的处理方法
Promise.resolve(value)
返回一个状态由给定value决定的Promise对象。


创建register action

和login action代码基本一样。区别是aixos.post的url路由不一样。

它们都会commit('auth_request'), commit('auth_success')因为这两个action的目标都是把user信息存入state。

创建logout action

当登出后,删除储存在本地设备的token。

同时使用delete方法删除Vue实例对象的属性。 这里用delete axios的headers。

    logout({commit}) {
return new Promise((resolve, reject) => {
commit('logout')
localStorage.removeItem('token')
// 使用Vue.delete(target), target可以是对象或数组。
delete axios.defaults.headers.common['Authorization']
resolve()
})
}

Hide Pages Behind Auth

本文的目的时implement anthentication,未通过验证的用户不能访问某些页面。

为了实现这个目的,需要弄明白:

1. 用户想访问的页面,应当有一个方法可以检查是否用户是通过验证的。

2.一个页面是专为验证过的用户访问的,还是所有用户都可以访问。

使用vue-router可以解决上面的2个问题

vue-router的导航守卫和路由meta信息可以对从当前url到另一个url的过程进行全程的控制。

  1. meta对路由记录进行条件设置
  2. navigation Guard对整个导航过程进行控制,根据meta进行判断是否跳转或者取消导航。

首先router.js:

import Vue from 'vue'
import Router from 'vue-router'
import store from './store.js'
import Home from './views/Home.vue'
import About from './views/About.vue'
import Login from './components/Login.vue'
import Secure from './components/Secure.vue'
import Register from './components/Register.vue' Vue.use(Router)

引进vue, vue-router, 和vuex store setup.

所有的组件也被引进,用于定义路由。

然后,使用meta属性。

[...]
let router = new Router({
mode: 'history',
routes: [
//...略过home, login, register
{
path: '/secure',
name: 'secure',
component: Secure,
meta: {
requiresAuth: true
}
},
//...略过about
]
}) export default router/

使用router.beforeEach注册一个全局前置守卫:

router.beforeEach((to,from,next) => {
// 要前往的路由是否有meta条件:
// 是,进一步判断
// 否,直接跳转到这个路由。
if (to.matched.some(record => record.meta.requiresAuth)) {
// 判断用户是否有权限进入这个页面:(使用了vuex store)
if (store.getters.isLoggedIn) {
next()
} else {
next('/login')
}
} else {
next()
}
})

给路由记录增加各类条件(使用meta),然后用Navigation Guards来进行判断后,取消或者跳转。

这里还使用了VuexStore.getters中的方法,去掉了重复的条件判断。

通过Vuex store,我们能够定义actions来检查路由记录中的条件,定义getters返回这些条件的结果。

处理到期的Token

因为储存token在本地localStorage,token会永久保存。

如果它已经到期,那么我们的请求会因为invalid token而失败

打开./src/App.vue,增加一个hook:

export default {
[...]
created: function () {
this.$http.interceptors.response.use(undefined, function (err) {
return new Promise(function (resolve, reject) {
if (err.status === 401 && err.config && !err.config.__isRetryRequest) {
this.$store.dispatch(logout)
}
throw err;
});
});
}
}

拦截收到的response,看是否得到的是401 Unauthorized response。

如果是401,则dispatch logout action,让本地token被删除,然后跳转到login页面,重新登陆!

如此改善了用户体验!

使用Vuex来处理Authentication token的更多相关文章

  1. 解决修改密码报错‘passwd:Authentication token’

     1.修改密码时报错: 错误信息:'passwd: Authentication token manipulation error' [root@localhost test]# ' | passwd ...

  2. vmware 解决 authentication token manipulation error

    vmvare虚拟机长时间未使用,导致再次登录的时候密码忘了,无法登录. 启动时长按shift,进入root(recovery)模式, (recovery mode),进入"Recovery ...

  3. 无法修改linux/ubuntu密码(Authentication token manipulation error )问题解决过程【转】

    转自:https://blog.csdn.net/caizi001/article/details/38659189 Vmware虚拟机里的ubunut系统长期不用,密码忘记了,无奈只能通过slax ...

  4. Linux学习笔记之passwd:Authentication token manipulation error_错误的解决办法

    如果在linux中,不管是root用户还是普通用户登录后,修改自己的密码,出现—passwd:Authentication token manipulation error—错误的解决办法: root ...

  5. linux环境,crontab报错Authentication token is no longer valid; new one required You (aimonitor) are not allowed to access to (crontab) because of pam configuration.

    问题描述: 今天同事反应,一个系统上的某些数据没有生成,看了下,怀疑定时任务没有执行,就看下了crontab,发现报了下面的错误: [aimonitor@4A-LF-w08 ~]$ crontab - ...

  6. 解决"authentication token manipulation error"

      昨天安装是Ubuntu Server. 配置好了几个软件后就忘记继续了...今天打开,居然忘记了密码...真是的..  后来还是要改了. 不想重新弄什么的了..百度了下怎么改密码...然后就有一篇 ...

  7. linux修改密码出现Authentication token manipulation error的解决办法

    转自 :http://blog.163.com/junwu_lb/blog/static/1916798920120103647199/ Authentication token manipulati ...

  8. Authentication token is no longer valid

    Linux: Authentication token is no longer valid Problem: Authentication token is no longer valid; new ...

  9. authentication token manipulation error

    用户服务器中修改密码,输入passwd命令后,报错authentication token manipulation error   发生该错误原因是: 1.分区没有空间导致. 2./etc/pass ...

随机推荐

  1. default activity not found的问题

    莫名其妙的同一个project下的所有modlue全都出现了这个问题,在网上查了一些解决方法,总结一下就是在运行时把default activity改成nothing,这个把活动都搞没了肯定不行.还有 ...

  2. topcoder srm 697 div1 -3

    1.给定长度为$n$ 的数组$b$,构造长度为$n$ 的且没有重复元素的数组$a$,令$p_{i}$表示$a$中除$a_{i}$外其他元素的乘积.构造出的$a$满足$a_{i}^{b_{i}}$能够被 ...

  3. python&django 常见问题及解决方法

    0.python-dev安装(ubuntu) apt-get install  python-dev 1.Open(filename,mode) 报错实例: f = open('d:\Users\16 ...

  4. Restful framework【第五篇】解析器

    基本使用 -解析器 -源码从request.data -全局配置 -'DEFAULT_PARSER_CLASSES':['rest_framework.parsers.JSONParser'], -局 ...

  5. Python 模块的加载顺序

    基本概念 module 模块, 一个 py 文件或以其他文件形式存在的可被导入的就是一个模块 package 包,包含有 init 文件的文件夹 relative path 相对路径,相对于某个目录的 ...

  6. bzoj1565: [NOI2009]植物大战僵尸 最大权闭合子图,tarjan

    bzoj1565: [NOI2009]植物大战僵尸 链接 https://www.lydsy.com/JudgeOnline/problem.php?id=1565 思路 很容易的想到最大权闭合子图 ...

  7. powershell脚本的格式化

    Auto Formatting PowerShell in Visual Studio Code 1.安装visual studio code 2.安装powershell extension 3.打 ...

  8. Many Website Of WallPaper

    我在这里给大家推荐几个不错的壁纸网站 毕竟一张赏心悦目的壁纸能让你的工作效率提高不少 注意前方高能 一大波网站即将来袭 一系列 如你所见 alphacoders wallpaperdj Wallhav ...

  9. lamp服务器被人恶意绑定域名的解决办法

    还没开始就被别人绑定了域名 事情的起因与发现 刚买了个服务器搭建了一个dz,想着域名还没备案,就先搭建了起来,然后在做DDOS测试时偶然发现服务器被别人恶意把域名绑定了 最初的解决方案 没管..... ...

  10. AtCoder Beginner Contest 117 解题报告

    果然abc都是手速场. 倒序开的qwq. D题因为忘记1e12二进制几位上界爆了一发. A - Entrance Examination 就是除一下就行了... 看样例猜题意系列. #include& ...