一、登录校验成功时,向session加入关键信息,代码如下:
session.setAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, userId);
二、获取当前session账户的登录数,及有多少个客户端使用了当前账户登录:
@Autowired
private RedisOperationsSessionRepository sessionRepository;
public Integer fetchSameLoginNum(HttpServletRequest request) {
int result = 0;
HttpSession session = request.getSession();
if (session != null) {
String userId = (String) session.getAttribute(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME);
if (StringUtils.isNotEmpty(userId)) {
Map<String, ? extends Session> nameAndIndexValue = sessionRepository
.findByIndexNameAndIndexValue(FindByIndexNameSessionRepository.PRINCIPAL_NAME_INDEX_NAME, userId);
result = nameAndIndexValue.size();
}
}
return result;
}