增加邮箱登录功能 后端
parent
e00a0e3e4b
commit
d817a35fe8
|
@ -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));
|
||||
|
|
|
@ -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;
|
||||
}
|
||||
|
|
|
@ -68,14 +68,17 @@ public class SysLoginService {
|
|||
Result<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
|
||||
|
||||
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
|
||||
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) {
|
||||
|
|
|
@ -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);
|
||||
if (user.getEmail()==null){
|
||||
throw new ServiceException("邮箱不存在/密码错误");
|
||||
}else{
|
||||
throw new ServiceException("用户不存在/密码错误");
|
||||
}
|
||||
} else {
|
||||
clearLoginRecordCache(username);
|
||||
}
|
||||
|
|
|
@ -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">
|
||||
|
|
Loading…
Reference in New Issue