httpOnly会话Cookie + Servlet 3.0(例如Glassfish v3)

问题描述:

默认情况下,Glassfish v3不会在会话cookie上设置httpOnly标志(当按照惯例使用request.getSession()创建时)。httpOnly会话Cookie + Servlet 3.0(例如Glassfish v3)

我知道,有一个方法javax.servlet.SessionCookieConfig.setHttpOnly(),但我不确定,如果这是最好的办法,如果是的话,最好的地方放在那里。

BTW,当然这不能可以在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中。

这是通过一个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> 
+0

谢谢,它的工作! (我只查看了web-app_3_0.xsd,但没有意识到,该模式使用了') – 2010-06-13 20:52:23

+0

@chris_l不客气。我也没有意识到,我注意到,虽然谷歌搜索:) – 2010-06-13 21:10:11

+0

我怎样才能在我的web.xml中声明web-common_3_0.xsd?我将我的web.xml升级到3.0 user6123723 2013-08-30 15:34:53

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

<session-config> 
    <cookie-config> 
     <http-only>true</http-only> 
     <secure>true</secure> 
    </cookie-config> 
</session-config> 
+0

由于某种原因,安全标记向我发送ViewExpiredException。我错过了什么? – 2017-09-11 10:20:43