node.js - 使用 ExpressJS 进行身份验证
问题描述
我想通过提供标头请求和带有用户密码(base64 加密)和用户名的请求来与身份验证服务器集成。我有以下需要使用 ExpressJS 实现的 API。
请求标头
Content-type: application/json
Authorization: Basic aW50.......
Basic Authentication
Username: xyz
Password: ********
要求
{
"userName": "abc",
"password: "*****"
}
解决方案
使用Axios,您可以发送自定义标头。
axios.post('/user', {
firstName: 'Fred',
lastName: 'Flintstone'
}, {
auth: { // Only basic auth
username: 'janedoe',
password: 's00pers3cret'
},
headers: {
'X-Requested-With': 'XMLHttpRequest',
'Authorization': 'Bearer 1223..'
},
})
.then(function (response) {
console.log(response);
})
.catch(function (error) {
console.log(error);
});
推荐阅读
- python - 难以使用适用于 x86 和 armv7 的 struct.pack 格式
- python - ValueError:无法将字符串转换为浮点数:'01-01-20 0:15'
- highcharts - Highcharts - Highstock 图表在悬停数据点上显示双线以显示工具提示
- sql - 给所有月份的数字,这些数字在我的数据库中作为字符串,当用户选择任何月份时,下一行将自动填充
- c# - 如何使用 C# MongoDb 驱动程序更新深度嵌套数组中的“类型安全”字段?
- xcode - 命令代码符号失败,退出代码为非零 + onesignal + xcode 12.3
- ssl - 来自购买的通配符 SSL 的 Kubernetes SSL
- vue.js - 在 vuejs 的上下文中单击 chartjs 不会发生日期选择
- android - WebView 无法在浏览器和应用程序中打开链接
- javascript - Formik - 从两个单独的字段设置相同的字段值