如何在tomcat 6中将会话cookie标记为安全(仅限https)

Ass*_*mon 5 security spring-security tomcat6 java-ee

我做了很多googeling并找不到答案.我已尝试在战争中的web.xml文件中设置以下内容而不会产生任何影响:

<session-config>
        <session-timeout>60</session-timeout>
        <cookie-config>
            <http-only>true</http-only>
            <secure>true</secure>
        </cookie-config>
    </session-config>
Run Code Online (Sandbox Code Playgroud)

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

sou*_*ica 5

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

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

org/apache/catalina/connector/Request.java下面是在方法末尾检查请求是否安全的代码,如果是,则secure在 cookie 上设置标志:

/**
 * 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);
    }
}
Run Code Online (Sandbox Code Playgroud)

更新:您可以使用过滤器等手动尝试自己设置它。您可以检查将“secure”标志设置为 JSESSION id cookie 的示例