更改auth

master
ShiSiWei 2023-10-24 21:36:54 +08:00
parent 2479315291
commit d8c34ebf81
6 changed files with 48 additions and 36 deletions

View File

@ -19,7 +19,7 @@ import com.four.system.api.model.LoginUser;
/**
* token
*
*
* @author ruoyi
*/
@RestController
@ -35,7 +35,7 @@ public class TokenController
public R<?> login(@RequestBody LoginBody form)
{
// 用户登录
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword());
LoginUser userInfo = sysLoginService.login(form.getEmail(), form.getPassword());
// 获取登录token
return R.ok(tokenService.createToken(userInfo));
}

View File

@ -2,7 +2,7 @@ package com.four.auth.form;
/**
*
*
*
* @author ruoyi
*/
public class LoginBody
@ -12,6 +12,7 @@ public class LoginBody
*/
private String username;
private String email;
/**
*
*/
@ -22,6 +23,15 @@ public class LoginBody
return username;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public void setUsername(String username)
{
this.username = username;

View File

@ -20,7 +20,7 @@ import com.four.system.api.model.LoginUser;
/**
*
*
*
* @author ruoyi
*/
@Component
@ -41,63 +41,63 @@ public class SysLoginService
/**
*
*/
public LoginUser login(String username, String password)
public LoginUser login(String email, String password)
{
// 用户名或密码为空 错误
if (StringUtils.isAnyBlank(username, password))
if (StringUtils.isAnyBlank(email, password))
{
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
recordLogService.recordLogininfor(email, 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(email, Constants.LOGIN_FAIL, "用户密码不在指定范围");
throw new ServiceException("用户密码不在指定范围");
}
// 用户名不在指定范围内 错误
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|| username.length() > UserConstants.USERNAME_MAX_LENGTH)
if (email.length() < UserConstants.USERNAME_MIN_LENGTH
|| email.length() > UserConstants.USERNAME_MAX_LENGTH)
{
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
recordLogService.recordLogininfor(email, 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(email, Constants.LOGIN_FAIL, "很遗憾访问IP已被列入系统黑名单");
throw new ServiceException("很遗憾访问IP已被列入系统黑名单");
}
// 查询用户信息
R<LoginUser> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
R<LoginUser> userResult = remoteUserService.getUserInfo(email, SecurityConstants.INNER);
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
{
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
throw new ServiceException("登录用户:" + username + " 不存在");
recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "登录用户不存在");
throw new ServiceException("登录用户:" + email + " 不存在");
}
if (R.FAIL == userResult.getCode())
{
throw new ServiceException(userResult.getMsg());
}
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(email, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
throw new ServiceException("对不起,您的账号:" + email + " 已被删除");
}
if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + email + " 已停用");
}
passwordService.validate(user, password);
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
recordLogService.recordLogininfor(email, Constants.LOGIN_SUCCESS, "登录成功");
return userInfo;
}

View File

@ -12,7 +12,7 @@ import com.four.system.api.domain.SysUser;
/**
*
*
*
* @author ruoyi
*/
@Component
@ -30,7 +30,7 @@ public class SysPasswordService
/**
*
*
*
* @param username
* @return key
*/
@ -41,9 +41,9 @@ public class SysPasswordService
public void validate(SysUser user, String password)
{
String username = user.getUserName();
String userEmail = user.getEmail();
Integer retryCount = redisService.getCacheObject(getCacheKey(username));
Integer retryCount = redisService.getCacheObject(getCacheKey(userEmail));
if (retryCount == null)
{
@ -53,20 +53,20 @@ public class SysPasswordService
if (retryCount >= Integer.valueOf(maxRetryCount).intValue())
{
String errMsg = String.format("密码输入错误%s次帐户锁定%s分钟", maxRetryCount, lockTime);
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL,errMsg);
recordLogService.recordLogininfor(userEmail, Constants.LOGIN_FAIL,errMsg);
throw new ServiceException(errMsg);
}
if (!matches(user, password))
{
retryCount = retryCount + 1;
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, String.format("密码输入错误%s次", retryCount));
redisService.setCacheObject(getCacheKey(username), retryCount, lockTime, TimeUnit.MINUTES);
recordLogService.recordLogininfor(userEmail, Constants.LOGIN_FAIL, String.format("密码输入错误%s次", retryCount));
redisService.setCacheObject(getCacheKey(userEmail), retryCount, lockTime, TimeUnit.MINUTES);
throw new ServiceException("用户不存在/密码错误");
}
else
{
clearLoginRecordCache(username);
clearLoginRecordCache(userEmail);
}
}

View File

@ -11,7 +11,7 @@ import com.four.system.api.domain.SysLogininfor;
/**
*
*
*
* @author ruoyi
*/
@Component
@ -22,16 +22,16 @@ public class SysRecordLogService
/**
*
*
* @param username
*
* @param email
* @param status
* @param message
* @return
*/
public void recordLogininfor(String username, String status, String message)
public void recordLogininfor(String email, String status, String message)
{
SysLogininfor logininfor = new SysLogininfor();
logininfor.setUserName(username);
logininfor.setUserName(email);
logininfor.setIpaddr(IpUtils.getIpAddr());
logininfor.setMsg(message);
// 日志状态

View File

@ -1,9 +1,9 @@
# Tomcat
server:
server:
port: 9200
# Spring
spring:
spring:
application:
# 应用名称
name: four-auth
@ -15,9 +15,11 @@ spring:
discovery:
# 服务注册地址
server-addr: 101.34.252.165:8848
namespace: xiaoxu
config:
# 配置中心地址
server-addr: 101.34.252.165:8848
namespace: xiaoxu
# 配置文件格式
file-extension: yml
# 共享配置