From 088c94e723e93095a8bb0b6256ae927e2c699b40 Mon Sep 17 00:00:00 2001 From: KilLze Date: Tue, 13 Jan 2026 00:32:30 +0800 Subject: [PATCH] =?UTF-8?q?=E7=94=A8=E6=88=B7=E5=B0=81=E7=A6=81=E6=8B=A6?= =?UTF-8?q?=E6=88=AA=E5=99=A8=EF=BC=8C=E7=99=BB=E5=BD=95=E9=AA=8C=E8=AF=81?= =?UTF-8?q?=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../java/com/bao/dating/util/UserBanUtil.java | 43 +++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 src/main/java/com/bao/dating/util/UserBanUtil.java diff --git a/src/main/java/com/bao/dating/util/UserBanUtil.java b/src/main/java/com/bao/dating/util/UserBanUtil.java new file mode 100644 index 0000000..13a0745 --- /dev/null +++ b/src/main/java/com/bao/dating/util/UserBanUtil.java @@ -0,0 +1,43 @@ +package com.bao.dating.util; + +import com.bao.dating.context.UserContext; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; +import org.springframework.stereotype.Component; + +/** + * 用户封禁验证工具类 + * 提供统一的用户封禁状态检查功能 + * + * @author KilLze + */ +@Component +public class UserBanUtil { + + @Autowired + private RedisTemplate redisTemplate; + + /** + * 验证指定用户是否被封禁 + * + * @param userId 用户ID + * @throws RuntimeException 如果用户被封禁则抛出异常 + */ + public void validateUserNotBanned(Long userId) { + String banKey = "user:ban:" + userId; + if (Boolean.TRUE.equals(redisTemplate.hasKey(banKey))) { + String reason = (String) redisTemplate.opsForValue().get(banKey); + throw new RuntimeException("账号已被封禁:" + reason); + } + } + + /** + * 验证当前登录用户是否被封禁 + * + * @throws RuntimeException 如果用户被封禁则抛出异常 + */ + public void validateCurrentUserNotBanned() { + Long userId = UserContext.getUserId(); + validateUserNotBanned(userId); + } +} \ No newline at end of file