This commit is contained in:
2024-04-22 21:22:23 +08:00
parent 4cff327321
commit 7f2d8fb39a
4 changed files with 34 additions and 25 deletions

View File

@@ -180,14 +180,13 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
lastActivateTime.clear();
for (Map.Entry<String, LocalDateTime> entry : copy.entrySet()) {
int count = repository.updateLastActiveTimeByIdAndLastActiveTimeLessThan(
entry.getValue(), entry.getKey(), entry.getValue());
int count =
repository.updateLastActiveTimeByIdAndLastActiveTimeLessThan(
entry.getValue(), entry.getKey(), entry.getValue());
log.info("更新会话最后活跃时间: {} to {}, count:{}", entry.getKey(), entry.getValue(), count);
}
}
public void touch(LoginUserDto loginDto) {
@@ -250,25 +249,28 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
public void deleteLogin(String sessionId) {
try {
LoginUserEntity loginUser = this.get(sessionId);
if (this.repository.existsById(sessionId)) {
this.repository.deleteById(sessionId);
LoginUserEntity loginUser = this.get(sessionId);
// 如果当前用户没有其他会话, 则清除用户缓存
if (this.countByUserId(loginUser.getUser().getId()) == 0) {
userService.clearUserCache(loginUser.getUser().getId());
rateLimiterService.destroyRL(sessionId, loginUser.getUser().getId());
this.repository.deleteById(sessionId);
// 如果当前用户没有其他会话, 则清除用户缓存
if (this.countByUserId(loginUser.getUser().getId()) == 0) {
userService.clearUserCache(loginUser.getUser().getId());
rateLimiterService.destroyRL(sessionId, loginUser.getUser().getId());
} else {
rateLimiterService.destroyRL(sessionId, null);
}
}else {
rateLimiterService.destroyRL(sessionId, null);
log.warn("会话 {} 不存在, 无需删除登录信息", sessionId);
}
} catch (Exception e) {
log.warn("删除会话失败 {}",sessionId, e);
log.warn("删除会话失败 {}", sessionId, e);
}
this.clearCache(sessionId);
@@ -276,14 +278,17 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
private int countByUserId(String id) {
return Math.toIntExact(this.count(new Specification<LoginUserEntity>() {
@Override
public Predicate toPredicate(Root<LoginUserEntity> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.equal(root.get("user").get("id"), id);
}
}));
return Math.toIntExact(
this.count(
new Specification<LoginUserEntity>() {
@Override
public Predicate toPredicate(
Root<LoginUserEntity> root,
CriteriaQuery<?> query,
CriteriaBuilder criteriaBuilder) {
return criteriaBuilder.equal(root.get("user").get("id"), id);
}
}));
}
public void logout(IdRequest request) {

View File

@@ -20,6 +20,7 @@ import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.convert.ConversionService;
@@ -131,7 +132,7 @@ class OrganizationService extends BaseService<OrganizationEntity, OrganizationRe
List<String> names = this.repository.findNamesByIds(strings);
Map<String, String> javaMap = Stream.ofAll(names).toMap(x -> x.split(",")[0], x -> x.split(",")[1]).toJavaMap();
Map<String, String> javaMap = Stream.ofAll(names).filter(StringUtils::isNotBlank).toMap(x -> x.split(",")[0], x -> x.split(",")[1]).toJavaMap();
strings.forEach(x -> javaMap.putIfAbsent(x, null));

View File

@@ -125,11 +125,13 @@ public class SessionService {
loginDto = loginUserService.getFromCache(sessionId);
} catch (Exception e) {
log.warn("会话已过期 {}", sessionId, e);
log.warn("会话已过期 {}", sessionId);
logout(sessionId);
throw new BizException("invalidToken", "会话已过期,请重新登录");
}
HttpServletRequest request =
((ServletRequestAttributes) RequestContextHolder.getRequestAttributes())
.getRequest();

View File

@@ -411,6 +411,7 @@ public class UserService extends BaseService<UserEntity, UserRepository> {
Map<String, String> javaMap =
Stream.ofAll(names)
.filter(StringUtils::isNotBlank)
.toMap(x -> x.split(",")[0], x -> x.split(",")[1])
.toJavaMap();