SpringBoot-----Security安全机制的sessions配置策略

本文介绍了SpringBoot中Security的安全机制,特别是关于session的配置策略。包括always(始终创建HttpSession)、never(从不创建但使用已存在)、if_required(按需创建)和stateless(无状态,推荐使用)。详细解析了stateless策略为何被推荐,并提示可查看SecurityProperties.java源代码进行深入理解。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、配置security.sessions策略

#安全配置
security:
  sessions: stateless
  basic:
    enabled: true #启用SpringSecurity的安全配置
  user:
    name: wendy #认证用户名
    password: wendy1 #认证密码
    role:  #授权
    - USER

2、security.sessions策略如下:

always:保存session状态(每次会话都保存,可能会导致内存溢出【Always create an {@link HttpSession}】)

never:不会创建HttpSession,但是会使用已经存在的HttpSession[Spring Security will never create an {@link HttpSession}]

if_required:仅在需要HttpSession创建【Spring Security will only create an {@link HttpSession} if required】

stateless:不会保存session状态【 Spring Security will never create an {@link HttpSession} and it will never use it
     * to obtain the {@link SecurityContext}】

注意:stateless策略推荐使用,也是默认配置

3、具体跟查看源代码SecurityProperties.java的配置项

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值