首页 > 解决方案 > server.session.cookie.max-age 似乎不起作用

问题描述

我在我的配置中添加了以下属性:

server.session.cookie.max-age=3600

Set-CookieHTTP 标头是:

Set-Cookie: JSESSIONID=3407BD3E1C7153D70EFC5DBD16B059E4; Path=/; Secure; HttpOnly

所以看起来 Spring 忽略了这个属性。是否已弃用?如果没有,为什么它不起作用?

这是我的配置:

@Override
protected void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests()
            .anyRequest()
            .authenticated()
            .and()
            .formLogin()
            .successForwardUrl("/")
            .defaultSuccessUrl("/", true)
            .permitAll()
            .and()
            .logout().logoutRequestMatcher(new AntPathRequestMatcher("/logout"))
            .deleteCookies("JSESSIONID")
            .invalidateHttpSession(true)
            .and()
            .rememberMe()
            .key("key")
            .tokenValiditySeconds(86400);
}

标签: javaspring-bootspring-security

解决方案


使用 Spring Boot 2.1.4,您必须使用属性server.servlet.session.cookie.max-age而不是server.session.cookie.max-age,请参阅Spring Boot 参考指南

附录 A. 常见应用程序属性

可以在application.properties文件内、文件内application.yml或作为命令行开关指定各种属性。本附录提供了常见 Spring Boot 属性的列表以及对使用它们的底层类的引用。

[...]

# EMBEDDED SERVER CONFIGURATION (ServerProperties)
[...]
server.servlet.session.cookie.max-age= # Maximum age of the session cookie. If a duration suffix is not specified, seconds will be used.

Spring Boot 2.0.0 RC1 配置变更日志


推荐阅读