如何在 Spring Boot 中管理访客会话 [英] How to manage guest sessions in Spring Boot

查看:54
本文介绍了如何在 Spring Boot 中管理访客会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个始终处于访客模式的 Spring Boot 应用程序.不使用登录.问题是始终使用相同的应用程序实例.如果购物车里装满了产品,当我从其他浏览器或设备打开它时,它仍然是一样的.

I have a Spring Boot application which is always on guest mode. No login is used. The problem is that the same instance of the application is always used. If the shopping cart is filled with products its still the same when I open it from another browser or device.

我该如何解决这个问题?我需要使用 Spring Security 吗?

How can I resolve this? Do I need to use Spring Security?

推荐答案

你可以添加带有permit all、无需登录的spring security,它会自动为每个guest用户管理你的会话,并且会自动添加一个用户头JSESSIONID.

You can add spring security with permit all, no login, and it will manage your session automatically for each guest user, and will automatically add a user header JSESSIONID.

并且您可以告诉 spring 在您的网络安全配置中始终创建一个用户会话:

And you can tell spring to always create a user session in your web security config:

protected void configure(HttpSecurity http) throws Exception {

    http.sessionManagement().sessionCreationPolicy(SessionCreationPolicy.ALWAYS)

如果将来您要实现用户身份验证,您可以通过设置 .maximumSessions() 轻松限制最大会话/用户.

and if in future you will implement user authentication, you can easily limit maximum session/user by setting .maximumSessions() .

这篇关于如何在 Spring Boot 中管理访客会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