欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Vue中axios攔截器如何單獨(dú)配置token

 更新時(shí)間:2019年12月27日 13:19:33   作者:就是不健身  
這篇文章主要介紹了Vue axios攔截器如何單獨(dú)配置token及vue axios攔截器的使用,需要的朋友可以參考下

在了解到cookie、session、token的作用后學(xué)習(xí)token的使用

cookie

cookie是隨著url將參數(shù)發(fā)送到后臺(tái),安全性最低,并且大小受限,不超過4kb左右,它的數(shù)據(jù)保存在客戶端

session

session數(shù)據(jù)保存在服務(wù)端,在內(nèi)存中開辟空間存儲(chǔ)數(shù)據(jù),session文件名即sessionID保存在cookie內(nèi),隨cookie傳送到服務(wù)端后在服務(wù)端匹配session文件

token

token是服務(wù)端的一種算法,如果登錄成功,服務(wù)端就會(huì)根據(jù)算法生成一個(gè)字符串,將字符串傳遞回客戶端。這個(gè)字符串就是token,安全性最高

以上都有可能受到CSRF攻擊

axios攔截器會(huì)在發(fā)送請(qǐng)求前先進(jìn)行處理,將token放進(jìn)key中保存在請(qǐng)求頭中,這個(gè)key是前后臺(tái)約定好的。這樣配置好后,每次發(fā)送請(qǐng)求的時(shí)候,請(qǐng)求頭都會(huì)帶上token傳送到后臺(tái)進(jìn)行校驗(yàn)。

axios的特點(diǎn)(官網(wǎng))

  • 支持瀏覽器和node.js
  • 支持promise
  • 能攔截請(qǐng)求和響應(yīng)
  • 能轉(zhuǎn)換請(qǐng)求和響應(yīng)數(shù)據(jù)
  • 能取消請(qǐng)求
  • 自動(dòng)轉(zhuǎn)換JSON數(shù)據(jù)
  • 瀏覽器端支持防止CSRF(跨站請(qǐng)求偽造)

方法一:我們?cè)谑褂胊xios請(qǐng)求的時(shí)候可以先獲取我們已經(jīng)存入localStorage里的token

然后在攔截器里使用[…]進(jìn)行拼接

import axios from 'axios';
import qs from 'qs';
axios.defaults.baseURL = process.env.VUE_APP_BASE_API;
let token = localStorage.getItem('token')
// Add a request interceptor
axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  //console.log(config)
  if(config.method==='post'){
    config.data=qs.stringify({
      token:token,
      ...config.data
    })
  }else if(config.method==='get'){
    config.params={
      token:token,
      ...config.params
    }
  }
  return config;
 }, function (error) {
  // Do something with request error
  return Promise.reject(error);
 });

// Add a response interceptor
axios.interceptors.response.use(function (response) {
  // Do something with response data
  return response;
 }, function (error) {
  // Do something with response error
  return Promise.reject(error);
 });

 class http{
   static get(url,params){
     return axios.get(url,params)
   }
   static post(url,params){
    return axios.post(url,params)
  }
 }
 export default http;

方法二:

axios修改全局默認(rèn)配置:

axios.defaults.baseURL = 'https://api.example.com';
axios.defaults.headers.common['Authorization'] = AUTH_TOKEN;
axios.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded';

axios配置攔截器:

// 添加一個(gè)請(qǐng)求攔截器
axios.interceptors.request.use(function (config) {
  // Do something before request is sent
  return config;
  //這里經(jīng)常搭配token使用,將token值配置到tokenkey中,將tokenkey放在請(qǐng)求頭中
  config.headers['Authorization'] = token;
  
 }, function (error) {
  // Do something with request error
  return Promise.reject(error);
 });

// 添加一個(gè)響應(yīng)攔截器
axios.interceptors.response.use(function (response) {
  // Do something with response data
  return response;
 }, function (error) {
  // Do something with response error
  return Promise.reject(error);
 });

這兩種方法就可以讓我們?cè)赼xios攔截器里拼接token了,而不是每一個(gè)請(qǐng)求都需要加一個(gè)token值

總結(jié)

以上所述是小編給大家介紹的Vue中axios攔截器如何單獨(dú)配置token,希望對(duì)大家有所幫助,如果大家有任何疑問請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!

相關(guān)文章

最新評(píng)論