简体   繁体   English

在 http 请求中包含网络令牌

[英]Including web token in http requests

I'm using an ajax call to return a json web token on success.我正在使用 ajax 调用在成功时返回一个 json web 令牌。 There's lots of information from sites like jwt.io that you should then update your http request headers to "Authorization Bearer 'token'" but I have no idea how to do that.有很多来自 jwt.io 等网站的信息,您应该将 http 请求标头更新为“授权承载'令牌'”,但我不知道该怎么做。 I update subsequent ajax calls using ajaxSetup(), but it's not a single page app so I will be using standard http gets and posts afterwards.我使用 ajaxSetup() 更新后续的 ajax 调用,但它不是单页应用程序,所以我将使用标准的 http 获取和帖子之后。 I'm not using any frameworks like Backbone or Angular.我没有使用任何框架,如 Backbone 或 Angular。 Thanks!谢谢!

If you are outside the realm of a single page application you could store your token value in the client side context by using a cookie or local storage.如果您在单页应用程序的范围之外,您可以使用 cookie 或本地存储将您的令牌值存储在客户端上下文中。 Then, your subsequent ajaxSetup methods could retrieve the stored value.然后,您后续的 ajaxSetup 方法可以检索存储的值。

Info on localStorage from MDN: https://developer.mozilla.org/en-US/docs/Web/API/Window/localStorage来自 MDN 的 localStorage 信息: https : //developer.mozilla.org/en-US/docs/Web/API/Window/localStorage

Once you get the token you store it in localStorage, then redirect to the next page.获得令牌后,将其存储在 localStorage 中,然后重定向到下一页。 Get the token in that page and delete it from localStorage.获取该页面中的令牌并将其从 localStorage 中删除。 Use it to authenticate once and refresh it, because of xss atacks you need to handle this carefully.使用它进行一次身份验证并刷新它,由于 xss 攻击,您需要小心处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM