欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

封装axios与express加密token

程序员文章站 2022-06-21 20:24:02
import axios from 'axios'axios.interceptors.request.use(success => { // console.log(success) return success}, err => { console.log(err) return err})axios.interceptors.response.use(success => { // console.log(success) return success...
import axios from 'axios'
axios.interceptors.request.use(success => {
  var arr = []
  document.cookie.split(';').map(res=>{arr = arr.concat(res.split('='))})
  success.headers['token'] = arr[arr.length-1]
  return success
}, err => {
  // console.log(err)
  return err
})
axios.interceptors.response.use(success => {
  // console.log('header',success.headers)
  document.cookie = `token=${success.headers['setcookie']}` // 只有后端设置了暴露响应头
  //res.header('Access-Control-Expose-Headers','setcookie,X-Powered-By')
  return success
}, err => {
  console.log(err)
  return err
})
let cancelToken = axios.CancelToken
class Http {
  cancelToken = false // false不取消发送 true 取消发送
  self = this
  header = {
    timeout: 3000,
    url: '',
    baseURL: '',
    method: '',
    params: '',
    data: '',
    responseType: 'json',
    cancelToken: '',
    headers: {
    },
    withCredentials: false
  }
  constructor () {
    this.get = this.getInfo
    this.post = this.postInfo
    this.fileupload = this.fileuploadInfo
  }
  cancal () {
    this.header.cancelToken = new cancelToken(function (res) {
      res('取消发送')
    })
  }
  getInfo (url,data, cancal = false) {
    if(cancal) {
      this.cancal()
    }
    this.header.url = url
    this.header.params = data
    this.header.method = 'get'
    return axios({...this.header})
  }
  postInfo (url,data, cancal = false) {
    if(cancal) {
      this.cancal()
    }
    this.header.url = url
    this.header.data = data
    this.header.method = 'post'
    return axios({...this.header})
  }
  fileuploadInfo (url,form, cancal = false) {
    if(cancal) {
      this.cancal()
    }
    this.header.url = url
    this.header.data = form
    this.header.method = 'post'
    this.header.responseType = 'blob'
    return axios({...this.header})
  }
}
const api = new Http()
export default api

后端express
加密文件

var crypto = require('crypto')
function aesEncrypt(data, key) {
  const cipher = crypto.createCipher('aes192', key);
  var crypted = cipher.update(data, 'utf8', 'hex');
  crypted += cipher.final('hex');
  return crypted;
}

function aesDecrypt(encrypted, key) {
  const decipher = crypto.createDecipher('aes192', key);
  var decrypted = decipher.update(encrypted, 'hex', 'utf8');
  decrypted += decipher.final('utf8');
  return decrypted;
}
module.exports = {
  aesEncrypt,
  aesDecrypt
}
var express = require('express')
var crypto = require('./crypto')
var app = express()
app.all('*', function(req, res, next) {
  res.header("Access-Control-Allow-Origin", "*");
  res.header("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT");
  res.header("Access-Control-Allow-Headers", "token,name,age,Origin, X-Requested-With, Content-Type, Accept, Authorization");
  res.header('X-Powered-By','123')
  res.header('Access-Control-Expose-Headers','setcookie,X-Powered-By')
  next();
});
app.use('/html',(req,res) => {
  var cry = crypto.aesEncrypt('1212','123')
  res.header('setcookie', cry)
  console.log(crypto.aesDecrypt(cry,'123'))
  res.json({ok:200})
})


app.listen(3000,()=>{
  console.log('服务开启')
})

本文地址:https://blog.csdn.net/qq_43505774/article/details/112897313

相关标签: vue ajax js