首页 > 解决方案 > 使用 ExpressJS 进行身份验证

问题描述

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

请求标头

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

要求

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

标签: node.jsapiexpressauthenticationpost

解决方案


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

推荐阅读