10 Commits

Author SHA1 Message Date
KilLze
39af4e6596 修该死的bug 2026-01-14 11:44:36 +08:00
KilLze
ea5c95584d 优化会话列表,添加用户在线状态 2026-01-14 11:31:10 +08:00
KilLze
e170016019 完成用户在线状态查询 2026-01-14 11:21:06 +08:00
KilLze
6d95b8d391 完成数据库定时解封低频同步(凌晨3点),主要还是依赖redis实时同步 2026-01-13 01:34:05 +08:00
KilLze
1c2a4edae9 完成用户封禁,解封,封禁查询(由于没有管理员,所以现在先用普通用户测试封禁) 2026-01-13 01:29:34 +08:00
KilLze
088c94e723 用户封禁拦截器,登录验证完成 2026-01-13 00:32:30 +08:00
KilLze
a3d0d7423c 用户封禁拦截器,登录验证完成 2026-01-13 00:32:17 +08:00
KilLze
4f94c43f94 修bug 2026-01-11 00:57:29 +08:00
KilLze
7a8a03510c 修bug 2026-01-11 00:28:23 +08:00
KilLze
2189575659 修bug 2026-01-11 00:25:19 +08:00
19 changed files with 470 additions and 133 deletions

View File

@@ -0,0 +1,51 @@
package com.bao.dating.controller;
import com.bao.dating.common.Result;
import com.bao.dating.common.ResultCode;
import com.bao.dating.pojo.dto.UserBanDTO;
import com.bao.dating.pojo.entity.UserBan;
import com.bao.dating.service.UserBanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
* 管理员控制器
* @author lenovo
*/
@RestController
@RequestMapping("/admin")
public class AdminController {
@Autowired
private UserBanService userBanService;
/**
* 封禁用户
*/
@PostMapping("/{userId}/ban")
public Result<?> banUser(@PathVariable Long userId,
@RequestBody UserBanDTO userBanDTO) {
userBanDTO.setUserId(userId);
userBanService.banUser(userBanDTO);
return Result.success(ResultCode.SUCCESS, "封禁成功");
}
/**
* 解封用户
*/
@PostMapping("/{userId}/unban")
public Result<?> unbanUser(@PathVariable Long userId) {
userBanService.unbanUser(userId);
return Result.success(ResultCode.SUCCESS, "解封成功");
}
/**
* 查询封禁状态
*/
@GetMapping("/{userId}/banInfo")
public Result<UserBan> banInfo(@PathVariable Long userId) {
UserBan ban = userBanService.getActiveBan(userId);
return Result.success(ResultCode.SUCCESS, "查询成功", ban);
}
}

View File

@@ -4,9 +4,8 @@ import com.bao.dating.anno.Log;
import com.bao.dating.common.Result; import com.bao.dating.common.Result;
import com.bao.dating.common.ResultCode; import com.bao.dating.common.ResultCode;
import com.bao.dating.context.UserContext; import com.bao.dating.context.UserContext;
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
import com.bao.dating.pojo.dto.UserLoginDTO;
import com.bao.dating.pojo.dto.UserInfoDTO; import com.bao.dating.pojo.dto.UserInfoDTO;
import com.bao.dating.pojo.dto.UserLoginDTO;
import com.bao.dating.pojo.vo.UserInfoVO; import com.bao.dating.pojo.vo.UserInfoVO;
import com.bao.dating.pojo.vo.UserLoginVO; import com.bao.dating.pojo.vo.UserLoginVO;
import com.bao.dating.service.UserService; import com.bao.dating.service.UserService;
@@ -93,7 +92,7 @@ public class UserController {
*/ */
@Log @Log
@PostMapping("/info/update") @PostMapping("/info/update")
public Result<UserInfoVO> userInfoUpdate(@RequestBody UserInfoUpdateDTO userInfoUpdateDTO) { public Result<UserInfoVO> userInfoUpdate(@RequestBody UserInfoDTO userInfoUpdateDTO) {
Long userId = UserContext.getUserId(); Long userId = UserContext.getUserId();
userInfoUpdateDTO.setUserId(userId); userInfoUpdateDTO.setUserId(userId);
UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO); UserInfoVO userInfoVO =userService.updateUserInfo(userInfoUpdateDTO);
@@ -204,4 +203,15 @@ public class UserController {
return Result.success(ResultCode.SUCCESS,"用户登录成功",userLoginVO); return Result.success(ResultCode.SUCCESS,"用户登录成功",userLoginVO);
} }
/**
* 判断用户是否在线
* @param userId 用户ID
* @return 用户是否在线
*/
@GetMapping("/{userId}/online")
public Result<Boolean> isUserOnline(@PathVariable Long userId) {
boolean online = userService.isUserOnline(userId);
return Result.success(ResultCode.SUCCESS, "查询成功", online);
}
} }

