public CurrentUser getCurrentUserByJID(String JID){
CurrentUser currentUser;
try {
RedisOperations<Object, Object> ops = repository.getSessionRedisOperations();
Map<Object, Object> entries = ops.boundHashOps(JID).entries();
SecurityContext securityContext = (SecurityContext) entries.get("sessionAttr:SPRING_SECURITY_CONTEXT");
if (securityContext != null) {
Authentication authentication = securityContext.getAuthentication();
currentUser = (CurrentUser) authentication.getPrincipal();
return currentUser;
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
ROSR是spring session包里的,定义了变量,redis的key会用到。 封装了RedisOperations用来操作redis。