栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

React JS-如何通过获取语句对凭据进行身份验证

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

React JS-如何通过获取语句对凭据进行身份验证

不要将您的授权令牌放在正文中。将其放在标题中。第一个功能将传递用户名,密码和身份验证类型(即

grant_type=password
)。然后,我的第二个功能将使用它来认证请求。不再需要传递任何用户信息,因为我的api根据传入的令牌知道谁在请求。OAuth
2.0
的当前文档在此处,您可以在Mozilla的网站上找到有关使用标头与fetch的更多信息。获取文档。

// request the token// subscribe to this event and use the returned json to save your token to state or session storageexport function requestAccessToken(data) {  const loginInfo = `${data}&grant_type=password`;  return fetch(`${API_URL}Token`, {    method: 'POST',    headers: new Headers({      'Content-Type': 'application/x-www-form-urlenpred',    }),    body: loginInfo,  })    .then((response) => response.json());    // in your case set state to returned token}// use said token to authenticate requestexport function requestUserInfo(token) {  return fetch(`${API_URL}api/participant/userinfo`, {    method: 'GET',    headers: new Headers({      Authorization: `Bearer ${token}`,    }),  })    .then((response) => response.json());}

我还建议:

  1. 从thunk或saga调用fetch,但这超出了问题的范围。

  2. 无需将令牌放在隐藏字段中。顺便说一句,仍然可以访问。只需保持其状态即可。您可以采取其他一些措施来保护它,但是这也不在问题范围之内。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/427237.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号