更改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 * token
* *
* @author ruoyi * @author ruoyi
*/ */
@RestController @RestController
@ -35,7 +35,7 @@ public class TokenController
public R<?> login(@RequestBody LoginBody form) public R<?> login(@RequestBody LoginBody form)
{ {
// 用户登录 // 用户登录
LoginUser userInfo = sysLoginService.login(form.getUsername(), form.getPassword()); LoginUser userInfo = sysLoginService.login(form.getEmail(), form.getPassword());
// 获取登录token // 获取登录token
return R.ok(tokenService.createToken(userInfo)); return R.ok(tokenService.createToken(userInfo));
} }

View File

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

View File

@ -20,7 +20,7 @@ import com.four.system.api.model.LoginUser;
/** /**
* *
* *
* @author ruoyi * @author ruoyi
*/ */
@Component @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("用户/密码必须填写"); throw new ServiceException("用户/密码必须填写");
} }
// 密码如果不在指定范围内 错误 // 密码如果不在指定范围内 错误
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) || password.length() > UserConstants.PASSWORD_MAX_LENGTH)
{ {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围"); recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "用户密码不在指定范围");
throw new ServiceException("用户密码不在指定范围"); throw new ServiceException("用户密码不在指定范围");
} }
// 用户名不在指定范围内 错误 // 用户名不在指定范围内 错误
if (username.length() < UserConstants.USERNAME_MIN_LENGTH if (email.length() < UserConstants.USERNAME_MIN_LENGTH
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) || email.length() > UserConstants.USERNAME_MAX_LENGTH)
{ {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围"); recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "用户名不在指定范围");
throw new ServiceException("用户名不在指定范围"); throw new ServiceException("用户名不在指定范围");
} }
// IP黑名单校验 // IP黑名单校验
String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST)); String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr()))
{ {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "很遗憾访问IP已被列入系统黑名单"); recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "很遗憾访问IP已被列入系统黑名单");
throw new ServiceException("很遗憾访问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())) if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
{ {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在"); recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "登录用户不存在");
throw new ServiceException("登录用户:" + username + " 不存在"); throw new ServiceException("登录用户:" + email + " 不存在");
} }
if (R.FAIL == userResult.getCode()) if (R.FAIL == userResult.getCode())
{ {
throw new ServiceException(userResult.getMsg()); throw new ServiceException(userResult.getMsg());
} }
LoginUser userInfo = userResult.getData(); LoginUser userInfo = userResult.getData();
SysUser user = userResult.getData().getSysUser(); SysUser user = userResult.getData().getSysUser();
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{ {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除"); recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
throw new ServiceException("对不起,您的账号:" + username + " 已被删除"); throw new ServiceException("对不起,您的账号:" + email + " 已被删除");
} }
if (UserStatus.DISABLE.getCode().equals(user.getStatus())) if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{ {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员"); recordLogService.recordLogininfor(email, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + username + " 已停用"); throw new ServiceException("对不起,您的账号:" + email + " 已停用");
} }
passwordService.validate(user, password); passwordService.validate(user, password);
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功"); recordLogService.recordLogininfor(email, Constants.LOGIN_SUCCESS, "登录成功");
return userInfo; return userInfo;
} }

View File

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

View File

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

View File

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