淘宝客单页网站,安徽省住房城乡建设厅网站,河南建筑材料价格信息网,网站设计亮点1. 需求
在项目中往往需要实现一个限制不同设备同时登录的功能#xff0c;比如我只允许同一时间只有一个客户端能登录#xff0c;而其他的已登陆的客户端会被挤出来
而springsecurity中恰好就帮我们实现好了对应的接口功能#xff0c;我们只需要自定义配置就好
2. 结合sp…1. 需求
在项目中往往需要实现一个限制不同设备同时登录的功能比如我只允许同一时间只有一个客户端能登录而其他的已登陆的客户端会被挤出来
而springsecurity中恰好就帮我们实现好了对应的接口功能我们只需要自定义配置就好
2. 结合springsecurity代码
这里简单输出JSON串实际应用中需封装返回前端axios的响应res做为响应体
public class MySessionInformationExpiredStrategy implements SessionInfomationExpiredStrategy{Overridepublic void onExpiredSessionDetected(SessionInfomationExpiredEvent event) throws IOException, ServletException{//创建map封装结果HashMap res new HashMap();res.put(code, 555);res.put(message, This account has been logged in from another device);//返回响应HttpservletResponse response event.getResponse();response.setContentType(application/json;charsetUTF-8); response.getWriter().println(JSON.toJSONString(res)); }
}
3. 配置文件配置
在securityconfig配置类中添加
http.sessionManagement(session - {//这里的1 就是同一时刻的会话并发数session.maximumSession(1).expiredSessionStrategy(new MySessionInfomationExpiredStrategy());
});