首页 > 其他 > 详细

token验证

时间:2018-12-27 12:42:37      阅读:150      评论:0      收藏:0      [点我收藏+]
Token验证

设置前端路由跳转

router.beforeEach((to, from, next) => {
  if (to.matched.some(route => route.meta.auth)) {
    // 判断token是否存在,如果存在则正常跳转
    if (localStorage.getItem(‘token‘)) {
      next()
    } else {
      next(‘/login‘)
    }
  } else {
    next()
  }
})

登录成功,将token存储

login () {
  // 发起请求登录
  axios.post(‘/user/login‘, this.user).then(res => {
    console.log(res.data)
    // 接收到token后将token存储到localstorage
    // 前缀必须要加
    localStorage.setItem(‘token‘, "Bearer " + res.data.res.token)
  })
}

设置请求拦截器

只要有token就让每次请求都携带token

// 设置请求拦截器
axios.interceptors.request.use(config => {
  // console.log(config)
  const token = localStorage.getItem(‘token‘)
  if (token) {
    // 将获取到的token设置给header中的Authorization
    config.headers.Authorization = token
  }
  return config
})
设置响应拦截器

对响应结果进行处理,token出现问题,返回一定是401

// 设置响应拦截器
axios.interceptors.response.use(res => {
  // 对结果进行处理
  if (res.data.res_code === 401) {
    // 跳转登录
    router.replace(‘/login‘)
    // 删除token
    localStorage.removeItem(‘token‘)
  }
  return res
}, err => {
  // 如果是正常接口,会走err,err.response.status值为401 进行跳转及删除token的操作
})

 

token验证

原文:https://www.cnblogs.com/bao2333/p/10184218.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!