无需 Cookie 和 URL 重写的会话处理
我有一个旧网站(servlet、JSP 和 Struts)。目前,会话管理是通过使用 cookie 来处理的。我想重新设计这个网站以使浏览器独立。
我知道有一种替代方法 - URL 重写,但是,这对我来说重写(编码)应用程序中的所有 URL 是不可行的。
我正在寻找一种不会对我的代码产生太大影响的解决方案。如果有人有可行的解决方案,请建议我。这对我来说会有很大的帮助。
I have an old web site(servlets, JSP, and Struts). Currently, session management handled by using cookies. I wanted to redesign this site to make browser independent.
I know there is an alternate - URL re-writing, however, this is not feasible for me to re-write(encode) all the URLs in my application.
I am looking for a solution which should not impact my code much. Please suggest me, if anyone is having a feasible solution. It will be a great help to me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这毫无意义。只需使用 URL 重写即可。否则,您基本上最终会重新发明整个 HttpSession 概念。您需要更改代码中使用
HttpSession
的每一行。这比修复 Web 应用程序以利用 URL 重写需要更多的时间。咬紧牙关,将此作为吸取的教训,这样您就不会犯同样的错误,即不为未来需要支持不支持 cookie 的浏览器的项目进行 URL 重写。This makes no sense. Just use URL rewriting. Otherwise you basically end up in reinventing the whole
HttpSession
concept. You'd need to change every line in your code which usesHttpSession
. This will require much more time than fixing your webapp to utilize URL rewriting. Bite the bullet and take this as a lesson learnt so that you don't make the same mistake of not doing URL rewriting for the future projects which requires supporting browsers which don't support cookies.据我想象,除了 URL 或 Cookie 中的会话令牌之外,只有三分之一的选项是如此肮脏和不切实际,我不会推荐它;)但是我们开始了:
在会话的每个页面上都有一个隐藏的表单字段令牌和对服务器的每个请求都必须是包含隐藏字段值的表单提交。
As far as I can imagine there is only one third option other than session token in URL or Cookie that is so dirty and impractical that I would not recommend it ;) But here we go:
Have a hidden form field on every page with the session token and every request to the server must be a form submit including the hidden fields value.
从我的角度来看,当仅针对浏览器独立性进行优化(排除通过 GET 的隐式会话)时,cookie 已经是最佳解决方案。
使用 javascript 重写所有 a.href 以添加会话哈希作为参数。
如果您追求真正的浏览器独立性,那么这不应该是您的解决方案,因为 cookie 比 javascript 支持更广泛。
更大的数据块可以存储在 LocalStorage 中。
。
对于较大的客户端会话数据,易于设置且速度相当快 但您仍然需要通过 POST/GET AJAX 调用向服务器发送一些数据,以实际跟踪服务器端的会话。
Cookie 应该是朋友,而不是敌人。
From my point of view cookies are already the best solution when optimizing for browser independence only (excluding implicit sessions via GET).
Rewrite all a.href with javascript to add the session hash as parameter.
This shouldn't be your solution if you go for true browser independence as cookies are more widespread than javascript support.
Larger chunks of data can be stored in LocalStorage.
and
Easy to set up and considerably faster for larger client side session data. But you still have to send some data to the server via POST/GET AJAX calls to actually track the session on the server-side.
Cookies should be friends, not foes.