18

默认情况下,Glassfish v3 不会在会话 cookie 上设置 httpOnly 标志(当像往常一样使用创建时request.getSession())。

我知道,有一种方法javax.servlet.SessionCookieConfig.setHttpOnly(),但我不确定这是否是最好的方法,如果是的话,最好的地方是放置那条线。

顺便说一句,当然它不能在 servlet 本身中完成(例如在 init() 中):

java.lang.IllegalStateException: PWC1426: 
Unable to configure httpOnly session tracking cookie property for 
servlet context /..., because this servlet context has already been initialized

一般来说,我更喜欢使用配置选项,例如在 web.xml 中。

4

2 回答 2

23

这是通过 Servlet 3.0 支持的web.xml(请参阅参考资料web-common_3_0.xsd):

<web-app>
  <session-config>
    <cookie-config>
      <!--             
        Specifies whether any session tracking cookies created 
        by this web application will be marked as HttpOnly
      -->
      <http-only>true</http-only>
    </cookie-config>
  </session-config>
</web-app>
于 2010-06-13T20:13:33.183 回答
3

您还可以添加<secure>true</secure>以提高安全性。

<session-config>
    <cookie-config>
        <http-only>true</http-only> 
        <secure>true</secure>
    </cookie-config>
</session-config>
于 2014-10-15T13:40:31.533 回答