View File

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

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,43 @@
package com.bao.dating.mapper;
import com.bao.dating.pojo.entity.UserBan;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
@Mapper
public interface UserBanMapper {
/**
* 新增封禁记录
* @param userBan 封禁记录
* @return 影响行数
*/
int insertBan(UserBan userBan);
/**
* 查询是否存在生效中的封禁
* @param userId 用户ID
* @return 存在返回1不存在返回0
*/
int existsActiveBan(@Param("userId") Long userId);
/**
* 查询生效中的封禁记录
* @param userId 用户ID
* @return 封禁记录
*/
UserBan selectActiveBan(@Param("userId") Long userId);
/**
* 解封用户
* @param userId 用户ID
* @return 影响行数
*/
int unbanUser(@Param("userId") Long userId);
/**
* 定时任务:过期自动解封
* @return 影响行数
*/
int updateExpiredBans();
}

View File

@@ -0,0 +1,13 @@
package com.bao.dating.pojo.dto;
import lombok.Data;
/**
* 用户封禁数据传输对象
* @author KilLze
*/
@Data
public class UserBanDTO {
private Long userId;
private String reason;
private Integer banDays;
}

View File

@@ -1,25 +0,0 @@
package com.bao.dating.pojo.dto;
import lombok.Data;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
/**
* 用户信息更新数据传输对象
* @author KilLze
*/
@Data
public class UserInfoUpdateDTO {
private Long userId;
private String userName;
private String nickname;
private String avatarUrl;
private String backgroundUrl;
private Integer gender;
private LocalDate birthday;
private List<String> hobbies;
private String signature;
private LocalDateTime updatedAt;
}

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

@@ -29,4 +29,6 @@ public class ChatSessionsVO {
private Integer topStatus; private Integer topStatus;
/** 免打扰状态 */ /** 免打扰状态 */
private Integer muteStatus; private Integer muteStatus;
/** 会话状态 */
private Boolean online;
} }

View File

@@ -26,4 +26,5 @@ public class UserInfoVO implements Serializable {
private LocalDateTime createdAt; private LocalDateTime createdAt;
private Double latitude; private Double latitude;
private Double longitude; private Double longitude;
private Boolean online;
} }

View File

@@ -0,0 +1,31 @@
package com.bao.dating.service;
import com.bao.dating.pojo.dto.UserBanDTO;
import com.bao.dating.pojo.entity.UserBan;
/**
* 用户封禁服务接口
* @author KilLze
*/
public interface UserBanService {
/**
* 封禁用户
* @param userBanDTO 用户封禁信息
*
*/
void banUser(UserBanDTO userBanDTO);
/**
* 解封用户
* @param userId 用户ID
*/
void unbanUser(Long userId);
/**
* 查询封禁信息
* @param userId 用户ID
* @return 封禁信息
*/
UserBan getActiveBan(Long userId);
}

View File

