【问题标题】:Tomcat Spring Security - set session cookie expiration timeTomcat Spring Security - 设置会话cookie过期时间
【发布时间】:2014-11-27 10:13:53
【问题描述】:

我正在构建一个基于嵌入式 Tomcat - 7.0.55、Spring-Boot-1.1.6、Spring-webmvc/core - 4.0.7 和 Spring-Security - 3.2.5 的 Web 应用程序。

我的配置如下:

@Configuration
public class ServletCtxConfig {

    @Bean
    @Profile({ Profiles.PRODUCTION, Profiles.QA, Profiles.DEV })
    EmbeddedServletContainerFactory servletContainerFactory() {
        TomcatEmbeddedServletContainerFactory retVal = new TomcatEmbeddedServletContainerFactory();
        retVal.setContextPath("contextPath");
        retVal.setTomcatContextCustomizers(Arrays.asList(contextCustomizer()));
        retVal.setPort(111);
        Connector httpConnector = new Connector("org.apache.coyote.http11.Http11NioProtocol");
        httpConnector.setPort(123);
        httpConnector.setRedirectPort(456);
        retVal.addAdditionalTomcatConnectors(httpConnector);

        return retVal;
    }

    @Bean
    CustomCustomizer contextCustomizer() {
        return new CustomCustomizer();
    }

}

class CustomCustomizer implements TomcatContextCustomizer {

    @Value("${session.timeout:10080}")
    Integer sessionTimeOut;

    @Override
    public void customize(Context context) {
        context.setSessionCookieName("comilion-fw");
        context.setSessionTimeout(sessionTimeOut);
        context.setUseHttpOnly(false);
    }
}

我可以设置会话过期时间,但它不会反映在浏览器上的 cookie 过期时间上。 有人可以指导我如何设置cookie过期时间吗?

【问题讨论】:

  • 能否请您向我们展示您的会话管理配置,以便我们确定其中是否存在故障?
  • 将我的配置添加到问题中

标签: spring-mvc spring-security


【解决方案1】:

我最终所做的是自定义EmbeddedServletContainerFactory,如下所示:

    @Bean
    EmbeddedServletContainerFactory servletContainerFactory() {
    logger.debug("Raising Embedded servlet container with port: ", port, " and context path: ", contextPath);
    TomcatEmbeddedServletContainerFactory retVal = new TomcatEmbeddedServletContainerFactory() {
        @Override
        protected void postProcessContext(Context context) {
            SecurityConstraint securityConstraint = new SecurityConstraint();
            securityConstraint.setUserConstraint("CONFIDENTIAL");
            SecurityCollection collection = new SecurityCollection();
            collection.addPattern("/*");
            securityConstraint.addCollection(collection);
            context.addConstraint(securityConstraint);
        }
    };
    retVal.setContextPath(contextPath);
    retVal.setTomcatContextCustomizers(Arrays.asList(contextCustomizer()));
    retVal.addAdditionalTomcatConnectors(this.createConnection());
    return retVal;
}

@Bean
CustomCustomizer contextCustomizer() {
    return new CustomCustomizer();
}

class CustomCustomizer implements TomcatContextCustomizer {

    @Value(Properties.$_SESSION_TIMEOUT)
    Integer sessionTimeOut;

    @Override
    public void customize(Context context) {
        context.setSessionCookieName("XXX");
        context.setSessionTimeout(sessionTimeOut);
}
}

【讨论】:

    【解决方案2】:

    尝试在 web 应用程序初始化阶段访问 servlet 上下文并设置如下值:

    servletContext.getSessionCookieConfig().setMaxAge(600);
    

    看看WebApplicationInitializer和SpringServletContainerInitializer

    如果您仍然以某种方式使用 web.xml 运行 Web 应用程序,请前往 jsessionid-cookie-with-expiration-date-in-tomcat

    【讨论】:

    • 感谢您的回答,但我正在寻找来自 Spring-Security 框架的解决方案(因为这样的更改可能会改变框架行为)
    猜你喜欢
    • 1970-01-01
    • 2023-03-27
    • 2013-06-13
    • 1970-01-01
    • 1970-01-01
    • 2011-07-20
    • 2011-03-04
    • 2016-04-20
    • 1970-01-01
    相关资源
    最近更新 更多