09、SpringBoot集成SpringSecurity实战:设置用户最大登录数

前言

在项目中,可能会有这样的需求,只允许用户同一时间只能在一处登录。

实现

设置Session过期策略,创建SessionExpiredStrategy 类

@Component
public class SessionExpiredStrategy implements SessionInformationExpiredStrategy {
   
   
     
    @Autowired
    private SignInFailureHandler signInFailureHandler;

    @Override
    public void onExpiredSessionDetected(SessionInformationExpiredEvent event) throws IOException {
   
   
     
        UserDetails userDetails = (UserDetails) event.getSessionInformation().getPrincipal();
        String format = String.format("尊敬的[%s],您的账号在其他地方登录,请及时修改密码!", userDetails.getUsername());
        AuthenticationException exception = new AuthenticationServiceException(format);
        signInFailureHandler.onAuthenticationFailure(event

版权声明:本文不是「本站」原创文章,版权归原作者所有 | 原文地址: