【问题标题】:How to flag session cookie as secure (https only) in tomcat 6如何在 tomcat 6 中将会话 cookie 标记为安全(仅限 https)
【发布时间】:2012-07-30 20:06:30
【问题描述】:

我已经做了很多谷歌搜索,但找不到答案。我尝试在战争中的 web.xml 文件中设置以下内容,但没有任何影响:

<session-config>
        <session-timeout>60</session-timeout>
        <cookie-config>
            <http-only>true</http-only>
            <secure>true</secure>
        </cookie-config>
    </session-config>

在 tomcat context.xml 文件中添加 useHttpOnly 可以将 cookie 限制为仅 http,但我仍然需要确保它们安全。

【问题讨论】:

    标签: security jakarta-ee spring-security tomcat6


    【解决方案1】:

    你不需要做任何事情。只要启动会话的请求是https,Tomcat就会将会话cookie标记为secure。

    我还查看了是否有任何正式记录该事实的内容,但我找不到。但这至少是 Tomcat 6.0.32 及更高版本的行为。

    这是来自org/apache/catalina/connector/Request.java 的代码,在方法结束时,它会检查请求是否安全,如果安全,则在cookie 上设置secure 标志:

    /**
     * Configures the given JSESSIONID cookie.
     *
     * @param cookie The JSESSIONID cookie to be configured
     */
    protected void configureSessionCookie(Cookie cookie) {
        cookie.setMaxAge(-1);
    
        Context ctxt = getContext();
    
        String contextPath = null;
        if (ctxt != null && !getConnector().getEmptySessionPath()) {
            if (ctxt.getSessionCookiePath() != null) {
                contextPath = ctxt.getSessionCookiePath();
            } else {
                contextPath = ctxt.getEncodedPath();
            }
        }
        if ((contextPath != null) && (contextPath.length() > 0)) {
            cookie.setPath(contextPath);
        } else {
            cookie.setPath("/");
        }
    
        if (ctxt != null && ctxt.getSessionCookieDomain() != null) {
            cookie.setDomain(ctxt.getSessionCookieDomain());
        }
    
        if (isSecure()) {
            cookie.setSecure(true);
        }
    }
    

    更新:您可以手动尝试通过使用过滤器等自行设置。您可以查看以下示例 set 'secure' flag to JSESSION id cookie

    【讨论】:

    • 只是想确认一下:在您选择的浏览器开发工具(在我的例子中是 Safari 的 Webinspector)中查看会话 cookie 会告诉您 cookie 确实是仅安全 cookie
    猜你喜欢
    • 2015-12-23
    • 2021-10-25
    • 2019-01-15
    • 1970-01-01
    • 2011-04-15
    • 1970-01-01
    • 2015-05-22
    • 2012-01-02
    • 2018-09-08
    相关资源
    最近更新 更多