@@ -1,9 +1,7 @@
package com.bao.dating.service; package com.bao.dating.service;
import com.bao.dating.pojo.dto.UserInfoDTO; import com.bao.dating.pojo.dto.UserInfoDTO;
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
import com.bao.dating.pojo.dto.UserLoginDTO; import com.bao.dating.pojo.dto.UserLoginDTO;
import com.bao.dating.pojo.entity.User;
import com.bao.dating.pojo.vo.UserInfoVO; import com.bao.dating.pojo.vo.UserInfoVO;
import com.bao.dating.pojo.vo.UserLoginVO; import com.bao.dating.pojo.vo.UserLoginVO;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@@ -79,7 +77,6 @@ public interface UserService {
* @return 用户 * @return 用户
*/ */
UserInfoDTO getUserInfoById(Long userId); UserInfoDTO getUserInfoById(Long userId);
UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO);
void sendSmsCode(String phone); void sendSmsCode(String phone);
@@ -95,4 +92,11 @@ public interface UserService {
* @return 用户列表 * @return 用户列表
*/ */
List<UserInfoVO> findNearbyUsers(double lat,double lng,double radiusKm); List<UserInfoVO> findNearbyUsers(double lat,double lng,double radiusKm);
/**
* 判断用户是否在线
* @param userId 用户ID
* @return 是否在线
*/
boolean isUserOnline(Long userId);
} }

View File

@@ -236,6 +236,7 @@ public class ChatServiceImpl implements ChatService {
vo.setSessionName("用户" + session.getTargetUserId()); vo.setSessionName("用户" + session.getTargetUserId());
vo.setAvatarUrl(null); vo.setAvatarUrl(null);
} }
vo.setOnline(userService.isUserOnline(vo.getTargetUserId()));
return vo; return vo;
}).collect(Collectors.toList()); }).collect(Collectors.toList());
} }

View File

@@ -0,0 +1,74 @@
package com.bao.dating.service.impl;
import com.bao.dating.mapper.UserBanMapper;
import com.bao.dating.pojo.dto.UserBanDTO;
import com.bao.dating.pojo.entity.UserBan;
import com.bao.dating.service.UserBanService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import java.time.LocalDateTime;
import java.util.concurrent.TimeUnit;
@Service
public class UserBanServiceImpl implements UserBanService {
@Autowired
private UserBanMapper userBanMapper;
@Autowired
private RedisTemplate<String, Object> redisTemplate;
@Override
public void banUser(UserBanDTO userBanDTO) {
// 已被封禁,直接拒绝
if (userBanMapper.existsActiveBan(userBanDTO.getUserId()) > 0) {
throw new RuntimeException("用户已处于封禁状态");
}
LocalDateTime now = LocalDateTime.now();
LocalDateTime endTime = userBanDTO.getBanDays() == null ? null : now.plusDays(userBanDTO.getBanDays());
// 1. 写数据库
UserBan ban = new UserBan();
ban.setUserId(userBanDTO.getUserId());
ban.setReason(userBanDTO.getReason());
ban.setBanStartTime(now);
ban.setBanEndTime(endTime);
ban.setStatus(1);
userBanMapper.insertBan(ban);
// 2. 写 Redis
String key = "user:ban:" + userBanDTO.getUserId();
if (userBanDTO.getBanDays() == null) {
redisTemplate.opsForValue().set(key, userBanDTO.getReason());
} else {
redisTemplate.opsForValue().set(key, userBanDTO.getReason(), userBanDTO.getBanDays(), TimeUnit.DAYS);
}
// 3. 踢下线
redisTemplate.delete("login:token:" + userBanDTO.getUserId());
}
/**
* 解封用户
*/
@Override
public void unbanUser(Long userId) {
// 更新数据库
userBanMapper.unbanUser(userId);
// 删除 Redis
redisTemplate.delete("user:ban:" + userId);
}
/**
* 获取用户封禁信息
*/
@Override
public UserBan getActiveBan(Long userId) {
return userBanMapper.selectActiveBan(userId);
}
}

View File

