Compare commits
1 Commits
v2.3.0(jdk
...
add-email-
Author | SHA1 | Date | |
---|---|---|---|
![]() |
a730c46a8c |
@@ -92,7 +92,7 @@ public class AuthController {
|
|||||||
@GetMapping("/get-permission-info")
|
@GetMapping("/get-permission-info")
|
||||||
@Operation(summary = "获取登录用户的权限信息")
|
@Operation(summary = "获取登录用户的权限信息")
|
||||||
public CommonResult<AuthPermissionInfoRespVO> getPermissionInfo() {
|
public CommonResult<AuthPermissionInfoRespVO> getPermissionInfo() {
|
||||||
// 1.1 获得用户信息
|
// 1.1 <EFBFBD>
|
||||||
AdminUserDO user = userService.getUser(getLoginUserId());
|
AdminUserDO user = userService.getUser(getLoginUserId());
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
return success(null);
|
return success(null);
|
||||||
@@ -154,4 +154,13 @@ public class AuthController {
|
|||||||
return success(authService.socialLogin(reqVO));
|
return success(authService.socialLogin(reqVO));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ========== 邮箱登录相关 ==========
|
||||||
|
|
||||||
|
@PostMapping("/email-login")
|
||||||
|
@PermitAll
|
||||||
|
@Operation(summary = "使用邮箱和密码登录")
|
||||||
|
public CommonResult<AuthLoginRespVO> emailLogin(@RequestBody @Valid AuthEmailLoginReqVO reqVO) {
|
||||||
|
return success(authService.emailLogin(reqVO));
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -11,6 +11,7 @@ import lombok.NoArgsConstructor;
|
|||||||
import org.hibernate.validator.constraints.Length;
|
import org.hibernate.validator.constraints.Length;
|
||||||
|
|
||||||
import javax.validation.constraints.AssertTrue;
|
import javax.validation.constraints.AssertTrue;
|
||||||
|
import javax.validation.constraints.Email;
|
||||||
import javax.validation.constraints.NotEmpty;
|
import javax.validation.constraints.NotEmpty;
|
||||||
import javax.validation.constraints.Pattern;
|
import javax.validation.constraints.Pattern;
|
||||||
|
|
||||||
@@ -32,6 +33,11 @@ public class AuthLoginReqVO {
|
|||||||
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
|
@Length(min = 4, max = 16, message = "密码长度为 4-16 位")
|
||||||
private String password;
|
private String password;
|
||||||
|
|
||||||
|
@Schema(description = "邮箱", requiredMode = Schema.RequiredMode.REQUIRED, example = "example@example.com")
|
||||||
|
@NotEmpty(message = "邮箱不能为空")
|
||||||
|
@Email(message = "邮箱格式不正确")
|
||||||
|
private String email;
|
||||||
|
|
||||||
// ========== 图片验证码相关 ==========
|
// ========== 图片验证码相关 ==========
|
||||||
|
|
||||||
@Schema(description = "验证码,验证码开启时,需要传递", requiredMode = Schema.RequiredMode.REQUIRED,
|
@Schema(description = "验证码,验证码开启时,需要传递", requiredMode = Schema.RequiredMode.REQUIRED,
|
||||||
@@ -66,4 +72,4 @@ public class AuthLoginReqVO {
|
|||||||
return socialType == null || StrUtil.isNotEmpty(socialState);
|
return socialType == null || StrUtil.isNotEmpty(socialState);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -27,4 +27,7 @@ public class AuthLoginRespVO {
|
|||||||
@Schema(description = "过期时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
@Schema(description = "过期时间", requiredMode = Schema.RequiredMode.REQUIRED)
|
||||||
private LocalDateTime expiresTime;
|
private LocalDateTime expiresTime;
|
||||||
|
|
||||||
|
@Schema(description = "邮箱", requiredMode = Schema.RequiredMode.REQUIRED, example = "example@example.com")
|
||||||
|
private String email;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -70,4 +70,12 @@ public interface AdminAuthService {
|
|||||||
*/
|
*/
|
||||||
AuthLoginRespVO refreshToken(String refreshToken);
|
AuthLoginRespVO refreshToken(String refreshToken);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 邮箱登录
|
||||||
|
*
|
||||||
|
* @param reqVO 登录信息
|
||||||
|
* @return 登录结果
|
||||||
|
*/
|
||||||
|
AuthLoginRespVO emailLogin(@Valid AuthEmailLoginReqVO reqVO);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@@ -42,7 +42,7 @@ import static cn.iocoder.yudao.module.system.enums.ErrorCodeConstants.*;
|
|||||||
/**
|
/**
|
||||||
* Auth Service 实现类
|
* Auth Service 实现类
|
||||||
*
|
*
|
||||||
* @author 芋道源码
|
* @author <EFBFBD>
|
||||||
*/
|
*/
|
||||||
@Service
|
@Service
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@@ -66,7 +66,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|||||||
private SmsCodeApi smsCodeApi;
|
private SmsCodeApi smsCodeApi;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 验证码的开关,默认为 true
|
* <EFBFBD>
|
||||||
*/
|
*/
|
||||||
@Value("${yudao.captcha.enable:true}")
|
@Value("${yudao.captcha.enable:true}")
|
||||||
private Boolean captchaEnable;
|
private Boolean captchaEnable;
|
||||||
@@ -124,7 +124,7 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|||||||
// 校验验证码
|
// 校验验证码
|
||||||
smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.ADMIN_MEMBER_LOGIN.getScene(), getClientIP()));
|
smsCodeApi.useSmsCode(AuthConvert.INSTANCE.convert(reqVO, SmsSceneEnum.ADMIN_MEMBER_LOGIN.getScene(), getClientIP()));
|
||||||
|
|
||||||
// 获得用户信息
|
// <EFBFBD>
|
||||||
AdminUserDO user = userService.getUserByMobile(reqVO.getMobile());
|
AdminUserDO user = userService.getUserByMobile(reqVO.getMobile());
|
||||||
if (user == null) {
|
if (user == null) {
|
||||||
throw exception(USER_NOT_EXISTS);
|
throw exception(USER_NOT_EXISTS);
|
||||||
@@ -134,6 +134,35 @@ public class AdminAuthServiceImpl implements AdminAuthService {
|
|||||||
return createTokenAfterLoginSuccess(user.getId(), reqVO.getMobile(), LoginLogTypeEnum.LOGIN_MOBILE);
|
return createTokenAfterLoginSuccess(user.getId(), reqVO.getMobile(), LoginLogTypeEnum.LOGIN_MOBILE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public AuthLoginRespVO emailLogin(AuthEmailLoginReqVO 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,
|
private void createLoginLog(Long userId, String username,
|
||||||
LoginLogTypeEnum logTypeEnum, LoginResultEnum loginResult) {
|
LoginLogTypeEnum logTypeEnum, LoginResultEnum loginResult) {
|
||||||
// 插入登录日志
|
// 插入登录日志
|
||||||
|
Reference in New Issue
Block a user