请求头authorization中加入认证信息

axios.interceptors.request.use(
  config => {
    //请求拦截
   if (config.url.includes('/es')) {
      const username = 'admin';
      const pasw = 'IwdPkriZxAqpN0hV2qCx1HyQ_';
      config.headers['Authorization'] = 'Basic' + ' ' + btoa(username + ':' + pasw);
    }
    return config;
  },
  error => {
    return Promise.reject(error);
  }
);

前端的base64加密和解密的使用方法,一个是加密:window.btoa(),一个是解密:window.atob()

var str = "hello";
var str64 = window.btoa("hello");
console.log("字符串是:"+str);  //"hello"
console.log("经base64编码后:"+str64); //"aGVsbG8="
console.log("base64解码后:"+window.atob(str64));//"hello"

Basic Authorization 认证核心就是每次请求是都必须使用该用户的uasename和password。

使用Basic Authorization认证有他的优点和缺点。优点在于开发调试简单,利于发起方控制等,缺点就是需要传递username和password,存在很大安全隐患;修改了账户名和密码需要重新校验等。

注意:‘Basic’ + ’ ’ + btoa(username + ‘:’ + pasw)中的空格。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值