修复会话管理强转失败问题

pull/76/head
smallbun 2024-01-08 00:37:26 +00:00 committed by Gitee
commit 252042f490
No known key found for this signature in database
GPG Key ID: 173E9B9CA92EEF8F
1 changed files with 33 additions and 24 deletions

View File

@ -25,6 +25,7 @@ import java.util.*;
import java.util.stream.Collectors;
import org.apache.commons.collections4.CollectionUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.data.redis.core.Cursor;
import org.springframework.data.redis.core.RedisCallback;
import org.springframework.data.redis.core.RedisOperations;
@ -108,30 +109,11 @@ public class ClusterSessionRegistryImpl<T extends org.springframework.session.Se
SecurityContext securityContext = session.getAttribute(SPRING_SECURITY_CONTEXT);
//转为实体
Authentication authentication = securityContext.getAuthentication();
UserDetails principal = (UserDetails) authentication.getPrincipal();
WebAuthenticationDetails details = (WebAuthenticationDetails) authentication
.getDetails();
Session sessionDetails = new Session(principal.getId(), principal.getUsername());
//last request
Instant instant = session.getLastAccessedTime();
ZoneId zoneId = ZoneId.systemDefault();
//最后请求时间
LocalDateTime lastRequestTime = instant.atZone(zoneId).toLocalDateTime();
sessionDetails.setLastRequestTime(lastRequestTime);
//登录时间
sessionDetails.setAuthenticationTime(details.getAuthenticationTime());
//登录时间
sessionDetails
.setAuthenticationProvider(details.getAuthenticationProvider().getType());
//用户类型
sessionDetails.setUserType(principal.getUserType());
//地理位置
sessionDetails.setGeoLocation(details.getGeoLocation());
//用户代理
sessionDetails.setUserAgent(details.getUserAgent());
//会话ID
sessionDetails.setSessionId(session.getId());
list.add(sessionDetails);
Object principal = authentication.getPrincipal();
if (principal instanceof UserDetails userDetails) {
Session sessionDetails = getSession(session, userDetails, authentication);
list.add(sessionDetails);
}
} catch (NullPointerException ignored) {
}
}
@ -139,6 +121,33 @@ public class ClusterSessionRegistryImpl<T extends org.springframework.session.Se
return list;
}
@NotNull
private static Session getSession(MapSession session, UserDetails userDetails, Authentication authentication) {
WebAuthenticationDetails details = (WebAuthenticationDetails) authentication
.getDetails();
Session sessionDetails = new Session(userDetails.getId(), userDetails.getUsername());
//last request
Instant instant = session.getLastAccessedTime();
ZoneId zoneId = ZoneId.systemDefault();
//最后请求时间
LocalDateTime lastRequestTime = instant.atZone(zoneId).toLocalDateTime();
sessionDetails.setLastRequestTime(lastRequestTime);
//登录时间
sessionDetails.setAuthenticationTime(details.getAuthenticationTime());
//登录时间
sessionDetails
.setAuthenticationProvider(details.getAuthenticationProvider().getType());
//用户类型
sessionDetails.setUserType(userDetails.getUserType());
//地理位置
sessionDetails.setGeoLocation(details.getGeoLocation());
//用户代理
sessionDetails.setUserAgent(details.getUserAgent());
//会话ID
sessionDetails.setSessionId(session.getId());
return sessionDetails;
}
/**
* session
*