【问题标题】:Authentication using ExpressJS使用 ExpressJS 进行身份验证
【发布时间】:2020-06-16 05:03:18
【问题描述】:

我想通过提供标头请求和带有用户密码(base64 加密)和用户名的请求来与身份验证服务器集成。我有以下需要使用 ExpressJS 实现的 API。

请求标头

Content-type: application/json
Authorization: Basic aW50.......
Basic Authentication
Username: xyz
Password: ********

请求

{
  "userName": "abc",
  "password: "*****"
}

【问题讨论】:

  • 请显示您尝试过的代码并告诉它有什么问题。
  • 可能没有多大帮助,但请注意 base64 是一种编码,而不是一种加密
  • 我不知道如何附加标头来发布身份验证请求。因此,我需要您的帮助。

标签: node.js api express authentication post


【解决方案1】:

使用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);
  });

【讨论】:

  • 用户名和密码有两个,一个在表头,一个是用户输入的。怎么写成代码?此外,还有内容类型和授权。
  • 另外,根据文档,该请求已被弃用。
  • 更新了使用Axios库的答案
  • 在哪里添加 URL 以向 LDAP API 发出请求?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-03-11
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-17
  • 2017-06-13
相关资源
最近更新 更多