spring boot 中导致session id 不一致问题,

httpservletrequest.reset 用于重置,但是在重置的时候也会清空相关数据,例如session存的信息。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,我可以回答你关于 Spring Boot 自定义 Session问题。 在 Spring Boot Session 的默认实现是基于 Servlet 的 HttpSession 接口。如果你想要自定义 Session 实现,可以通过实现 Spring SessionSession 接口,并在配置文件指定该实现来实现。 下面是一个使用 Redis 存储的自定义 Session 实现的示例代码: 首先,创建一个 RedisSession 类,实现 Session 接口,并实现相应的方法,如下所示: ```java public class RedisSession implements Session { private final String id; private Map<String, Object> attributes = new HashMap<>(); private boolean isNew; public RedisSession(String id) { this.id = id; this.isNew = true; } @Override public String getId() { return this.id; } @Override public <T> T getAttribute(String name) { return (T) this.attributes.get(name); } @Override public void setAttribute(String name, Object value) { this.attributes.put(name, value); this.isNew = false; } @Override public void removeAttribute(String name) { this.attributes.remove(name); } @Override public Instant getCreationTime() { throw new UnsupportedOperationException(); } @Override public void setLastAccessedTime(Instant lastAccessedTime) { throw new UnsupportedOperationException(); } @Override public Instant getLastAccessedTime() { throw new UnsupportedOperationException(); } @Override public void setMaxInactiveInterval(Duration interval) { throw new UnsupportedOperationException(); } @Override public Duration getMaxInactiveInterval() { throw new UnsupportedOperationException(); } @Override public boolean isExpired() { throw new UnsupportedOperationException(); } public boolean isNew() { return this.isNew; } } ``` 然后,创建一个 RedisSessionRepository 类,实现 SessionRepository 接口,并实现相应的方法,如下所示: ```java public class RedisSessionRepository implements SessionRepository<RedisSession> { private final RedisTemplate<String, RedisSession> redisTemplate; public RedisSessionRepository(RedisTemplate<String, RedisSession> redisTemplate) { this.redisTemplate = redisTemplate; } @Override public RedisSession createSession() { String id = UUID.randomUUID().toString(); RedisSession session = new RedisSession(id); this.redisTemplate.opsForValue().set(id, session); return session; } @Override public void save(RedisSession session) { this.redisTemplate.opsForValue().set(session.getId(), session); } @Override public RedisSession findById(String id) { return this.redisTemplate.opsForValue().get(id); } @Override public void deleteById(String id) { this.redisTemplate.delete(id); } } ``` 最后,在 Spring Boot 的配置文件指定使用 RedisSessionRepository 实现 SessionRepository,如下所示: ```properties spring.session.store-type=redis spring.redis.host=localhost spring.redis.port=6379 spring.redis.password= spring.redis.timeout=10000 spring.session.redis.flush-mode=on_save spring.session.redis.namespace=spring:session spring.session.repository-type=redis ``` 以上就是关于 Spring Boot 自定义 Session 的步骤,希望能帮到你。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值