mirror of
https://codeup.aliyun.com/64f7d6b8ce01efaafef1e678/coal/coal.git
synced 2026-01-25 07:46:40 +08:00
bugfix
This commit is contained in:
@@ -21,6 +21,10 @@ import com.google.common.collect.Maps;
|
||||
|
||||
import jakarta.annotation.PostConstruct;
|
||||
import jakarta.annotation.PreDestroy;
|
||||
import jakarta.persistence.criteria.CriteriaBuilder;
|
||||
import jakarta.persistence.criteria.CriteriaQuery;
|
||||
import jakarta.persistence.criteria.Predicate;
|
||||
import jakarta.persistence.criteria.Root;
|
||||
|
||||
import lombok.SneakyThrows;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
@@ -35,6 +39,7 @@ import org.springframework.core.convert.ConversionService;
|
||||
import org.springframework.data.domain.Page;
|
||||
import org.springframework.data.domain.PageRequest;
|
||||
import org.springframework.data.domain.Sort;
|
||||
import org.springframework.data.jpa.domain.Specification;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@@ -245,8 +250,21 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
|
||||
try {
|
||||
LoginUserEntity loginUser = this.get(sessionId);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
||||
|
||||
} catch (Exception e) {
|
||||
log.warn("删除会话失败 {}",sessionId, e);
|
||||
}
|
||||
@@ -254,6 +272,18 @@ public class LoginUserService extends BaseService<LoginUserEntity, LoginUserRepo
|
||||
this.clearCache(sessionId);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}));
|
||||
|
||||
}
|
||||
|
||||
public void logout(IdRequest request) {
|
||||
String sessionId = request.getId();
|
||||
if (sessionId != null) {
|
||||
|
||||
@@ -251,15 +251,15 @@ public class RateLimiterService {
|
||||
RateIntervalUnit.DAYS);
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void destroyRL(String sessionId, String userId) {
|
||||
|
||||
if (StringUtils.isEmpty(sessionId)) {
|
||||
throw new RuntimeException("sessionId is empty");
|
||||
}
|
||||
|
||||
if (StringUtils.isEmpty(userId)) {
|
||||
throw new RuntimeException("userId is empty");
|
||||
}
|
||||
|
||||
|
||||
log.info("删除会话限流 session: {} user: {}", sessionId, userId);
|
||||
|
||||
@@ -290,6 +290,14 @@ public class RateLimiterService {
|
||||
} catch (Exception e) {
|
||||
log.warn("", e);
|
||||
}
|
||||
|
||||
// 如果当前用户没有其他会话, 则删除用户限流
|
||||
|
||||
|
||||
if (StringUtils.isEmpty(userId)) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
RRateLimiter userMinRL =
|
||||
|
||||
Reference in New Issue
Block a user