Add mail login to management backend
Add email and password login functionality to the management backend. * **AuthController.java** - Add a new endpoint `/mail-login` for email and password login. - Use `@PostMapping` and `@Operation` annotations for the new endpoint. - Call the `authService.mailLogin` method in the new endpoint. * **AdminAuthService.java** - Add a new method `AuthLoginRespVO mailLogin(AuthMailLoginReqVO reqVO)` to the interface. * **AdminAuthServiceImpl.java** - Implement the `mailLogin` method in the `AdminAuthServiceImpl` class. - Authenticate the user using the email and password. - Create a token after successful login and return the response. * **AdminUserService.java** - Add a method `AdminUserDO getUserByEmail(String email)` to the interface. * **AdminUserServiceImpl.java** - Implement the `getUserByEmail` method to fetch user details based on email. * **AuthMailLoginReqVO.java** - Create a new class `AuthMailLoginReqVO` for handling email and password login requests. - Add fields `email` and `password` with appropriate validation annotations. - Add a constructor, getters, and setters for the fields. --- For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/YunaiV/ruoyi-vue-pro?shareId=XXXX-XXXX-XXXX-XXXX).
This commit is contained in:
@@ -154,4 +154,11 @@ public class AuthController {
|
||||
return success(authService.socialLogin(reqVO));
|
||||
}
|
||||
|
||||
@PostMapping("/mail-login")
|
||||
@PermitAll
|
||||
@Operation(summary = "使用邮箱密码登录")
|
||||
public CommonResult<AuthLoginRespVO> mailLogin(@RequestBody @Valid AuthMailLoginReqVO reqVO) {
|
||||
return success(authService.mailLogin(reqVO));
|
||||
}
|
||||
|
||||
}
|
||||
|
@@ -0,0 +1,30 @@
|
||||
package cn.iocoder.yudao.module.system.controller.admin.auth.vo;
|
||||
|
||||
import io.swagger.v3.oas.annotations.media.Schema;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.hibernate.validator.constraints.Length;
|
||||
|
||||
import jakarta.validation.constraints.Email;
|
||||
import jakarta.validation.constraints.NotEmpty;
|
||||
|
||||
@Schema(description = "管理后台 - 邮箱密码登录 Request VO")
|
||||
@Data
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@Builder
|
||||
public class AuthMailLoginReqVO {
|
||||
|
||||
@Schema(description = "邮箱", requiredMode = Schema.RequiredMode.REQUIRED, example = "test@example.com")
|
||||
@NotEmpty(message = "邮箱不能为空")
|
||||
@Email(message = "邮箱格式不正确")
|
||||
private String email;
|
||||
|
||||
@Schema(description = "密码", requiredMode = Schema.RequiredMode.REQUIRED, example = "buzhidao")
|
||||
@NotEmpty(message = "密码不能为空")
|
||||
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
|
||||
private String password;
|
||||
|
||||
}
|
@@ -70,4 +70,12 @@ public interface AdminAuthService {
|
||||
*/
|
||||
AuthLoginRespVO refreshToken(String refreshToken);
|
||||
|
||||
/**
|
||||
* 邮箱登录
|
||||
*
|
||||
* @param reqVO 登录信息
|
||||
* @return 登录结果
|
||||
*/
|
||||
AuthLoginRespVO mailLogin(@Valid AuthMailLoginReqVO reqVO);
|
||||
|
||||
}
|
||||
|
@@ -134,6 +134,35 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
||||
return createTokenAfterLoginSuccess(user.getId(), reqVO.getMobile(), LoginLogTypeEnum.LOGIN_MOBILE);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AuthLoginRespVO mailLogin(AuthMailLoginReqVO reqVO) {
|
||||
// 使用邮箱和密码进行登录
|
||||
AdminUserDO user = authenticateByEmail(reqVO.getEmail(), reqVO.getPassword());
|
||||
|
||||
// 创建 Token 令牌,记录登录日志
|
||||
return createTokenAfterLoginSuccess(user.getId(), reqVO.getEmail(), LoginLogTypeEnum.LOGIN_EMAIL);
|
||||
}
|
||||
|
||||
private AdminUserDO authenticateByEmail(String email, String password) {
|
||||
final LoginLogTypeEnum logTypeEnum = LoginLogTypeEnum.LOGIN_EMAIL;
|
||||
// 校验邮箱是否存在
|
||||
AdminUserDO user = userService.getUserByEmail(email);
|
||||
if (user == null) {
|
||||
createLoginLog(null, email, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
||||
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
|
||||
}
|
||||
if (!userService.isPasswordMatch(password, user.getPassword())) {
|
||||
createLoginLog(user.getId(), email, logTypeEnum, LoginResultEnum.BAD_CREDENTIALS);
|
||||
throw exception(AUTH_LOGIN_BAD_CREDENTIALS);
|
||||
}
|
||||
// 校验是否禁用
|
||||
if (CommonStatusEnum.isDisable(user.getStatus())) {
|
||||
createLoginLog(user.getId(), email, logTypeEnum, LoginResultEnum.USER_DISABLED);
|
||||
throw exception(AUTH_LOGIN_USER_DISABLED);
|
||||
}
|
||||
return user;
|
||||
}
|
||||
|
||||
private void createLoginLog(Long userId, String username,
|
||||
LoginLogTypeEnum logTypeEnum, LoginResultEnum loginResult) {
|
||||
// 插入登录日志
|
||||
|
@@ -105,6 +105,14 @@ public interface AdminUserService {
|
||||
*/
|
||||
AdminUserDO getUserByMobile(String mobile);
|
||||
|
||||
/**
|
||||
* 通过邮箱获取用户
|
||||
*
|
||||
* @param email 邮箱
|
||||
* @return 用户对象信息
|
||||
*/
|
||||
AdminUserDO getUserByEmail(String email);
|
||||
|
||||
/**
|
||||
* 获得用户分页列表
|
||||
*
|
||||
|
@@ -249,6 +249,11 @@ public class AdminUserServiceImpl implements AdminUserService {
|
||||
return userMapper.selectByMobile(mobile);
|
||||
}
|
||||
|
||||
@Override
|
||||
public AdminUserDO getUserByEmail(String email) {
|
||||
return userMapper.selectByEmail(email);
|
||||
}
|
||||
|
||||
@Override
|
||||
public PageResult<AdminUserDO> getUserPage(UserPageReqVO reqVO) {
|
||||
return userMapper.selectPage(reqVO, getDeptCondition(reqVO.getDeptId()));
|
||||
|
Reference in New Issue
Block a user