如果我刷新网页,如何保留我的商店?
let store = new Vuex.Store( {
modules: {
demo, auth
},
strict: true
} );
let router = new VueRouter( {
mode: 'history',
saveScrollPosition: true,
routes: routes
} )
我使用历史模式,但如果我重新加载我的网页,我的商店是空的。有解决办法吗?
原文由 Seb 发布,翻译遵循 CC BY-SA 4.0 许可协议
要在应用刷新后保留状态数据,您可以使用
localStorage
或sessionStorage
。localStorage
中存储令牌的问题是关于该应用程序容易受到 XSS(跨站点脚本)攻击。除此之外,它是相当安全的。localStorage
绑定到域,因此www.yourapp.com
的数据即使在浏览器关闭后也会保留在那里,这与 cookie 不同,您不必管理哪个站点发出请求.具有相同域的浏览器上的所有选项卡将能够使用localStorage
中的数据如果不需要上述行为,您可以选择
sessionStorage
,它的工作方式几乎相同,但浏览器关闭时数据会被清除。csrf-token
以防止 CSRF(跨站点请求伪造)攻击。如果您打算继续使用 cookie,请确保它们在标头中将
httpOnly
标志设置为 true,否则它们就不好。总的来说,我发现 localStorage 是一种非常普遍推荐的用于维护令牌和其他应用程序数据的解决方案。
我会在这个答案中添加来源以供参考。
使用无状态(=无会话)身份验证时需要 CSRF 令牌吗? 检查接受的答案和其他答案。
https://github.com/OWASP/CheatSheetSeries/blob/master/cheatsheets/HTML5_Security_Cheat_Sheet.md
https://stormpath.com/blog/where-to-store-your-jwts-cookies-vs-html5-web-storage
https://www.whitehatsec.com/blog/web-storage-security/ 这会让您了解使用 localStorage 的陷阱和操作方法。