如何阻止AXIOS向外部API发送nuxt-auth令牌?

如何使用axios从身份验证中间件背后的页面向外部API发出请求?该接口不需要任何身份验证。
每次发送包含auth令牌的请求时,都不会产生任何问题,但是每次向外部API发送auth令牌并不安全。到目前为止我已经尝试过了:

const config = {
            headers: { Authorization: '' }
        };
        let response = $axios.$get(`APIURL`,config)

但是,标头请求仍包含身份验证令牌。


解决方案

您确实不应该使用令牌头作为默认值。

您是否在单独的文件中使用AXios实例,您可以在其中配置请求/响应配置?那么就这么做吧。

仅为特定的url设置默认的auth标头,例如base API url,如下所示 http://api.localhost:3333并仅在那里发送令牌。

示例:

// Add a request interceptor

import axios from "axios";
axios.interceptors.request.use(config => {

const token = '';
   const apiUrl = https://api.xxdomain.com/;

   if (config.url.contains(apiUrl)) {
       config.headers['Authorization'] = 'Bearer ' + token;
   }else{
     config.headers['Content-Type'] = 'application/json';
   }

   return config;
},
error => {
   Promise.reject(error)
});

尝试阅读此帖子-&>helpful link

相关文章