如何使用axios从身份验证中间件背后的页面向外部API发出请求?该API不需要任何身份验证。
每次发送包含身份验证令牌的请求时,都不会产生任何问题,但每次都将身份验证令牌发送到外部API并不安全。到目前为止,我已经尝试过了:
const config = {
headers: { Authorization: '' }
};
let response = $axios.$get(`APIURL`,config)但是,头请求仍然包含auth令牌。
发布于 2021-03-06 21:13:21
你确实不应该使用令牌头作为默认值。
您是否在单独的文件中使用axios实例,您可以在其中配置请求/响应配置?那就这么做吧。
只为特定的url设置默认的auth头,例如像http://api.localhost:3333这样的基础API url,并且只在那里发送令牌。
示例:
// 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
https://stackoverflow.com/questions/66503467
复制相似问题