feat: auth support i18n message
This commit is contained in:
@@ -17,6 +17,6 @@ public class WfcAuthApplication
|
||||
public static void main(String[] args)
|
||||
{
|
||||
SpringApplication.run(WfcAuthApplication.class, args);
|
||||
System.out.println("(♥◠‿◠)ノ゙ 认证授权中心启动成功 ლ(´ڡ`ლ)゙ \n");
|
||||
System.out.println("(♥◠‿◠)ノ゙ Certification and authorization center started successfully ლ(´ڡ`ლ)゙ \n");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,7 +49,7 @@ public class TokenController {
|
||||
if ("u".equals(form.getAuthType())) {
|
||||
WANFiRedirectParams wanFiRedirectParams = form.getWanfiRedirectParams();
|
||||
if (wanFiRedirectParams == null) {
|
||||
return R.fail("Invalid Login Parameter");
|
||||
return R.fail("auth.invalid.login.parameter");
|
||||
}
|
||||
// 用户登录
|
||||
LoginUser<UUser> userInfo = uLoginService.login(form.getUsername(), form.getPassword());
|
||||
@@ -69,7 +69,7 @@ public class TokenController {
|
||||
return R.ok(tokenService.createToken(sysInfo));
|
||||
}
|
||||
|
||||
return R.fail("authentication type not supported");
|
||||
return R.fail("auth.authentication.type.not.supported");
|
||||
}
|
||||
|
||||
@DeleteMapping("logout")
|
||||
@@ -106,7 +106,7 @@ public class TokenController {
|
||||
sysLoginService.register(form);
|
||||
return R.ok();
|
||||
}
|
||||
return R.fail("authentication type not supported");
|
||||
return R.fail("auth.authentication.type.not.supported");
|
||||
}
|
||||
|
||||
@PostMapping("checkRepeat")
|
||||
@@ -116,12 +116,12 @@ public class TokenController {
|
||||
return R.ok();
|
||||
}
|
||||
if ("sys".equals(form.getAuthType())) {}
|
||||
return R.fail("authentication type not supported");
|
||||
return R.fail("auth.authentication.type.not.supported");
|
||||
}
|
||||
|
||||
@GetMapping("health")
|
||||
public R<?> health(HttpServletRequest request) {
|
||||
// TODO: check health
|
||||
return R.ok(null,"Auth is healthy");
|
||||
// check health
|
||||
return R.ok(null,"auth.is.healthy");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.wfc.common.core.enums.UserStatus;
|
||||
import org.wfc.common.core.exception.ServiceException;
|
||||
import org.wfc.common.core.text.Convert;
|
||||
import org.wfc.common.core.utils.DateUtils;
|
||||
import org.wfc.common.core.utils.MessageUtils;
|
||||
import org.wfc.common.core.utils.StringUtils;
|
||||
import org.wfc.common.core.utils.ip.IpUtils;
|
||||
import org.wfc.common.core.web.form.RegisterBody;
|
||||
@@ -45,33 +46,34 @@ public class SysLoginService {
|
||||
public LoginUser<SysUser> login(String username, String password) {
|
||||
// 用户名或密码为空 错误
|
||||
if (StringUtils.isAnyBlank(username, password)) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "user.password.not.blank");
|
||||
throw new ServiceException("user.password.not.blank");
|
||||
}
|
||||
// 密码如果不在指定范围内 错误
|
||||
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|
||||
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
|
||||
throw new ServiceException("用户密码不在指定范围");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "auth.password.not.in.specified.range");
|
||||
throw new ServiceException("auth.password.not.in.specified.range");
|
||||
}
|
||||
// 用户名不在指定范围内 错误
|
||||
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
||||
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
|
||||
throw new ServiceException("用户名不在指定范围");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "auth.username.not.in.specified.range");
|
||||
throw new ServiceException("auth.username.not.in.specified.range");
|
||||
}
|
||||
// IP黑名单校验
|
||||
String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
|
||||
if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "很遗憾,访问IP已被列入系统黑名单");
|
||||
throw new ServiceException("很遗憾,访问IP已被列入系统黑名单");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "auth.ip.in.blacklist");
|
||||
throw new ServiceException("auth.ip.in.blacklist");
|
||||
}
|
||||
// 查询用户信息
|
||||
R<LoginUser<SysUser>> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
|
||||
|
||||
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
|
||||
throw new ServiceException("登录用户:" + username + " 不存在");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "auth.username.not.exists");
|
||||
String msg = MessageUtils.message("auth.username.not.exists", username);
|
||||
throw new ServiceException(msg);
|
||||
}
|
||||
|
||||
if (R.FAIL == userResult.getCode()) {
|
||||
@@ -81,15 +83,17 @@ public class SysLoginService {
|
||||
LoginUser<SysUser> userInfo = userResult.getData();
|
||||
SysUser user = userInfo.getUser();
|
||||
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
|
||||
throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "auth.user.deleted");
|
||||
String msg = MessageUtils.message("auth.user.deleted", username);
|
||||
throw new ServiceException(msg);
|
||||
}
|
||||
if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
|
||||
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "auth.user.blocked.contact.admin");
|
||||
String msg = MessageUtils.message("auth.user.blocked", username);
|
||||
throw new ServiceException(msg);
|
||||
}
|
||||
passwordService.validate(user, password);
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "auth.login.success");
|
||||
recordLoginInfo(user.getUserId());
|
||||
return userInfo;
|
||||
}
|
||||
@@ -110,7 +114,7 @@ public class SysLoginService {
|
||||
}
|
||||
|
||||
public void logout(String loginName) {
|
||||
recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
|
||||
recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "auth.logout.success");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,11 +129,11 @@ public class SysLoginService {
|
||||
}
|
||||
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
||||
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
|
||||
throw new ServiceException("账户长度必须在2到20个字符之间");
|
||||
throw new ServiceException("auth.username.must.in.specified.range");
|
||||
}
|
||||
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|
||||
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
|
||||
throw new ServiceException("密码长度必须在5到20个字符之间");
|
||||
throw new ServiceException("auth.password.must.in.specified.range");
|
||||
}
|
||||
|
||||
// 注册用户信息
|
||||
@@ -149,6 +153,6 @@ public class SysLoginService {
|
||||
if (R.FAIL == registerResult.getCode()) {
|
||||
throw new ServiceException(registerResult.getMsg());
|
||||
}
|
||||
recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
|
||||
recordLogService.recordLogininfor(username, Constants.REGISTER, "auth.register.success");
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.wfc.common.core.constant.CacheConstants;
|
||||
import org.wfc.common.core.constant.Constants;
|
||||
import org.wfc.common.core.exception.ServiceException;
|
||||
import org.wfc.common.core.exception.user.UserPasswordNotMatchException;
|
||||
import org.wfc.common.core.utils.MessageUtils;
|
||||
import org.wfc.common.redis.service.RedisService;
|
||||
import org.wfc.common.security.utils.SecurityUtils;
|
||||
import org.wfc.system.api.domain.SysUser;
|
||||
@@ -54,7 +55,7 @@ public class SysPasswordService
|
||||
|
||||
if (retryCount >= Integer.valueOf(maxRetryCount).intValue())
|
||||
{
|
||||
String errMsg = String.format("密码输入错误%s次,帐户锁定%s分钟", maxRetryCount, lockTime);
|
||||
String errMsg = MessageUtils.message("auth.password.retry.limit.count", maxRetryCount, lockTime);
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL,errMsg);
|
||||
throw new ServiceException(errMsg);
|
||||
}
|
||||
@@ -62,7 +63,8 @@ public class SysPasswordService
|
||||
if (!matches(user, password))
|
||||
{
|
||||
retryCount = retryCount + 1;
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, String.format("密码输入错误%s次", retryCount));
|
||||
String errMsg = MessageUtils.message("auth.password.retry.limit.exceed", retryCount);
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, errMsg);
|
||||
redisService.setCacheObject(getCacheKey(username), retryCount, lockTime, TimeUnit.MINUTES);
|
||||
throw new UserPasswordNotMatchException();
|
||||
}
|
||||
|
||||
@@ -12,6 +12,7 @@ import org.wfc.common.core.enums.UserStatus;
|
||||
import org.wfc.common.core.exception.ServiceException;
|
||||
import org.wfc.common.core.text.Convert;
|
||||
import org.wfc.common.core.utils.DateUtils;
|
||||
import org.wfc.common.core.utils.MessageUtils;
|
||||
import org.wfc.common.core.utils.StringUtils;
|
||||
import org.wfc.common.core.utils.ip.IpUtils;
|
||||
import org.wfc.common.core.web.form.RegisterBody;
|
||||
@@ -45,33 +46,34 @@ public class ULoginService {
|
||||
public LoginUser<UUser> login(String username, String password) {
|
||||
// 用户名或密码为空 错误
|
||||
if (StringUtils.isAnyBlank(username, password)) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "user.password.not.blank");
|
||||
throw new ServiceException("user.password.not.blank");
|
||||
}
|
||||
// 密码如果不在指定范围内 错误
|
||||
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|
||||
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户密码不在指定范围");
|
||||
throw new ServiceException("用户密码不在指定范围");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "auth.password.not.in.specified.range");
|
||||
throw new ServiceException("auth.password.not.in.specified.range");
|
||||
}
|
||||
// 用户名不在指定范围内 错误
|
||||
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
||||
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
|
||||
throw new ServiceException("用户名不在指定范围");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "auth.username.not.in.specified.range");
|
||||
throw new ServiceException("auth.username.not.in.specified.range");
|
||||
}
|
||||
// IP黑名单校验
|
||||
String blackStr = Convert.toStr(redisService.getCacheObject(CacheConstants.SYS_LOGIN_BLACKIPLIST));
|
||||
if (IpUtils.isMatchedIp(blackStr, IpUtils.getIpAddr())) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "很遗憾,访问IP已被列入系统黑名单");
|
||||
throw new ServiceException("很遗憾,访问IP已被列入系统黑名单");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "auth.ip.in.blacklist");
|
||||
throw new ServiceException("auth.ip.in.blacklist");
|
||||
}
|
||||
// 查询用户信息
|
||||
R<LoginUser<UUser>> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
|
||||
|
||||
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
|
||||
throw new ServiceException("登录用户:" + username + " 不存在");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "auth.username.not.exists");
|
||||
String msg = MessageUtils.message("auth.username.not.exists", username);
|
||||
throw new ServiceException(msg);
|
||||
}
|
||||
|
||||
if (R.FAIL == userResult.getCode()) {
|
||||
@@ -81,15 +83,17 @@ public class ULoginService {
|
||||
LoginUser<UUser> userInfo = userResult.getData();
|
||||
UUser user = userInfo.getUser();
|
||||
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
|
||||
throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "auth.user.deleted");
|
||||
String msg = MessageUtils.message("auth.user.deleted", username);
|
||||
throw new ServiceException(msg);
|
||||
}
|
||||
if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
|
||||
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "auth.user.blocked.contact.admin");
|
||||
String msg = MessageUtils.message("auth.user.blocked", username);
|
||||
throw new ServiceException(msg);
|
||||
}
|
||||
passwordService.validate(user, password);
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "登录成功");
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_SUCCESS, "auth.login.success");
|
||||
recordLoginInfo(user.getUserId());
|
||||
return userInfo;
|
||||
}
|
||||
@@ -110,7 +114,7 @@ public class ULoginService {
|
||||
}
|
||||
|
||||
public void logout(String loginName) {
|
||||
recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
|
||||
recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "auth.logout.success");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -125,11 +129,11 @@ public class ULoginService {
|
||||
}
|
||||
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|
||||
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
|
||||
throw new ServiceException("账户长度必须在2到20个字符之间");
|
||||
throw new ServiceException("auth.username.must.in.specified.range");
|
||||
}
|
||||
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|
||||
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
|
||||
throw new ServiceException("密码长度必须在5到20个字符之间");
|
||||
throw new ServiceException("auth.password.must.in.specified.range");
|
||||
}
|
||||
|
||||
// 注册用户信息
|
||||
@@ -149,7 +153,7 @@ public class ULoginService {
|
||||
if (R.FAIL == registerResult.getCode()) {
|
||||
throw new ServiceException(registerResult.getMsg());
|
||||
}
|
||||
recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
|
||||
recordLogService.recordLogininfor(username, Constants.REGISTER, "auth.register.success");
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -166,7 +170,7 @@ public class ULoginService {
|
||||
}
|
||||
Boolean isRepeat = result.getData();
|
||||
if (isRepeat) {
|
||||
throw new ServiceException("用户已存在");
|
||||
throw new ServiceException("auth.user.registered");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -6,6 +6,7 @@ import org.wfc.common.core.constant.CacheConstants;
|
||||
import org.wfc.common.core.constant.Constants;
|
||||
import org.wfc.common.core.exception.ServiceException;
|
||||
import org.wfc.common.core.exception.user.UserPasswordNotMatchException;
|
||||
import org.wfc.common.core.utils.MessageUtils;
|
||||
import org.wfc.common.redis.service.RedisService;
|
||||
import org.wfc.common.security.utils.SecurityUtils;
|
||||
import org.wfc.user.api.domain.UUser;
|
||||
@@ -54,7 +55,7 @@ public class UPasswordService
|
||||
|
||||
if (retryCount >= Integer.valueOf(maxRetryCount).intValue())
|
||||
{
|
||||
String errMsg = String.format("密码输入错误%s次,帐户锁定%s分钟", maxRetryCount, lockTime);
|
||||
String errMsg = MessageUtils.message("auth.password.retry.limit.count", maxRetryCount, lockTime);
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL,errMsg);
|
||||
throw new ServiceException(errMsg);
|
||||
}
|
||||
@@ -62,7 +63,8 @@ public class UPasswordService
|
||||
if (!matches(user, password))
|
||||
{
|
||||
retryCount = retryCount + 1;
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, String.format("密码输入错误%s次", retryCount));
|
||||
String errMsg = MessageUtils.message("auth.password.retry.limit.exceed", retryCount);
|
||||
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, errMsg);
|
||||
redisService.setCacheObject(getCacheKey(username), retryCount, lockTime, TimeUnit.MINUTES);
|
||||
throw new UserPasswordNotMatchException();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user