feat(登录): 实现用户登录功能及相关模块
- 添加LoginController处理登录请求 - 实现UserLoginService登录逻辑,包括角色和权限验证 - 新增LoginRequest和LoginUser DTO - 在UsersService中添加登录方法 - 添加RoleInitializer初始化系统角色 - 更新项目结构文档 - 临时禁用Spring Security配置
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
package com.qf.backend.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.baomidou.mybatisplus.extension.service.IService;
|
||||
import com.qf.backend.common.Result;
|
||||
import com.qf.backend.entity.Users;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* 用户服务接口
|
||||
*/
|
||||
@@ -25,6 +25,14 @@ public interface UsersService extends IService<Users> {
|
||||
*/
|
||||
Result<Users> getUserByEmail(String email);
|
||||
|
||||
/**
|
||||
* 登录
|
||||
* @param username 用户名
|
||||
* @param password 密码
|
||||
* @return 用户信息
|
||||
*/
|
||||
Result<Users> login(String username, String password);
|
||||
|
||||
/**
|
||||
* 创建用户
|
||||
* @param users 用户信息
|
||||
|
||||
@@ -4,6 +4,12 @@
|
||||
*/
|
||||
|
||||
package com.qf.backend.service.impl;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashSet;
|
||||
import java.util.List;
|
||||
import java.util.Set;
|
||||
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
@@ -12,13 +18,21 @@ import org.springframework.stereotype.Service;
|
||||
import com.qf.backend.common.Result;
|
||||
import com.qf.backend.common.ResultUtils;
|
||||
import com.qf.backend.dto.LoginUser;
|
||||
import com.qf.backend.entity.Permissions;
|
||||
import com.qf.backend.entity.Roles;
|
||||
import com.qf.backend.entity.UserRoles;
|
||||
import com.qf.backend.entity.Users;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.service.PermissionsService;
|
||||
import com.qf.backend.service.RolePermissionsService;
|
||||
import com.qf.backend.service.RolesService;
|
||||
import com.qf.backend.service.UserLoginService;
|
||||
import com.qf.backend.service.UserRolesService;
|
||||
import com.qf.backend.service.UsersService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
* @author 30803
|
||||
@@ -28,7 +42,14 @@ public class UserLoginServiceImpl implements UserLoginService {
|
||||
private Logger logger = LoggerFactory.getLogger(UserLoginServiceImpl.class);
|
||||
@Autowired
|
||||
private UsersService usersServiceImpl;
|
||||
|
||||
@Autowired
|
||||
private UserRolesService userRolesServiceImpl;
|
||||
@Autowired
|
||||
private RolesService rolesServiceImpl;
|
||||
@Autowired
|
||||
private RolePermissionsService rolePermissionsServiceImpl;
|
||||
@Autowired
|
||||
private PermissionsService permissionsServiceImpl;
|
||||
/**
|
||||
* 用户登录
|
||||
* @param username 用户名
|
||||
@@ -39,26 +60,62 @@ public class UserLoginServiceImpl implements UserLoginService {
|
||||
public Result<LoginUser> login(String username, String password) {
|
||||
logger.info("用户登录,用户名:{}", username);
|
||||
// 1. 校验用户名和密码是否为空
|
||||
try{
|
||||
if (ValidateUtil.isEmpty(username) || ValidateUtil.isEmpty(password)) {
|
||||
return ResultUtils.fail(ErrorCode.PARAM_ERROR, "用户名或密码不能为空");
|
||||
}
|
||||
// 2. 校验用户名是否存在
|
||||
Users user = usersServiceImpl.getUserByUsername(username).getData();
|
||||
if (user == null) {
|
||||
return ResultUtils.fail(ErrorCode.USER_NOT_FOUND, "用户名不存在");
|
||||
}
|
||||
// 3. 校验密码是否正确
|
||||
throw new IllegalArgumentException("用户名或密码不能为空");
|
||||
}
|
||||
// 加密密码
|
||||
String encryptedPassword = ValidateUtil.encryptPassword(password);
|
||||
if (!user.getPassword().equals(encryptedPassword)) {
|
||||
return ResultUtils.fail(ErrorCode.PASSWORD_ERROR, "密码错误");
|
||||
// 2. 登录
|
||||
Result<Users> result = usersServiceImpl.login(username, encryptedPassword);
|
||||
if (result == null || result.getData() == null) {
|
||||
throw new IllegalArgumentException("用户名不存在或密码错误");
|
||||
}
|
||||
// 4. 登录成功,返回登录用户信息
|
||||
|
||||
Users user = result.getData();
|
||||
// 3. 获取用户角色
|
||||
Result<List<UserRoles>> userRolesResult = userRolesServiceImpl.getUserRolesByUserId(user.getId());
|
||||
if (userRolesResult == null || userRolesResult.getData() == null) {
|
||||
throw new IllegalArgumentException("获取用户角色失败");
|
||||
}
|
||||
|
||||
List<UserRoles> userRoles = userRolesResult.getData();
|
||||
Set<String> roleNames = new HashSet<>();
|
||||
Set<Long> roleIds = new HashSet<>();
|
||||
|
||||
// 4. 获取角色名称和角色ID
|
||||
for (UserRoles ur : userRoles) {
|
||||
Roles role = rolesServiceImpl.getById(ur.getRoleId());
|
||||
if (role != null) {
|
||||
roleNames.add(role.getRoleName());
|
||||
roleIds.add(role.getId());
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 获取用户权限
|
||||
Set<String> permissionCodes = new HashSet<>();
|
||||
for (Long roleId : roleIds) {
|
||||
List<Long> permissionIds = rolePermissionsServiceImpl.listPermissionIdsByRoleId(roleId);
|
||||
for (Long permissionId : permissionIds) {
|
||||
Permissions permission = permissionsServiceImpl.getById(permissionId);
|
||||
if (permission != null) {
|
||||
permissionCodes.add(permission.getPermissionCode());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 6. 构建LoginUser对象
|
||||
LoginUser loginUser = new LoginUser();
|
||||
// loginUser.setUserId(user.getUserId());
|
||||
loginUser.setId(user.getId());
|
||||
loginUser.setUsername(user.getUsername());
|
||||
// loginUser.setRoles(userRolesServiceImpl.findRolesByUserId(user.getUserId()));
|
||||
loginUser.setRoles(new ArrayList<>(roleNames));
|
||||
loginUser.setPermissions(new ArrayList<>(permissionCodes));
|
||||
|
||||
return ResultUtils.success(loginUser);
|
||||
} catch (Exception e) {
|
||||
logger.error("用户登录失败,用户名:{}", username, e);
|
||||
return ResultUtils.fail(ErrorCode.SYSTEM_ERROR, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -8,6 +8,7 @@ package com.qf.backend.service.impl;
|
||||
import java.util.List;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
||||
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
||||
@@ -18,7 +19,6 @@ import com.qf.backend.exception.BusinessException;
|
||||
import com.qf.backend.exception.ErrorCode;
|
||||
import com.qf.backend.mapper.UserRolesMapper;
|
||||
import com.qf.backend.service.UserRolesService;
|
||||
import com.qf.backend.util.ValidateUtil;
|
||||
|
||||
|
||||
|
||||
@@ -27,6 +27,7 @@ import com.qf.backend.util.ValidateUtil;
|
||||
*
|
||||
* @author 30803
|
||||
*/
|
||||
@Service
|
||||
public class UsersRolesServiceImpl extends ServiceImpl<UserRolesMapper, UserRoles> implements UserRolesService {
|
||||
@Autowired
|
||||
private UserRolesMapper userRolesMapper;
|
||||
|
||||
@@ -79,7 +79,29 @@ public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "查询用户失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
// 登录
|
||||
@Override
|
||||
public Result<Users> login(String username, String password) {
|
||||
logger.info("登录: 用户名 = {}", username);
|
||||
try {
|
||||
if (ValidateUtil.isEmpty(username) || ValidateUtil.isEmpty(password)) {
|
||||
throw new BusinessException(ErrorCode.INVALID_PARAM, "用户名或密码不能为空");
|
||||
}
|
||||
|
||||
// 校验用户名是否存在
|
||||
Users user = usersMapper.login(username, password);
|
||||
if (user == null) {
|
||||
throw new BusinessException(ErrorCode.USER_NOT_FOUND, "用户名不存在或密码错误");
|
||||
}
|
||||
|
||||
return ResultUtils.success(user);
|
||||
} catch (BusinessException e) {
|
||||
throw e;
|
||||
} catch (Exception e) {
|
||||
logger.error("登录失败: {}", e.getMessage(), e);
|
||||
throw new BusinessException(ErrorCode.DATABASE_ERROR, "登录失败: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
//创建用户
|
||||
@Override
|
||||
public Result<Boolean> createUser(Users users) {
|
||||
|
||||
Reference in New Issue
Block a user