@@ -1,18 +1,14 @@
package com.bao.dating.service.impl; package com.bao.dating.service.impl;
import com.bao.dating.common.Result;
import com.bao.dating.common.ResultCode;
import com.bao.dating.common.aliyun.AliOssUtil; import com.bao.dating.common.aliyun.AliOssUtil;
import com.bao.dating.common.aliyun.GreenImageScan; import com.bao.dating.common.aliyun.GreenImageScan;
import com.bao.dating.common.aliyun.GreenTextScan; import com.bao.dating.common.aliyun.GreenTextScan;
import com.bao.dating.common.aliyun.SmsUtil; import com.bao.dating.common.aliyun.SmsUtil;
import com.bao.dating.common.result.AliOssResult; import com.bao.dating.common.result.AliOssResult;
import com.bao.dating.common.result.GreenAuditResult; import com.bao.dating.common.result.GreenAuditResult;
import com.bao.dating.config.RedisConfig;
import com.bao.dating.context.UserContext; import com.bao.dating.context.UserContext;
import com.bao.dating.mapper.UserMapper; import com.bao.dating.mapper.UserMapper;
import com.bao.dating.pojo.dto.UserInfoDTO; import com.bao.dating.pojo.dto.UserInfoDTO;
import com.bao.dating.pojo.dto.UserInfoUpdateDTO;
import com.bao.dating.pojo.dto.UserLoginDTO; import com.bao.dating.pojo.dto.UserLoginDTO;
import com.bao.dating.pojo.entity.User; import com.bao.dating.pojo.entity.User;
import com.bao.dating.pojo.vo.UserInfoVO; import com.bao.dating.pojo.vo.UserInfoVO;
@@ -24,10 +20,12 @@ 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;
import org.springframework.data.redis.core.RedisTemplate; import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartFile;
@@ -69,6 +67,9 @@ public class UserServiceImpl implements UserService {
@Autowired @Autowired
private VerificationCodeService verificationCodeService; private VerificationCodeService verificationCodeService;
@Autowired
private UserBanUtil userBanValidator;
/** /**
* 用户登录 * 用户登录
* *
@@ -95,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()));
@@ -121,6 +124,9 @@ public class UserServiceImpl implements UserService {
@Override @Override
public void logout(String token) { public void logout(String token) {
Claims claims = JwtUtil.getClaimsFromToken(token); Claims claims = JwtUtil.getClaimsFromToken(token);
// 获取token信息
String subject = claims.getSubject();
// 获取token的过期时间
Date expiration = claims.getExpiration(); Date expiration = claims.getExpiration();
// 判断 token 是否已过期 // 判断 token 是否已过期
long ttl = expiration.getTime() - System.currentTimeMillis(); long ttl = expiration.getTime() - System.currentTimeMillis();
@@ -129,6 +135,10 @@ public class UserServiceImpl implements UserService {
return; return;
} }
// 从Redis中删除登录token记录
String loginTokenKey = "login:token:" + subject;
redisTemplate.delete(loginTokenKey);
String logoutKey = "jwt:blacklist:" + token; String logoutKey = "jwt:blacklist:" + token;
redisTemplate.opsForValue().set( redisTemplate.opsForValue().set(
logoutKey, logoutKey,
@@ -243,100 +253,13 @@ public class UserServiceImpl implements UserService {
} }
/**
* 更新用户信息
* @param userInfoUpdateDTO
* @return
*/
@Override
public UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO) {
Long userId = userInfoUpdateDTO.getUserId();
User user = userMapper.selectByUserId(userId);
if (user == null) {
throw new RuntimeException("用户不存在");
}
// 将需要审核的内容合并成一个文本,用于减少调用次数
StringBuilder textBuilder = new StringBuilder();
if (userInfoUpdateDTO.getNickname() != null && !userInfoUpdateDTO.getNickname().isEmpty()) {
textBuilder.append(userInfoUpdateDTO.getNickname()).append(" ");
}
if (userInfoUpdateDTO.getHobbies() != null && !userInfoUpdateDTO.getHobbies().isEmpty()) {
// 将爱好列表转换为字符串,用空格分隔
String hobbiesStr = String.join(" ", userInfoUpdateDTO.getHobbies());
textBuilder.append(hobbiesStr).append(" ");
}
if (userInfoUpdateDTO.getSignature() != null && !userInfoUpdateDTO.getSignature().isEmpty()) {
textBuilder.append(userInfoUpdateDTO.getSignature()).append(" ");
}
// 文本审核
if (textBuilder.length() > 0) {
Map textResult;
try {
textResult = greenTextScan.greeTextScan(textBuilder.toString());
} catch (Exception e) {
throw new RuntimeException("用户信息文本审核失败");
}
String suggestion = (String) textResult.get("suggestion");
if (GreenAuditResult.BLOCK.equals(suggestion)) {
throw new RuntimeException("用户信息包含违规内容,修改失败");
}
if (GreenAuditResult.REVIEW.equals(suggestion)) {
throw new RuntimeException("用户信息需要人工审核,暂无法修改");
}
}
// 图片审核
List<String> imageKeys = new ArrayList<>();
if (userInfoUpdateDTO.getAvatarUrl() != null && !userInfoUpdateDTO.getAvatarUrl().isEmpty()) {
imageKeys.add(userInfoUpdateDTO.getAvatarUrl());
}
if (userInfoUpdateDTO.getBackgroundUrl() != null && !userInfoUpdateDTO.getBackgroundUrl().isEmpty()) {
imageKeys.add(userInfoUpdateDTO.getBackgroundUrl());
}
if (!imageKeys.isEmpty()) {
Map imageResult;
try {
imageResult = greenImageScan.imageScan(imageKeys);
} catch (Exception e) {
throw new RuntimeException("用户图片审核失败");
}
String suggestion = (String) imageResult.get("suggestion");
if (GreenAuditResult.BLOCK.equals(suggestion)) {
throw new RuntimeException("头像或背景图不合规,修改失败");
}
if (GreenAuditResult.REVIEW.equals(suggestion)) {
throw new RuntimeException("头像或背景图需要人工审核,暂无法修改");
}
}
// 默认昵称兜底
if (userInfoUpdateDTO.getNickname() == null || userInfoUpdateDTO.getNickname().trim().isEmpty()) {
userInfoUpdateDTO.setNickname(user.getUserName());
}
userInfoUpdateDTO.setUpdatedAt(LocalDateTime.now());
// 更新数据库
userMapper.updateUserInfoByUserId(userInfoUpdateDTO);
// 封装返回结果
User updatedUser = userMapper.selectByUserId(userInfoUpdateDTO.getUserId());
UserInfoVO userInfoVO = new UserInfoVO();
BeanUtils.copyProperties(updatedUser, userInfoVO);
return userInfoVO;
}
/** /**
* 更新用户信息 * 更新用户信息
* *
* @param userInfoUpdateDTO 用户信息更新参数 * @param userInfoUpdateDTO 用户信息更新参数
*/ */
@Override @Override
public UserInfoVO updateUserInfo(UserInfoUpdateDTO userInfoUpdateDTO) { public UserInfoVO updateUserInfo(UserInfoDTO userInfoUpdateDTO) {
Long userId = userInfoUpdateDTO.getUserId(); Long userId = userInfoUpdateDTO.getUserId();
User user = userMapper.selectByUserId(userId); User user = userMapper.selectByUserId(userId);
if (user == null) { if (user == null) {
@@ -417,6 +340,7 @@ public class UserServiceImpl implements UserService {
return userInfoVO; return userInfoVO;
} }
/** /**
* 用户注册 * 用户注册
* @param userName 用户名称 * @param userName 用户名称
@@ -459,11 +383,13 @@ public class UserServiceImpl implements UserService {
@Override @Override
public UserLoginVO emailLogin(String email, String code) { public UserLoginVO emailLogin(String email, String code) {
User user = userMapper.selectByUserEmailUser(email); User user = userMapper.selectByUserEmailUser(email);
if (user == null) if (user == null) {
return null; return null;
}
boolean flag = verificationCodeService.verifyEmailCode(email, code); boolean flag = verificationCodeService.verifyEmailCode(email, code);
if (!flag) if (!flag) {
return null; return null;
}
// 生成token // 生成token
String token = JwtUtil.generateToken(String.valueOf(user.getUserId())); String token = JwtUtil.generateToken(String.valueOf(user.getUserId()));
UserLoginVO userLoginVO = new UserLoginVO(); UserLoginVO userLoginVO = new UserLoginVO();
@@ -572,4 +498,23 @@ public class UserServiceImpl implements UserService {
} }
return result; return result;
} }
@Override
public boolean isUserOnline(Long userId) {
if (userId == null) {
return false;
}
// 1. 是否被封禁
String banKey = "user:ban:" + userId;
if (Boolean.TRUE.equals(redisTemplate.hasKey(banKey))) {
return false;
}
// 2. 是否存在登录 token
String tokenKey = "login:token:" + userId;
Boolean online = redisTemplate.hasKey(tokenKey);
return Boolean.TRUE.equals(online);
}
} }

View File

@@ -0,0 +1,26 @@
package com.bao.dating.task;
import com.bao.dating.mapper.UserBanMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.EnableScheduling;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
@Slf4j
@Component
@EnableScheduling
public class UserBanScheduleTask {
@Autowired
private UserBanMapper userBanMapper;
/**
* 每天凌晨 3 点同步过期封禁
*/
@Scheduled(cron = "0 0 3 * * ?")
public void syncExpiredUserBan() {
int rows = userBanMapper.updateExpiredBans();
log.info("封禁同步任务执行完成,解封 {} 个用户", rows);
}
}

View File

@@ -0,0 +1,65 @@
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;
import java.util.concurrent.TimeUnit;
/**
* 用户封禁验证工具类
* 提供统一的用户封禁状态检查功能
*
* @author KilLze
*/
@Component
public class UserBanUtil {
@Autowired
private RedisTemplate<String, Object> 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);
// 获取剩余过期时间(秒)
Long ttlSeconds = redisTemplate.getExpire(banKey, TimeUnit.SECONDS);
String remainingTime = "";
if (ttlSeconds != null && ttlSeconds > 0) {
long days = ttlSeconds / (24 * 3600);
long hours = (ttlSeconds % (24 * 3600)) / 3600;
long minutes = (ttlSeconds % 3600) / 60;
if (days > 0) {
remainingTime = ",剩余时间:" + days + "" + hours + "小时";
} else if (hours > 0) {
remainingTime = ",剩余时间:" + hours + "小时" + minutes + "分钟";
} else {
remainingTime = ",剩余时间:" + minutes + "分钟";
}
} else {
remainingTime = ",永久封禁";
}
throw new RuntimeException("账号已被封禁,原因:" + reason + remainingTime);
}
}
/**
* 验证当前登录用户是否被封禁
*
* @throws RuntimeException 如果用户被封禁则抛出异常
*/
public void validateCurrentUserNotBanned() {
Long userId = UserContext.getUserId();
validateUserNotBanned(userId);
}
}

