用户封禁拦截器,登录验证完成
This commit is contained in:
@@ -0,0 +1,4 @@
|
||||
package com.bao.dating.controller;
|
||||
|
||||
public class AdminController {
|
||||
}
|
||||
@@ -70,6 +70,18 @@ public class TokenInterceptor implements HandlerInterceptor {
|
||||
// 解析 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进行比对
|
||||
Object redisTokenObj = redisTemplate.opsForValue().get("login:token:" + userId);
|
||||
String redisToken = redisTokenObj != null ? redisTokenObj.toString() : null;
|
||||
|
||||
@@ -73,6 +73,14 @@ public class WsAuthInterceptor implements HandshakeInterceptor {
|
||||
|
||||
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进行比对
|
||||
String redisTokenKey = "login:token:" + userId;
|
||||
Object redisTokenObj = redisTemplate.opsForValue().get(redisTokenKey);
|
||||
|
||||
30
src/main/java/com/bao/dating/pojo/entity/UserBan.java
Normal file
30
src/main/java/com/bao/dating/pojo/entity/UserBan.java
Normal 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;
|
||||
}
|
||||
@@ -20,6 +20,7 @@ import com.bao.dating.util.CodeUtil;
|
||||
import com.bao.dating.util.FileUtil;
|
||||
import com.bao.dating.util.JwtUtil;
|
||||
import com.bao.dating.util.MD5Util;
|
||||
import com.bao.dating.util.UserBanUtil;
|
||||
import io.jsonwebtoken.Claims;
|
||||
import org.springframework.beans.BeanUtils;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -66,6 +67,9 @@ public class UserServiceImpl implements UserService {
|
||||
@Autowired
|
||||
private VerificationCodeService verificationCodeService;
|
||||
|
||||
@Autowired
|
||||
private UserBanUtil userBanValidator;
|
||||
|
||||
/**
|
||||
* 用户登录
|
||||
*
|
||||
@@ -92,6 +96,8 @@ public class UserServiceImpl implements UserService {
|
||||
if (!match) {
|
||||
throw new RuntimeException("密码错误");
|
||||
}
|
||||
// 用户封禁验证
|
||||
userBanValidator.validateUserNotBanned(user.getUserId());
|
||||
// 生成token
|
||||
String token = JwtUtil.generateToken(String.valueOf(user.getUserId()));
|
||||
|
||||
|
||||
Reference in New Issue
Block a user