【SpringSecurity-07】同账号多端登陆踢下线

SpringSecurity 同账号多端登陆踢下线

限制最大登陆用户数量

在这里插入图片描述

  • true表示已经登陆就不允许再次登陆
  • false表示允许再次登陆但是之前的登陆账号会被踢下线

自定义处理策略类CustomExpiredSessionStrategy

  • 页面跳转的方式
public class CustomExpiredSessionStrategy implements SessionInformationExpiredStrategy {
    
    //页面跳转的方式
    //页面跳转的处理逻辑
    private RedirectStrategy redirectStrategy = new DefaultRedirectStrategy();
    

    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event)
            throws IOException, ServletException {
        redirectStrategy.sendRedirect(event.getRequest(), event.getResponse(), "踢下线后跳转的路径");
    }
}
  • 前后端分离返回JSON的方式
public class CustomExpiredSessionStrategy implements SessionInformationExpiredStrategy {

    //前后端分离返回JSON的方式
    //jackson的JSON处理对象
    private ObjectMapper objectMapper = new ObjectMapper();


    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event)
            throws IOException, ServletException {

        Map<String, Object> map = new HashMap<>();
        map.put("code", 403);
        map.put("msg", "您的登陆已经超时或者已经在另一台机器登陆,您被迫下线!"
                + event.getSessionInformation().getLastRequest());

        String json = objectMapper.writeValueAsString(map);

        event.getResponse().setContentType("application/json;charset=utf-8");
        event.getResponse().getWriter().write(json);
    }
}

测试:

用谷歌浏览器登陆
在这里插入图片描述
再用火狐浏览器进行登陆,发现谷歌浏览器被题下线。
在这里插入图片描述

  • 3
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值