http - ServletResponse.reset() 是阻止未经授权(注销)用户访问 servlet 的可行方法吗?

标签 http servlets session-cookies servlet-filters session-state

我想知道 ServletResponse.reset() 是否是我需要阻止未登录用户访问 servlet 的额外步骤,或者它是否存在风险或缺点。

背景:

方式my website有效,最初用户只能在创建帐户并使用这些凭据登录后才能看到某些页面。我最初遇到的问题是,即使我使用了 session.invalidate(),当用户注销时,他们可以重新发送请求(例如,通过后退按钮)并且能够从未授权状态访问受限内容。假设 session 已过期,如上所述,我不确定这怎么可能或者可以采取什么措施来防止它发生。

通过实验,作为一种变通方法,我尝试了 ServletResponse.reset()似乎 解决了它,但我不明白它为什么起作用,并且因此,我不确定这是一个可靠可行的解决方案。

我的具体问题是:

  • 重置响应缓冲区(包括 header 和 http 状态代码)是安全的还是推荐的做法,或者有更好的方法吗?

  • 我如何管理使用 session 对象而不是使用 session cookie 的登录?

最佳答案

我不认为 ServletResponse.reset() 会造成任何伤害,但它不是标准操作程序,因此没有必要。只需确保您在使 session 无效和验证 session 方面都做了正确的事情。

使 session 无效:

  • 调用 session.invalidate() 以删除绑定(bind)到 session 的 session 属性,但还要确保您的代码明确删除它自己对先前 session 状态的陈旧引用。可以通过使用完全匹配的条件定位 cookie 并将其最长期限设置为 0 来明确删除 session cookie。

验证 session :

  • 认证时,可以通过getSession(false)是否返回null来判断是否有事件 session 。如果是这样,那么您可以使其无效,并且您可以检查request.isRequestedSessionIdValid(),或明确检查是否存在您在 session 管理中使用的任何特定属性方案。这将使您能够正确验证 session 的状态。

来自 JavaDocs:

ServletResponse

    void reset()

        Clears any data that exists in the buffer as well as the status code and headers. If the response has been         committed, this method throws an IllegalStateException.

    void invalidate()

        Invalidates this session then unbinds any objects bound to it.

ServletRequest

    boolean isRequestedSessionIdValid()

        Checks whether the requested session ID is still valid.

    HttpSession getSession(boolean create)

        Returns the current HttpSession associated with this request or, if there is no current session and create is true,
        returns a new session. If create is false and the request has no valid HttpSession, this method returns null.
        To make sure the session is properly maintained, you must call this method before the response is committed.
         is using cookies to maintain session integrity and is asked to create a new session when the response is         committed, an IllegalStateException is thrown.

关于http - ServletResponse.reset() 是阻止未经授权(注销)用户访问 servlet 的可行方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41517344/

相关文章:

java - Jetty如何检测HTTP连接是否被客户端关闭

servlets - 生成新的 session ID

php - SQLite 作为 session 存储

java - JSOUP-无法解析相对 URL

http - Vignette CMS 是否提供压缩的 HTML、CSS 和 JavaScript 内容?

Java servlet : transfer XML file to servlet

security - 我可以禁止随服务器请求自动发送 cookie 吗?

php - setcookie php 很慢或者发生了什么?

php - 无法回显 file_get_contents 中的函数内容

php - 模仿浏览器对汉字的 URL 编码?