在 Laravel 中保存 JWT 令牌的位置

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32494545/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:19:41  来源:igfitidea点击:

Where to save the JWT Token in Laravel

restlaravelcookiesjwt

提问by omer Farooq

Well i have a scenario where i have a rest api built on laravel and controlled by JWT. Then i use another route to request the api for the token. But once i receive the token after giving the login credentials where should i save the token?

好吧,我有一个场景,我有一个基于 Laravel 并由 JWT 控制的 rest api。然后我使用另一条路线来请求令牌的 api。但是一旦我在提供登录凭据后收到令牌,我应该在哪里保存令牌?

I think it should be saved in the client's browser, so on the next request we just fetch the token from the browser and then go through another request. Saving the token in the browser will also give the advantage with scaling the server, because if its on the server's session not all the servers will be in sync with this session data.

我认为它应该保存在客户端的浏览器中,因此在下一个请求中,我们只需从浏览器中获取令牌,然后再进行另一个请求。将令牌保存在浏览器中也有利于扩展服务器,因为如果它在服务器的会话上,则并非所有服务器都与此会话数据同步。

Now i want your opinion on this, should i store the token in a cookie or in the browser's LocalStorage? I know how to store it in cookie but dont know how to store in localstorage with js. It would be helpful if you could point out to any tutorial for this.

现在我想听听您的意见,我应该将令牌存储在 cookie 中还是浏览器的 LocalStorage 中?我知道如何将它存储在 cookie 中,但不知道如何使用 js 存储在 localstorage 中。如果您可以为此指出任何教程,那将会很有帮助。

采纳答案by James Flight

There is a good article hereon local storage vs cookies with JWT. They recommend cookies because of XSS vulnerabilities when using local storage.

这是一个很好的文章在这里的本地存储器VS智威汤逊饼干。由于使用本地存储时存在 XSS 漏洞,他们推荐使用 cookie。

It is also worth pointing out that if you are using the Laravel JWT Authpackage, that the token is still being stored on the server (using Laravel's cache system) by default, and so will not scale across servers. You should be able to change this with the storageconfig option.

还值得指出的是,如果您使用Laravel JWT Auth包,默认情况下令牌仍然存储在服务器上(使用 Laravel 的缓存系统),因此不会跨服务器扩展。您应该可以使用storageconfig 选项更改此设置。