1
0
Fork 0

增加邮箱登录功能 后端

master
Jiang Peng 2024-04-12 14:30:51 +08:00
parent e00a0e3e4b
commit d817a35fe8
5 changed files with 39 additions and 11 deletions

View File

@ -34,6 +34,7 @@ public class TokenController {
@PostMapping("login")
public Result<?> login (@RequestBody LoginBody form) {
// 用户登录
form.setEmail(form.getUsername()+"@163.com");
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
// 获取登录token
return Result.success(tokenService.createToken(userInfo));

View File

@ -11,6 +11,11 @@ public class LoginBody {
*/
private String username;
/**
*
*/
private String email;
/**
*
*/
@ -28,6 +33,14 @@ public class LoginBody {
return password;
}
public String getEmail() {
return email;
}
public void setEmail(String userEmail){
this.email = userEmail;
}
public void setPassword (String password) {
this.password = password;
}

View File

@ -68,14 +68,17 @@ public class SysLoginService {
Result<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
throw new ServiceException("登录用户:" + username + " 不存在");
if (username.contains("@")){
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录邮箱不存在");
throw new ServiceException("登录邮箱:" + username + " 不存在");
}else{
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
throw new ServiceException("登录用户:" + username + " 不存在");
}
}
if (Result.FAIL == userResult.getCode()) {
throw new ServiceException(userResult.getMsg());
}
LoginUser userInfo = userResult.getData();
SysUser user = userResult.getData().getSysUser();
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
@ -86,8 +89,15 @@ public class SysLoginService {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
}
if (username.contains("@")){
user.setEmail(null);
}
passwordService.validate(user, password);
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
if (username.contains("@")){
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "邮箱登录成功");
}else{
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "账号登录成功");
}
return userInfo;
}
@ -101,7 +111,7 @@ public class SysLoginService {
public void register (String username, String password) {
// 用户名或密码为空 错误
if (StringUtils.isAnyBlank(username, password)) {
throw new ServiceException("用户/密码必须填写");
throw new ServiceException("用户/邮箱/密码必须填写");
}
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {

View File

@ -58,7 +58,11 @@ public class SysPasswordService {
retryCount = retryCount + 1;
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, String.format("密码输入错误%s次", retryCount));
redisService.setCacheObject(getCacheKey(username), retryCount, lockTime, TimeUnit.MINUTES);
throw new ServiceException("用户不存在/密码错误");
if (user.getEmail()==null){
throw new ServiceException("邮箱不存在/密码错误");
}else{
throw new ServiceException("用户不存在/密码错误");
}
} else {
clearLoginRecordCache(username);
}

View File

@ -152,7 +152,7 @@
<select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
<include refid="selectUserVo"/>
where u.user_name = #{userName} and u.del_flag = '0'
where u.user_name = #{userName} or u.email = #{userName} and u.del_flag = '0'
</select>
<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
@ -165,7 +165,7 @@
from sys_user
where user_name = #{userName}
and del_flag = '0'
limit 1
limit 1
</select>
<select id="checkPhoneUnique" parameterType="String" resultMap="SysUserResult">
@ -173,7 +173,7 @@
from sys_user
where phonenumber = #{phonenumber}
and del_flag = '0'
limit 1
limit 1
</select>
<select id="checkEmailUnique" parameterType="String" resultMap="SysUserResult">
@ -181,7 +181,7 @@
from sys_user
where email = #{email}
and del_flag = '0'
limit 1
limit 1
</select>
<insert id="insertUser" parameterType="com.muyu.common.system.domain.SysUser" useGeneratedKeys="true" keyProperty="userId">