You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot中sessionManagement()已弃用,如何设置SessionCreationPolicy.STATELESS?

如何设置SessionCreationPolicy.STATELESS(替代已弃用的sessionManagement()方法)

在Spring Security新版本中,无参的sessionManagement()方法已被标记为弃用并将被移除,你可以通过lambda配置形式替代原有写法,实现无状态会话的设置。

修改后的完整代码

@Bean
SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http.authorizeHttpRequests(requests -> requests.anyRequest().authenticated())
        // 使用lambda配置会话管理,设置无状态
        .sessionManagement(session -> session.sessionCreationPolicy(SessionCreationPolicy.STATELESS))
        .authenticationProvider(authenticationProvider());

    return http.build();
}

原弃用提示翻译

弃用提示:sessionManagement()方法已被弃用,将在后续版本中移除,请使用接受Customizer<SessionManagementConfigurer<HttpSecurity>>参数的sessionManagement()重载方法替代。

说明

新版本Spring Security推荐使用这种lambda链式配置的风格,既保留了原有设置无状态会话的功能,又符合框架的最新设计规范,可避免后续版本的兼容问题。

内容的提问来源于stack exchange,提问作者ylmzzsy

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 02:53:13