修复bug054

This commit is contained in:
tsukiyalo 2024-11-11 08:31:19 +08:00
parent 53eb6b013e
commit 32e7432ba1
11 changed files with 81 additions and 23 deletions

View File

@ -43,6 +43,7 @@
<artifactId>alipay-sdk-java</artifactId> <artifactId>alipay-sdk-java</artifactId>
<version>4.38.0.ALL</version> <version>4.38.0.ALL</version>
</dependency> </dependency>
<!--hutool-->
<dependency> <dependency>
<groupId>cn.hutool</groupId> <groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId> <artifactId>hutool-all</artifactId>
@ -105,6 +106,22 @@
<artifactId>lombok</artifactId> <artifactId>lombok</artifactId>
<version>1.18.30</version> <version>1.18.30</version>
</dependency> </dependency>
<!-- redis -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-redis</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.session</groupId>
<artifactId>spring-session-data-redis</artifactId>
</dependency>
<!-- websocket-->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-websocket</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -235,3 +235,13 @@ create table if not exists systemInfo
content varchar(256) not null comment '功能内容', content varchar(256) not null comment '功能内容',
createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间' createTime datetime default CURRENT_TIMESTAMP not null comment '创建时间'
) comment '系统信息' collate = utf8mb4_unicode_ci; ) comment '系统信息' collate = utf8mb4_unicode_ci;
-- 聊天记录表
create table private_message
(
id bigint auto_increment primary key,
from_userAccount varchar(255) not null comment '发消息者',
to_userAccount varchar(255) not null comment '接收消息者',
orderId BIGINT null comment '订单id',
message varchar(255) null comment '消息内容'
)comment '聊天记录' collate = utf8mb4_unicode_ci;

View File

