用户封禁拦截器,登录验证完成

This commit is contained in:
KilLze
2026-01-13 00:32:17 +08:00
parent 4f94c43f94
commit a3d0d7423c
5 changed files with 64 additions and 4 deletions

View File

@@ -0,0 +1,4 @@
package com.bao.dating.controller;
public class AdminController {
}

View File

@@ -70,6 +70,18 @@ public class TokenInterceptor implements HandlerInterceptor {
// 解析 token // 解析 token
Long userId = Long.valueOf(JwtUtil.getSubjectFromToken(token)); Long userId = Long.valueOf(JwtUtil.getSubjectFromToken(token));
// 检查用户是否被封禁
String banKey = "user:ban:" + userId;
if (Boolean.TRUE.equals(redisTemplate.hasKey(banKey))) {
String reason = String.valueOf(redisTemplate.opsForValue().get(banKey));
log.error("用户 {} 已被封禁:{}", userId, reason);
response.setStatus(403);
response.setContentType("application/json;charset=UTF-8");
response.getWriter().write("账号已被封禁:" + reason);
return false;
}
// 从Redis获取存储的token进行比对 // 从Redis获取存储的token进行比对
Object redisTokenObj = redisTemplate.opsForValue().get("login:token:" + userId); Object redisTokenObj = redisTemplate.opsForValue().get("login:token:" + userId);
String redisToken = redisTokenObj != null ? redisTokenObj.toString() : null; String redisToken = redisTokenObj != null ? redisTokenObj.toString() : null;

View File

@@ -73,6 +73,14 @@ public class WsAuthInterceptor implements HandshakeInterceptor {
Long userId = Long.valueOf(userIdStr); Long userId = Long.valueOf(userIdStr);
// 检查用户是否被封禁
String banKey = "user:ban:" + userId;
if (Boolean.TRUE.equals(redisTemplate.hasKey(banKey))) {
String reason = String.valueOf(redisTemplate.opsForValue().get(banKey));
log.error("WebSocket拒绝用户 {} 被封禁,原因:{}", userId, reason);
return false;
}
// 从Redis获取存储的token进行比对 // 从Redis获取存储的token进行比对
String redisTokenKey = "login:token:" + userId; String redisTokenKey = "login:token:" + userId;
Object redisTokenObj = redisTemplate.opsForValue().get(redisTokenKey); Object redisTokenObj = redisTemplate.opsForValue().get(redisTokenKey);

View File

@@ -0,0 +1,30 @@
package com.bao.dating.pojo.entity;
import lombok.Data;
import java.time.LocalDateTime;
/**
* 用户封禁记录
* @author KilLze
*/
@Data
public class UserBan {
private Long id;
private Long userId;
private String reason;
private LocalDateTime banStartTime;
private LocalDateTime banEndTime;
/**
* 1:封禁中 0:已解封
*/
private Integer status;
private LocalDateTime createTime;
}

View File

@@ -20,6 +20,7 @@ import com.bao.dating.util.CodeUtil;
import com.bao.dating.util.FileUtil; import com.bao.dating.util.FileUtil;
import com.bao.dating.util.JwtUtil; import com.bao.dating.util.JwtUtil;
import com.bao.dating.util.MD5Util; import com.bao.dating.util.MD5Util;
import com.bao.dating.util.UserBanUtil;
import io.jsonwebtoken.Claims; import io.jsonwebtoken.Claims;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
@@ -66,6 +67,9 @@ public class UserServiceImpl implements UserService {
@Autowired @Autowired
private VerificationCodeService verificationCodeService; private VerificationCodeService verificationCodeService;
@Autowired
private UserBanUtil userBanValidator;
/** /**
* 用户登录 * 用户登录
* *
@@ -92,6 +96,8 @@ public class UserServiceImpl implements UserService {
if (!match) { if (!match) {
throw new RuntimeException("密码错误"); throw new RuntimeException("密码错误");
} }
// 用户封禁验证
userBanValidator.validateUserNotBanned(user.getUserId());
// 生成token // 生成token
String token = JwtUtil.generateToken(String.valueOf(user.getUserId())); String token = JwtUtil.generateToken(String.valueOf(user.getUserId()));