mirror of
https://gitee.com/bookshelfplus/bookshelfplus
synced 2025-10-11 02:05:15 +08:00
添加管理添加书籍前校验用户token是否有效,同时修改了获取用户信息的部分代码(从Controller层挪到Service层)(还未测试)
This commit is contained in:
@@ -1,7 +1,12 @@
|
||||
package plus.bookshelf.Service.Impl;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.data.redis.core.RedisTemplate;
|
||||
import org.springframework.stereotype.Service;
|
||||
import plus.bookshelf.Common.Error.BusinessErrorCode;
|
||||
import plus.bookshelf.Common.Error.BusinessException;
|
||||
import plus.bookshelf.Common.SessionManager.RedisSessionManager;
|
||||
import plus.bookshelf.Controller.VO.UserVO;
|
||||
import plus.bookshelf.Dao.DO.UserDO;
|
||||
import plus.bookshelf.Dao.Mapper.UserDOMapper;
|
||||
import plus.bookshelf.Service.Model.UserModel;
|
||||
@@ -29,6 +34,27 @@ public class UserServiceImpl implements UserService {
|
||||
return userModel;
|
||||
}
|
||||
|
||||
@Override
|
||||
public UserModel getUserByToken(RedisTemplate redisTemplate, String token) throws BusinessException {
|
||||
// token 未传入
|
||||
if (token == null || "".equals(token)) {
|
||||
throw new BusinessException(BusinessErrorCode.PARAMETER_VALIDATION_ERROR, "用户令牌未传入");
|
||||
}
|
||||
|
||||
// token 已过期
|
||||
Object userIdObject = RedisSessionManager.getInstance(redisTemplate).getValue(token);
|
||||
if (userIdObject == null) {
|
||||
throw new BusinessException(BusinessErrorCode.USER_TOKEN_EXPIRED, "登陆过期啦,请重新登录");
|
||||
}
|
||||
|
||||
Integer userId = (Integer) userIdObject;
|
||||
UserModel userModel = getUserById(userId);
|
||||
if (userModel == null) {
|
||||
throw new BusinessException(BusinessErrorCode.USER_NOT_EXIST);
|
||||
}
|
||||
return userModel;
|
||||
}
|
||||
|
||||
private UserModel convertFromDataObject(UserDO userDO) {
|
||||
if (userDO == null) {
|
||||
return null;
|
||||
|
Reference in New Issue
Block a user