fix(login): 修复用户登出时Redis数据清理问题

- 注入RedissonClient用于操作Redis数据
- 在登出时从Redis中删除对应的用户信息
- 添加异常处理确保登出操作的稳定性
- 优化手机号验证逻辑增加格式校验
This commit is contained in:
wangzhiwei 2026-01-29 17:35:14 +08:00
parent 85441c1a6c
commit a2803359a6
2 changed files with 31 additions and 3 deletions

View File

@ -11,6 +11,7 @@ import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.redisson.api.RedissonClient;
/**
* (SysUser)表控制层
@ -28,6 +29,12 @@ public class LoginController {
*/
@Resource
private SysUserService sysUserService;
/**
* Redisson客户端
*/
@Resource
private RedissonClient redissonClient;
/**
* 用户登录
@ -50,9 +57,24 @@ public class LoginController {
@PostMapping("/accountLogout")
@Operation(summary = "用户登出", description = "用户登出")
public CommonResult<String> logout() {
// 使用Sa-Token登出
cn.dev33.satoken.stp.StpUtil.logout();
return CommonResult.success("登出成功");
try {
// 获取当前用户的token
String token = cn.dev33.satoken.stp.StpUtil.getTokenValue();
// 使用Sa-Token登出
cn.dev33.satoken.stp.StpUtil.logout();
// 从Redis中删除用户信息
if (token != null && !token.isEmpty()) {
redissonClient.getBucket("loginUser:" + token).delete();
}
return CommonResult.success("登出成功");
} catch (Exception e) {
// 如果获取token失败仍然执行登出操作
cn.dev33.satoken.stp.StpUtil.logout();
return CommonResult.success("登出成功");
}
}
/**

View File

@ -652,6 +652,12 @@ public class SysUserServiceImpl implements SysUserService {
// 参数校验
Assert.notBlank(phone, "手机号不能为空");
// 验证手机号格式
String phoneRegex = "^1[3-9\\d{9}$";
if (!phone.matches(phoneRegex)) {
Assert.isTrue(false, "手机号格式不正确");
}
// 生成6位随机验证码
String code = generateSmsCode();