View File

@@ -0,0 +1,50 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.bao.dating.mapper.UserBanMapper">
<!-- 向数据库中添加用户封禁信息 -->
<insert id="insertBan" useGeneratedKeys="true" keyProperty="id">
INSERT INTO user_ban
(user_id, reason, ban_start_time, ban_end_time, status)
VALUES
(#{userId}, #{reason}, #{banStartTime}, #{banEndTime}, #{status})
</insert>
<!-- 查询指定用户是否存在未过期的封禁信息 -->
<select id="existsActiveBan" resultType="int">
SELECT COUNT(1)
FROM user_ban
WHERE user_id = #{userId}
AND status = 1
LIMIT 1
</select>
<!-- 查询指定用户是否存在未过期的封禁信息 -->
<select id="selectActiveBan" resultType="com.bao.dating.pojo.entity.UserBan">
SELECT *
FROM user_ban
WHERE user_id = #{userId}
AND status = 1
LIMIT 1
</select>
<!-- 解封指定用户 -->
<update id="unbanUser">
UPDATE user_ban
SET status = 0
WHERE user_id = #{userId}
AND status = 1
</update>
<!-- 批量更新已过期的封禁信息 -->
<update id="updateExpiredBans">
UPDATE user_ban
SET status = 0
WHERE status = 1
AND ban_end_time IS NOT NULL
AND ban_end_time &lt; NOW()
</update>
</mapper>