简体   繁体   中英

Including web token in http requests

I'm using an ajax call to return a json web token on success. 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. 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. I'm not using any frameworks like Backbone or 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. Then, your subsequent ajaxSetup methods could retrieve the stored value.

Info on localStorage from MDN: 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. Get the token in that page and delete it from localStorage. Use it to authenticate once and refresh it, because of xss atacks you need to handle this carefully.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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