简体   繁体   English

如何阻止 axios 将 nuxt-auth 令牌发送到外部 API?

[英]How to stop axios from sending nuxt-auth token to external apis?

How to make requests from a page behind the auth middleware to an external API using axios?如何使用 axios 从 auth 中间件后面的页面向外部 API 发出请求? the API doesn't need any authentication. API 不需要任何身份验证。
Every time requests are sent containing auth token, it doesn't make any problems but I don't feel safe to send auth tokens to an external API each time.每次发送包含身份验证令牌的请求时,它都不会产生任何问题,但我每次都将身份验证令牌发送到外部 API 并不安全。 I tried this so far:到目前为止我试过这个:

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

however header request still contains auth token.但是 header 请求仍然包含身份验证令牌。

You shouldn't use token header as default indeed.您不应该使用令牌 header 作为默认值。

Are You using axios instance in seperate file where You can configure the request/response config?您是否在可以配置请求/响应配置的单独文件中使用 axios 实例? So do so.所以就这样做吧。

Set the default auth header only for specific url, for example base API url like http://api.localhost:3333 and send the token only there. Set the default auth header only for specific url, for example base API url like http://api.localhost:3333 and send the token only there.

Example:例子:

// 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)
});

try to read also this post-> helpful link尝试阅读这篇文章-> 有用的链接

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM