命名空间 HTML 5 本地存储键
我正在使用 HTML 5 本地存储 API 来存储用户的访问令牌。然后,请求会附加此令牌来访问我的服务器端 API。我想知道是否需要为我使用的键命名空间。浏览器是否会处理这个问题,或者我是否需要将密钥称为“my-awesome-app-token”?我注意到 Twitter 对某些搜索参数执行此操作。
顺便说一句,我知道在那里存储令牌并不安全,但安全性在这里并不重要,问题在于命名空间密钥。
谢谢!
I'm using the HTML 5 local storage API to store a user's access token. The requests then appends this token to access my server side API. I'm wondering if I need to namespace the keys I use. Does the browser take care of this or do I need to call the key something like 'my-awesome-app-token'? I noticed Twitter does this for some search params.
BTW, I know it's not secure to store the token there but security is not important here, the question is about namespacing keys.
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
浏览器会处理它:
http://dev.w3.org/html5/webstorage /#dom-localstorage:
The browser takes care of it:
http://dev.w3.org/html5/webstorage/#dom-localstorage:
我看到到目前为止这个问题的每个答案都告诉您,您不需要为本地存储键“命名空间”。我不太确定这是正确的。
假设您开发一个应用程序,并且需要运行该应用程序的多个实例。这可以是一个测试和生产系统,或者多个测试系统。无论如何,如果这些应用程序在相同的协议/服务器/端口上运行,但具有不同的 url,它们将可以访问相同的本地存储。
如果您的密钥使用任何其他开发人员可能使用的名称,例如“用户”、“数据”、“缓存”,那么您最终可能会完全从另一个应用程序读取本地存储。如果该应用程序在同一服务器上运行。
可以肯定的是,如果您想在同一服务器上运行多个实例,您可能不想在密钥中使用应用程序前缀,也可能不想使用应用程序 ID。
如果我误解了本地存储的工作原理,请纠正我。
I see that every answer on this question so far tells you that you don't need to "namespace" your localstorage keys. I'm not so sure that this is correct.
Let's say that you develop an application, and you need to run several instances of that application. This could be a test and production system, or several test systems. Anyway, if these apps run on the same protocol/server/port, but with a different url, they will have access to the same localstorage.
if your keys are using names that any other developer might use like "user", "data", "cache", you might end up reading localstorage from another application altogether. If that app is running on the same server.
To be sure, you might wan't to use an app-prefix to your keys and maybe also an app-id if you want to run several instances on the same server.
If I have misunderstood how localstorage works, please correct me.
不,您不需要这样做,每个子域都有一个本地存储。
No you don't need to do it, one localStorage for each subdomain.
其他答案解决了跨应用程序名称空间,但在同一应用程序使用不同库的情况下,名称空间也很重要。如果您正在开发这样的库,那么为您的库的存储使用命名空间可能是对其他库开发人员(甚至未来的您)的尊重。
我今晚遇到的一个例子使用存储密钥
nonce
。如果多个库都尝试在同一个应用程序中使用nonce
,那就会出现问题。如果您正在开发供其他开发人员使用的库,最好提供一个前缀选项:myapilib_
+nonce
。The other answers address cross-application namespaces, but namespaces are also important in the case of different libraries used by the same application. If you are developing such a library, it would probably be respectful to other library developers (or even your future self) to namespace your library's storage usage.
An example I came across tonight uses the storage key
nonce
. If multiple libraries each tried to usenonce
within the same app, that would be problematic. If you are developing a library for use by other developers, it would be nice to provide a prefixing option:myapilib_
+nonce
.