前言
实现这块功能的时候,通过搜索,简单配置了下:
但是没生效。然后就是看代码,百度,还是没成功。
最后偶尔查到了这个:
既然找到了源码,那就断点,跑一下试试,结果,登录过程中,并没有进这个类org.springframework.security.web.authentication.session.ConcurrentSessionControlAuthenticationStrategy
。
然后根据类名,查找相关配置,就找到了其他的一些配置。
http://www.jetchen.cn/springsecurity-login-number/
https://www.cnblogs.com/dongjiao/p/14673895.html
https://www.cnblogs.com/sweetchildomine/p/6932488.html
https://blog.csdn.net/Xiaowu_First/article/details/115706985
https://blog.csdn.net/u013305082/article/details/52447798
https://blog.csdn.net/xiejx618/article/details/42892951
https://www.cnblogs.com/ryelqy/p/10304619.html
https://blog.csdn.net/elonpage/article/details/78955963
把配置整理一下,最后就基本成功了。
这是我的配置:
另外,在当前类引入这个
@Autowired
SessionRegistry sessionRegistry;
@Bean
public MyConcurrentSessionFilter myConcurrentSessionFilter(){
MyConcurrentSessionFilter filter = new MyConcurrentSessionFilter(sessionRegistry, new MySessionInformationExpiredStrategy());
return filter;
}
@Bean
public SessionRegistry sessionRegistry() {
return new MySessionRegistryImpl();
}
MySessionRegistryImpl
import org.springframework.security.core.session.SessionInformation;
import org.springframework.security.core.session.SessionRegistryImpl;
import org.springframework.security.core.userdetails.UserDetails;
import java.util.List;
/**
* @author qilong.sun
* @version v1.0
* @time 2021/10/27 9:49
* @title
* @description
*/
public class MySessionRegistryImpl extends SessionRegistryImpl {
@Override
public List<SessionInformation> getAllSessions(Object principal, boolean includeExpiredSessions) {
String username = ((UserDetails) principal).getUsername();
return super.getAllSessions(username, includeExpiredSessions);
}
}
MyConcurrentSessionFilter
import org.springframework.security.core.session.SessionRegistry;
import org.springframework.security.web.session.ConcurrentSessionFilter;
import org.springframework.security.web.session.SessionInformationExpiredStrategy;
/**
* @author qilong.sun
* @version v1.0
* @time 2021/10/27 13:15
* @title
* @description
*/
public class MyConcurrentSessionFilter extends ConcurrentSessionFilter {
public MyConcurrentSessionFilter(SessionRegistry sessionRegistry) {
super(sessionRegistry);
}
public MyConcurrentSessionFilter(SessionRegistry sessionRegistry, String expiredUrl) {
super(sessionRegistry, expiredUrl);
}
public MyConcurrentSessionFilter(SessionRegistry sessionRegistry, SessionInformationExpiredStrategy sessionInformationExpiredStrategy) {
super(sessionRegistry, sessionInformationExpiredStrategy);
}
}
还有UsernamePasswordAuthenticationFilter里面,登录的验证成功之后,要保存session
this.setDetails(request, authRequest);
// 调用验证
authenticate = this.getAuthenticationManager().authenticate(authRequest);
// 用户名密码验证通过后,注册session
sessionRegistry.registerNewSession(request.getSession().getId(), authRequest.getPrincipal());
可能不是全部,但是配置的地方比较多,自己多测测把,我这里仅当做笔记来用。后面整理完了,应该会把源码放出来。
解决maximumSessions(1)的问题
限制互相顶退是可以的,但是会出现这么个情况:
A登录 ok
B登录 ok
这时候A会退出
A登录 ok
这时候B会退出,但是同时,A再调接口,A也会退出。
解决办法:
https://www.cnblogs.com/dongjiao/p/14673895.html