forked from gtl/gtl-ruoyi-server
兼容邮箱登录,优化登录错误提示
parent
ee50cf4379
commit
146ec18831
|
@ -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.getUsernameOrEmail(), form.getPassword());
|
||||
// 获取登录token
|
||||
return Result.success(tokenService.createToken(userInfo));
|
||||
}
|
||||
|
@ -66,7 +66,7 @@ public class TokenController {
|
|||
@PostMapping("register")
|
||||
public Result<?> register (@RequestBody RegisterBody registerBody) {
|
||||
// 用户注册
|
||||
sysLoginService.register(registerBody.getUsername(), registerBody.getPassword());
|
||||
sysLoginService.register(registerBody.getUsernameOrEmail(), registerBody.getPassword());
|
||||
return Result.success();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,34 +1,22 @@
|
|||
package com.ruoyi.auth.form;
|
||||
|
||||
import lombok.Data;
|
||||
|
||||
/**
|
||||
* 用户登录对象
|
||||
*
|
||||
* @author muyu
|
||||
*/
|
||||
@Data
|
||||
public class LoginBody {
|
||||
/**
|
||||
* 用户名
|
||||
* 用户名或邮箱
|
||||
*/
|
||||
private String username;
|
||||
private String usernameOrEmail;
|
||||
|
||||
/**
|
||||
* 用户密码
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -40,36 +40,36 @@ public class SysLoginService {
|
|||
/**
|
||||
* 登录
|
||||
*/
|
||||
public LoginUser login (String username, String password) {
|
||||
// 用户名或密码为空 错误
|
||||
if (StringUtils.isAnyBlank(username, password)) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
|
||||
public LoginUser login (String usernameOrEmail, String password) {
|
||||
// 用户名/邮箱或密码为空 错误
|
||||
if (StringUtils.isAnyBlank(usernameOrEmail, password)) {
|
||||
recordLogService.recordLogininfor(usernameOrEmail, 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, "用户密码不在指定范围");
|
||||
recordLogService.recordLogininfor(usernameOrEmail, 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 (usernameOrEmail.length() < UserConstants.USERNAME_OR_EMAIL_MIN_LENGTH
|
||||
|| usernameOrEmail.length() > UserConstants.USERNAME_OR_EMAIL_MAX_LENGTH) {
|
||||
recordLogService.recordLogininfor(usernameOrEmail, 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(usernameOrEmail, Constants.LOGIN_FAIL, "很遗憾,访问IP已被列入系统黑名单");
|
||||
throw new ServiceException("很遗憾,访问IP已被列入系统黑名单");
|
||||
}
|
||||
// 查询用户信息
|
||||
Result<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
|
||||
Result<LoginUser> userResult = remoteUserService.getUserInfo(usernameOrEmail, SecurityConstants.INNER);
|
||||
|
||||
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
|
||||
throw new ServiceException("登录用户:" + username + " 不存在");
|
||||
recordLogService.recordLogininfor(usernameOrEmail, Constants.LOGIN_FAIL, "登录用户不存在");
|
||||
throw new ServiceException("登录用户:" + usernameOrEmail + " 不存在");
|
||||
}
|
||||
|
||||
if (Result.FAIL == userResult.getCode()) {
|
||||
|
@ -79,15 +79,16 @@ 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(usernameOrEmail, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
|
||||
throw new ServiceException("对不起,您的账号:" + usernameOrEmail + " 已被删除");
|
||||
}
|
||||
if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
|
||||
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
|
||||
recordLogService.recordLogininfor(usernameOrEmail, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
|
||||
throw new ServiceException("对不起,您的账号:" + usernameOrEmail + " 已停用");
|
||||
}
|
||||
passwordService.validate(user, password);
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
|
||||
passwordService.validate(user, password,userResult);
|
||||
|
||||
recordLogService.recordLogininfor(usernameOrEmail, Constants.LOGIN_SUCCESS, "登录成功");
|
||||
return userInfo;
|
||||
}
|
||||
|
||||
|
@ -103,8 +104,8 @@ public class SysLoginService {
|
|||
if (StringUtils.isAnyBlank(username, password)) {
|
||||
throw new ServiceException("用户/密码必须填写");
|
||||
}
|
||||
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
||||
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
|
||||
if (username.length() < UserConstants.USERNAME_OR_EMAIL_MIN_LENGTH
|
||||
|| username.length() > UserConstants.USERNAME_OR_EMAIL_MAX_LENGTH) {
|
||||
throw new ServiceException("账户长度必须在2到20个字符之间");
|
||||
}
|
||||
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|
||||
|
|
|
@ -2,9 +2,11 @@ package com.ruoyi.auth.service;
|
|||
|
||||
import com.ruoyi.common.core.constant.CacheConstants;
|
||||
import com.ruoyi.common.core.constant.Constants;
|
||||
import com.ruoyi.common.core.domain.Result;
|
||||
import com.ruoyi.common.core.exception.ServiceException;
|
||||
import com.ruoyi.common.redis.service.RedisService;
|
||||
import com.ruoyi.common.security.utils.SecurityUtils;
|
||||
import com.ruoyi.common.system.domain.LoginUser;
|
||||
import com.ruoyi.common.system.domain.SysUser;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.stereotype.Component;
|
||||
|
@ -39,7 +41,7 @@ public class SysPasswordService {
|
|||
return CacheConstants.PWD_ERR_CNT_KEY + username;
|
||||
}
|
||||
|
||||
public void validate (SysUser user, String password) {
|
||||
public void validate (SysUser user, String password, Result<LoginUser> userResult) {
|
||||
String username = user.getUserName();
|
||||
|
||||
Integer retryCount = redisService.getCacheObject(getCacheKey(username));
|
||||
|
@ -58,7 +60,7 @@ 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("用户不存在/密码错误");
|
||||
throw new ServiceException(userResult.getMsg());
|
||||
} else {
|
||||
clearLoginRecordCache(username);
|
||||
}
|
||||
|
|
|
@ -15,9 +15,11 @@ spring:
|
|||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
|
|
|
@ -100,9 +100,9 @@ public class UserConstants {
|
|||
/**
|
||||
* 用户名长度限制
|
||||
*/
|
||||
public static final int USERNAME_MIN_LENGTH = 2;
|
||||
public static final int USERNAME_OR_EMAIL_MIN_LENGTH = 2;
|
||||
|
||||
public static final int USERNAME_MAX_LENGTH = 20;
|
||||
public static final int USERNAME_OR_EMAIL_MAX_LENGTH = 20;
|
||||
|
||||
/**
|
||||
* 密码长度限制
|
||||
|
|
|
@ -19,13 +19,13 @@ public interface RemoteUserService {
|
|||
/**
|
||||
* 通过用户名查询用户信息
|
||||
*
|
||||
* @param username 用户名
|
||||
* @param usernameOrEmail 用户名或密码
|
||||
* @param source 请求来源
|
||||
*
|
||||
* @return 结果
|
||||
*/
|
||||
@GetMapping("/user/info/{username}")
|
||||
public Result<LoginUser> getUserInfo (@PathVariable("username") String username, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
@GetMapping("/user/info/{usernameOrEmail}")
|
||||
public Result<LoginUser> getUserInfo (@PathVariable("usernameOrEmail") String usernameOrEmail, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
|
||||
|
||||
/**
|
||||
* 注册用户信息
|
||||
|
|
|
@ -15,9 +15,11 @@ spring:
|
|||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
|
|
|
@ -15,9 +15,11 @@ spring:
|
|||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
|
|
|
@ -15,9 +15,11 @@ spring:
|
|||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
|
|
|
@ -15,9 +15,11 @@ spring:
|
|||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
|
|
|
@ -18,7 +18,6 @@ import com.ruoyi.system.service.SysUserService;
|
|||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.web.bind.annotation.*;
|
||||
import org.springframework.web.multipart.MultipartFile;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
/**
|
||||
|
@ -44,7 +43,7 @@ public class SysProfileController extends BaseController {
|
|||
@GetMapping
|
||||
public Result profile () {
|
||||
String username = SecurityUtils.getUsername();
|
||||
SysUser user = userService.selectUserByUserName(username);
|
||||
SysUser user = userService.selectUserByUserNameOrEmail(username);
|
||||
return Result.success(
|
||||
ProfileResp.builder()
|
||||
.roleGroup( userService.selectUserRoleGroup(username) )
|
||||
|
@ -87,7 +86,7 @@ public class SysProfileController extends BaseController {
|
|||
@PutMapping("/updatePwd")
|
||||
public Result updatePwd (String oldPassword, String newPassword) {
|
||||
String username = SecurityUtils.getUsername();
|
||||
SysUser user = userService.selectUserByUserName(username);
|
||||
SysUser user = userService.selectUserByUserNameOrEmail(username);
|
||||
String password = user.getPassword();
|
||||
if (!SecurityUtils.matchesPassword(oldPassword, password)) {
|
||||
return error("修改密码失败,旧密码错误");
|
||||
|
|
|
@ -97,11 +97,15 @@ public class SysUserController extends BaseController {
|
|||
* 获取当前用户信息
|
||||
*/
|
||||
@InnerAuth
|
||||
@GetMapping("/info/{username}")
|
||||
public Result<LoginUser> info (@PathVariable("username") String username) {
|
||||
SysUser sysUser = userService.selectUserByUserName(username);
|
||||
@GetMapping("/info/{usernameOrEmail}")
|
||||
public Result<LoginUser> info (@PathVariable("usernameOrEmail") String usernameOrEmail) {
|
||||
SysUser sysUser = userService.selectUserByUserNameOrEmail(usernameOrEmail);
|
||||
String mayNeedErrorMassage="用户名或密码错误";
|
||||
if(usernameOrEmail.matches("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*$")){
|
||||
mayNeedErrorMassage="邮箱或密码错误";
|
||||
}
|
||||
if (StringUtils.isNull(sysUser)) {
|
||||
return Result.error("用户名或密码错误");
|
||||
return Result.error();
|
||||
}
|
||||
// 角色集合
|
||||
Set<String> roles = permissionService.getRolePermission(sysUser);
|
||||
|
@ -111,7 +115,7 @@ public class SysUserController extends BaseController {
|
|||
sysUserVo.setSysUser(sysUser);
|
||||
sysUserVo.setRoles(roles);
|
||||
sysUserVo.setPermissions(permissions);
|
||||
return Result.success(sysUserVo);
|
||||
return Result.success(sysUserVo,mayNeedErrorMassage);
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
|
@ -42,11 +42,11 @@ public interface SysUserMapper extends BaseMapper<SysUser> {
|
|||
/**
|
||||
* 通过用户名查询用户
|
||||
*
|
||||
* @param userName 用户名
|
||||
* @param userNameOrEmail 用户名
|
||||
*
|
||||
* @return 用户对象信息
|
||||
*/
|
||||
public SysUser selectUserByUserName (String userName);
|
||||
public SysUser selectUserByUserNameOrEmail (String userNameOrEmail);
|
||||
|
||||
/**
|
||||
* 通过用户ID查询用户
|
||||
|
|
|
@ -41,11 +41,11 @@ public interface SysUserService extends IService<SysUser> {
|
|||
/**
|
||||
* 通过用户名查询用户
|
||||
*
|
||||
* @param userName 用户名
|
||||
* @param userNameOrEmail 用户名
|
||||
*
|
||||
* @return 用户对象信息
|
||||
*/
|
||||
public SysUser selectUserByUserName (String userName);
|
||||
public SysUser selectUserByUserNameOrEmail (String userNameOrEmail);
|
||||
|
||||
/**
|
||||
* 通过用户ID查询用户
|
||||
|
|
|
@ -93,13 +93,13 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|||
/**
|
||||
* 通过用户名查询用户
|
||||
*
|
||||
* @param userName 用户名
|
||||
* @param userNameOrEmail 用户名
|
||||
*
|
||||
* @return 用户对象信息
|
||||
*/
|
||||
@Override
|
||||
public SysUser selectUserByUserName (String userName) {
|
||||
return userMapper.selectUserByUserName(userName);
|
||||
public SysUser selectUserByUserNameOrEmail (String userNameOrEmail) {
|
||||
return userMapper.selectUserByUserNameOrEmail(userNameOrEmail);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -463,7 +463,7 @@ public class SysUserServiceImpl extends ServiceImpl<SysUserMapper, SysUser> impl
|
|||
for (SysUser user : userList) {
|
||||
try {
|
||||
// 验证是否存在这个用户
|
||||
SysUser u = userMapper.selectUserByUserName(user.getUserName());
|
||||
SysUser u = userMapper.selectUserByUserNameOrEmail(user.getUserName());
|
||||
if (StringUtils.isNull(u)) {
|
||||
BeanValidators.validateWithException(validator, user);
|
||||
user.setPassword(SecurityUtils.encryptPassword(password));
|
||||
|
|
|
@ -15,9 +15,11 @@ spring:
|
|||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
|
|
|
@ -150,9 +150,9 @@
|
|||
${params.dataScope}
|
||||
</select>
|
||||
|
||||
<select id="selectUserByUserName" parameterType="String" resultMap="SysUserResult">
|
||||
<select id="selectUserByUserNameOrEmail" parameterType="String" resultMap="SysUserResult">
|
||||
<include refid="selectUserVo"/>
|
||||
where u.user_name = #{userName} and u.del_flag = '0'
|
||||
where (u.user_name = #{userNameOrEmail} or u.email = #{userNameOrEmail}) and u.del_flag = '0'
|
||||
</select>
|
||||
|
||||
<select id="selectUserById" parameterType="Long" resultMap="SysUserResult">
|
||||
|
|
|
@ -15,9 +15,11 @@ spring:
|
|||
discovery:
|
||||
# 服务注册地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
config:
|
||||
# 配置中心地址
|
||||
server-addr: 47.98.98.250:8848
|
||||
namespace: 143f1a53-e544-4782-8667-877c532e2c66
|
||||
# 配置文件格式
|
||||
file-extension: yml
|
||||
# 共享配置
|
||||
|
|
Loading…
Reference in New Issue