企业🤖AI Agent构建引擎,智能编排和调试,一键部署,支持私有化部署方案 广告
这里的会话管理是指:允许同一账号同时在多台设备上登录,还是限制同一账号同时只能在同一台设备上登录。默认允许同一账号在多台设备上登录。 <br/> 实现步骤如下: **1. 实现账号过期处理策略接口** ```java @Component public class CustomSessionInformationExpiredStrategy implements SessionInformationExpiredStrategy { @Override public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException, ServletException { Map<String, Object> map = new HashMap<>(16); map.put("code", 2001); map.put("message", "账号已下线"); HttpServletResponse response = event.getResponse(); response.setContentType("text/json;charset=utf-8"); response.getWriter().write(JSON.toJSONString(map)); } } ``` **2. 设置账号并发数量并注册策略接口** ```java @Configuration @EnableWebSecurity public class WebSecurityConfig extends WebSecurityConfigurerAdapter { ... @Autowired private CustomSessionInformationExpiredStrategy expiredStrategy; @Override protected void configure(HttpSecurity http) throws Exception { ... http.sessionManagement() //这里设置账号并发数量为1,即限制同一账号同时只能在同一台设备上登录 .maximumSessions(1) .expiredSessionStrategy(expiredStrategy); ... } ... } ``` **3. 测试结果** (1)允许同一账号同时在多台设备上登录:如果超出预定的并发数量,则会随机将其中的一个旧登录挤下线,以便给新登录让位。 (2)限制同一账号同时只能在同一台设备上登录:新登录的会将旧登录的挤下线,以便给新登录让位。