@ -12,6 +12,7 @@ public enum ErrorCode {
NOT_LOGIN(40100, "未登录", ""), NOT_LOGIN(40100, "未登录", ""),
NO_AUTH(40101, "无权限", ""), NO_AUTH(40101, "无权限", ""),
NOT_FOUND_ERROR(40400,"请求数据不存在",""), NOT_FOUND_ERROR(40400,"请求数据不存在",""),
FORBIDDEN_ERROR(40300, "禁止访问",""),
SYSTEM_ERROR(50000, "系统内部异常", ""), SYSTEM_ERROR(50000, "系统内部异常", ""),
OPERATION_ERROR(50001, "操作失败", ""); OPERATION_ERROR(50001, "操作失败", "");

View File

@ -5,11 +5,6 @@ package com.bsz.school_send_back_end.contant;
*/ */
public interface UserConstant { public interface UserConstant {
/**
* 用户登录态键
*/
String USER_LOGIN_STATE = "userLoginState";
/** /**
* 盐值 * 盐值
*/ */

View File

@ -16,7 +16,7 @@ import com.bsz.school_send_back_end.common.BaseResponse;
import com.bsz.school_send_back_end.common.ErrorCode; import com.bsz.school_send_back_end.common.ErrorCode;
import com.bsz.school_send_back_end.common.ResultUtils; import com.bsz.school_send_back_end.common.ResultUtils;
import com.bsz.school_send_back_end.config.AlipayClients; import com.bsz.school_send_back_end.config.AlipayClients;
import com.bsz.school_send_back_end.contant.UserConstant; import com.bsz.school_send_back_end.contant.RedisKeyConstant;
import com.bsz.school_send_back_end.mapper.UserMapper; import com.bsz.school_send_back_end.mapper.UserMapper;
import com.bsz.school_send_back_end.model.domain.User; import com.bsz.school_send_back_end.model.domain.User;
import com.bsz.school_send_back_end.utils.RandomNumberGenerator; import com.bsz.school_send_back_end.utils.RandomNumberGenerator;
@ -30,7 +30,6 @@ import org.springframework.web.bind.annotation.RestController;
import javax.annotation.Resource; import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.PrintWriter; import java.io.PrintWriter;
import java.text.SimpleDateFormat; import java.text.SimpleDateFormat;
import java.util.*; import java.util.*;
@ -102,13 +101,13 @@ public class AlipayController {
user.setUserPassword("123456"); user.setUserPassword("123456");
user.setAvatarUrl(StringUtils.isAnyBlank(response1.getAvatar()) ? "https://tfs.alipayobjects.com/images/partner/ATuihpR50zu7UAAAAAAAAAAAAADtl2AA" : response1.getAvatar()); user.setAvatarUrl(StringUtils.isAnyBlank(response1.getAvatar()) ? "https://tfs.alipayobjects.com/images/partner/ATuihpR50zu7UAAAAAAAAAAAAADtl2AA" : response1.getAvatar());
userMapper.insert(user); userMapper.insert(user);
req.getSession().setAttribute(UserConstant.USER_LOGIN_STATE,user); req.getSession().setAttribute(RedisKeyConstant.USER_LOGIN_STATE,user);
return ResultUtils.success(user,"注册成功"); return ResultUtils.success(user,"注册成功");
} }
req.getSession().setAttribute(UserConstant.USER_LOGIN_STATE,oid); req.getSession().setAttribute(RedisKeyConstant.USER_LOGIN_STATE,oid);
return ResultUtils.success(oid,"登录成功"); return ResultUtils.success(oid,"登录成功");
} }
req.getSession().setAttribute(UserConstant.USER_LOGIN_STATE,oid); req.getSession().setAttribute(RedisKeyConstant.USER_LOGIN_STATE,oid);
return ResultUtils.success(oid,"登录成功"); return ResultUtils.success(oid,"登录成功");
} }
@ -158,10 +157,10 @@ public class AlipayController {
user.setUserPassword("123456"); user.setUserPassword("123456");
user.setAvatarUrl(StringUtils.isAnyBlank(response1.getAvatar()) ? "https://tfs.alipayobjects.com/images/partner/ATuihpR50zu7UAAAAAAAAAAAAADtl2AA" : response1.getAvatar()); user.setAvatarUrl(StringUtils.isAnyBlank(response1.getAvatar()) ? "https://tfs.alipayobjects.com/images/partner/ATuihpR50zu7UAAAAAAAAAAAAADtl2AA" : response1.getAvatar());
userMapper.insert(user); userMapper.insert(user);
req.getSession().setAttribute(UserConstant.USER_LOGIN_STATE,oid); req.getSession().setAttribute(RedisKeyConstant.USER_LOGIN_STATE,oid);
return ResultUtils.success(user,"注册成功"); return ResultUtils.success(user,"注册成功");
} }
req.getSession().setAttribute(UserConstant.USER_LOGIN_STATE,oid); req.getSession().setAttribute(RedisKeyConstant.USER_LOGIN_STATE,oid);
return ResultUtils.success(oid,"登录成功"); return ResultUtils.success(oid,"登录成功");
} }
return ResultUtils.error(ErrorCode.PARAMS_ERROR); return ResultUtils.error(ErrorCode.PARAMS_ERROR);

View File

