若依后台邮件登录

dev
mengwang 2024-04-12 14:25:00 +08:00
parent 4692ac8b21
commit d49321a37b
4 changed files with 29 additions and 38 deletions

View File

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

View File

@ -1,10 +1,13 @@
package com.muyu.auth.form;
import lombok.Data;
/**
*
*
* @author muyu
*/
@Data
public class LoginBody {
/**
*
@ -15,20 +18,4 @@ public class LoginBody {
*
*/
private String password;
public String getUsername () {
return username;
}
public void setUsername (String username) {
this.username = username;
}
public String getPassword () {
return password;
}
public void setPassword (String password) {
this.password = password;
}
}

View File

@ -1,5 +1,6 @@
package com.muyu.auth.service;
import com.muyu.auth.form.LoginBody;
import com.muyu.common.core.constant.CacheConstants;
import com.muyu.common.core.constant.Constants;
import com.muyu.common.core.constant.SecurityConstants;
@ -18,6 +19,9 @@ import com.muyu.common.system.domain.LoginUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
*
*
@ -40,36 +44,36 @@ public class SysLoginService {
/**
*
*/
public LoginUser login (String username, String password) {
public LoginUser login (LoginBody form) {
// 用户名或密码为空 错误
if (StringUtils.isAnyBlank(username, password)) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
throw new ServiceException("用户/密码必须填写");
if (StringUtils.isAnyBlank(form.getUsername(), form.getPassword())) {
recordLogService.recordLogininfor(form.getUsername(), Constants.LOGIN_FAIL, "用户/邮箱/密码必须填写");
throw new ServiceException("用户/邮箱/密码必须填写");
}
// 密码如果不在指定范围内 错误
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
if (form.getPassword().length() < UserConstants.PASSWORD_MIN_LENGTH
|| form.getPassword().length() > UserConstants.PASSWORD_MAX_LENGTH) {
recordLogService.recordLogininfor(form.getUsername(), Constants.LOGIN_FAIL, "用户密码不在指定范围");
throw new ServiceException("用户密码不在指定范围");
}
// 用户名不在指定范围内 错误
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
if (form.getUsername().length() < UserConstants.USERNAME_MIN_LENGTH
|| form.getUsername().length() > UserConstants.USERNAME_MAX_LENGTH) {
recordLogService.recordLogininfor(form.getUsername(), Constants.LOGIN_FAIL, "用户名不在指定范围");
throw new ServiceException("用户名不在指定范围");
}
// IP黑名单校验
String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "很遗憾访问IP已被列入系统黑名单");
recordLogService.recordLogininfor(form.getUsername(), Constants.LOGIN_FAIL, "很遗憾访问IP已被列入系统黑名单");
throw new ServiceException("很遗憾访问IP已被列入系统黑名单");
}
// 查询用户信息
Result<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
Result<LoginUser> userResult = remoteUserService.getUserInfo(form.getUsername(), SecurityConstants.INNER);
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
throw new ServiceException("登录用户:" + username + " 不存在");
recordLogService.recordLogininfor(form.getUsername(), Constants.LOGIN_FAIL, "登录用户不存在");
throw new ServiceException("登录用户:" + form.getUsername() + " 不存在");
}
if (Result.FAIL == userResult.getCode()) {
@ -79,15 +83,15 @@ public class SysLoginService {
LoginUser userInfo = userResult.getData();
SysUser user = userResult.getData().getSysUser();
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
recordLogService.recordLogininfor(form.getUsername(), Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
throw new ServiceException("对不起,您的账号:" + form.getUsername() + " 已被删除");
}
if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
recordLogService.recordLogininfor(form.getUsername(), Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + form.getUsername() + " 已停用");
}
passwordService.validate(user, password);
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
passwordService.validate(user, form.getPassword());
recordLogService.recordLogininfor(form.getUsername(), Constants.LOGIN_SUCCESS, "登录成功");
return userInfo;
}

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">