@ -32,7 +32,7 @@ public class FileController {
@Resource @Resource
private UserService userService; private UserService userService;
@PostMapping("/upload/server/not_login") @PostMapping("/upload/server/not_login")
public BaseResponse<String> uploadServerNotLogin (@RequestPart("file")MultipartFile multipartFile, UploadFileRequest uploadFileRequest) { public BaseResponse<String> uploadServerNotLogin (@RequestPart("file")MultipartFile multipartFile, @RequestBody UploadFileRequest uploadFileRequest) {
//获取业务名称 //获取业务名称
String biz = uploadFileRequest.getBiz(); String biz = uploadFileRequest.getBiz();
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz); FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);
@ -67,7 +67,7 @@ public class FileController {
} }
@PostMapping("/upload/server") @PostMapping("/upload/server")
public BaseResponse<String> uploadServerFile(@RequestPart("file") MultipartFile multipartFile, UploadFileRequest uploadFileRequest, HttpServletRequest request) { public BaseResponse<String> uploadServerFile(@RequestPart("file") MultipartFile multipartFile, @RequestBody UploadFileRequest uploadFileRequest, HttpServletRequest request) {
// 获取业务名称 // 获取业务名称
String biz = uploadFileRequest.getBiz(); String biz = uploadFileRequest.getBiz();
FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz); FileUploadBizEnum fileUploadBizEnum = FileUploadBizEnum.getEnumByValue(biz);

View File

@ -75,7 +75,7 @@ public class UserController {
* @return * @return
*/ */
@PostMapping("/login") @PostMapping("/login")
public BaseResponse<LoginUserVO> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) { public BaseResponse<String> userLogin(@RequestBody UserLoginRequest userLoginRequest, HttpServletRequest request) {
if (userLoginRequest == null) { if (userLoginRequest == null) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空"); throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
} }
@ -87,8 +87,8 @@ public class UserController {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空"); throw new BusinessException(ErrorCode.PARAMS_ERROR, "请求参数为空");
} }
LoginUserVO loginUserVO = userService.userLogin(userAccount, userPassword, request); String message = userService.userLogin(userAccount, userPassword, request);
return ResultUtils.success(loginUserVO); return ResultUtils.success(message);
} }
/* /*

View File

@ -1,8 +1,10 @@
package com.bsz.school_send_back_end.model.dto.file; package com.bsz.school_send_back_end.model.dto.file;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.Data; import lombok.Data;
import javax.validation.constraints.NotBlank;
import java.io.Serial; import java.io.Serial;
import java.io.Serializable; import java.io.Serializable;
@ -12,6 +14,8 @@ public class UploadFileRequest implements Serializable {
/** /**
* 业务 * 业务
*/ */
@NotBlank(message = "业务名称不能为空")
@Schema(description = "业务标识例如user_avatardishescardsystem", example = "user_avatar")
private String biz; private String biz;
@Serial @Serial

View File

@ -37,7 +37,7 @@ public interface UserService extends IService<User> {
* @return 脱敏后的用户信息 * @return 脱敏后的用户信息
*/ */
LoginUserVO userLogin(String userAccount, String userPassword, HttpServletRequest request); String userLogin(String userAccount, String userPassword, HttpServletRequest request);
/** /**

View File

@ -1,17 +1,21 @@
package com.bsz.school_send_back_end.service.impl; package com.bsz.school_send_back_end.service.impl;
import cn.hutool.core.util.RandomUtil; import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject; import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bsz.school_send_back_end.common.ErrorCode; import com.bsz.school_send_back_end.common.ErrorCode;
import com.bsz.school_send_back_end.exception.BusinessException; import com.bsz.school_send_back_end.exception.BusinessException;
import com.bsz.school_send_back_end.manager.SessionManager;
import com.bsz.school_send_back_end.mapper.UserMapper; import com.bsz.school_send_back_end.mapper.UserMapper;
import com.bsz.school_send_back_end.model.enums.UserRoleEnum;
import com.bsz.school_send_back_end.model.vo.LoginUserVO; import com.bsz.school_send_back_end.model.vo.LoginUserVO;
import com.bsz.school_send_back_end.model.domain.User; import com.bsz.school_send_back_end.model.domain.User;
import com.bsz.school_send_back_end.model.dto.user.UserSearchRequest; import com.bsz.school_send_back_end.model.dto.user.UserSearchRequest;
import com.bsz.school_send_back_end.model.dto.user.UserUpdatePasswordRequest; import com.bsz.school_send_back_end.model.dto.user.UserUpdatePasswordRequest;
import com.bsz.school_send_back_end.service.UserService; import com.bsz.school_send_back_end.service.UserService;
import com.bsz.school_send_back_end.utils.NetUtils;
import lombok.extern.slf4j.Slf4j; import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils; import org.springframework.beans.BeanUtils;
@ -25,7 +29,8 @@ import java.util.Date;
import java.util.regex.Matcher; import java.util.regex.Matcher;
import java.util.regex.Pattern; import java.util.regex.Pattern;
import static com.bsz.school_send_back_end.contant.UserConstant.USER_LOGIN_STATE; import static com.bsz.school_send_back_end.contant.RedisKeyConstant.USER_LOGIN_STATE;
/** /**
* @author bsz * @author bsz
@ -38,6 +43,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
@Resource @Resource
private UserMapper userMapper; private UserMapper userMapper;
@Resource
private SessionManager sessionManager;
/** /**
* 盐值 对密码进行混淆加密 * 盐值 对密码进行混淆加密
*/ */
@ -112,7 +120,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
*/ */
@Override @Override
public LoginUserVO userLogin(String userAccount, String userPassword, HttpServletRequest request) { public String userLogin(String userAccount, String userPassword, HttpServletRequest request) {
//非空 //非空
if (StringUtils.isAllBlank(userAccount, userPassword)) { if (StringUtils.isAllBlank(userAccount, userPassword)) {
throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空"); throw new BusinessException(ErrorCode.PARAMS_ERROR, "参数为空");
@ -147,8 +155,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
} }
//用户登陆成功 //用户登陆成功
request.getSession().setAttribute(USER_LOGIN_STATE, user); return sessionManager.login(user, request);
return this.getLoginUserVO(user);
} }
/** /**
@ -186,6 +193,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
*/ */
@Override @Override
public int userLogout(HttpServletRequest request) { public int userLogout(HttpServletRequest request) {
if (request.getSession().getAttribute(USER_LOGIN_STATE) == null) {
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR);
}
sessionManager.logout(request);
//移除登录态
request.getSession().removeAttribute(USER_LOGIN_STATE); request.getSession().removeAttribute(USER_LOGIN_STATE);
return 1; return 1;
} }
@ -206,11 +218,22 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, User>
throw new BusinessException(ErrorCode.NOT_LOGIN, "未登录"); throw new BusinessException(ErrorCode.NOT_LOGIN, "未登录");
} }
//从数据库中获取该用户的信息 //从数据库中获取该用户的信息
String ipAddress = NetUtils.getIpAddress(request);
String oldSessionId = sessionManager.checkOtherLogin(currentUser.getId(), ipAddress, request);
if (StrUtil.isNotBlank(oldSessionId)) {
request.getSession().removeAttribute(USER_LOGIN_STATE);
throw new BusinessException(ErrorCode.NO_AUTH, "已在其他设备登录,请重新登录");
}
long userId = currentUser.getId(); long userId = currentUser.getId();
currentUser = this.getById(userId); currentUser = this.getById(userId);
if (currentUser == null) { if (currentUser == null) {
throw new BusinessException(ErrorCode.NOT_LOGIN, "未登录"); throw new BusinessException(ErrorCode.NOT_LOGIN, "未登录");
} }
//被封号
if (UserRoleEnum.BAN.getValue().equals(currentUser.getUserRole())) {
throw new BusinessException(ErrorCode.NOT_FOUND_ERROR,"用户已被封禁,禁止登录");
}
return currentUser; return currentUser;
} }

View File

@ -21,8 +21,17 @@ spring:
mvc: mvc:
pathmatch: pathmatch:
matching-strategy: ant_path_matcher matching-strategy: ant_path_matcher
redis:
host: 39.101.78.35
port: 6379
database: 3
password: 12345678
timeout: 60000
profiles: profiles:
active: dev active: dev
session:
store-type: redis
timeout: 2592000
knife4j: knife4j:
enable: true enable: true
server: server: