2
0

feat: auth support i18n message

This commit is contained in:
zhangsz
2025-01-22 16:31:51 +08:00
parent b7f60a6a08
commit 8e7851eebe
11 changed files with 143 additions and 50 deletions

View File

@@ -2,8 +2,13 @@ package org.wfc.system.api.domain;
import java.util.Date;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.github.pagehelper.util.StringUtil;
import org.aspectj.bridge.Message;
import org.aspectj.bridge.MessageUtil;
import org.wfc.common.core.annotation.Excel;
import org.wfc.common.core.annotation.Excel.ColumnType;
import org.wfc.common.core.utils.MessageUtils;
import org.wfc.common.core.web.domain.BaseEntity;
/**
@@ -82,8 +87,18 @@ public class SysLogininfor extends BaseEntity
public String getMsg()
{
return msg;
String mes = this.msg;
if (!StringUtil.isEmpty(msg))
{
mes = MessageUtils.message(msg);
}
return mes;
}
// public String getMsg()
// {
// return msg;
// }
public void setMsg(String msg)
{

View File

@@ -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");
}
}

View File

@@ -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");
}
}

View File

@@ -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");
}
}

View File

@@ -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();
}

View File

@@ -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");
}
}

View File

@@ -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();
}

View File

@@ -2,6 +2,8 @@ package org.wfc.common.core.domain;
import java.io.Serializable;
import org.wfc.common.core.constant.Constants;
import org.wfc.common.core.utils.MessageUtils;
import org.wfc.common.core.utils.StringUtils;
/**
* 响应信息主体
@@ -85,8 +87,17 @@ public class R<T> implements Serializable
public String getMsg()
{
return msg;
String mes = null;
if (!StringUtils.isEmpty(msg)) {
mes = MessageUtils.message(msg);
}
return mes;
}
// public String getMsg()
// {
// return msg;
// }
public void setMsg(String msg)
{

View File

@@ -135,6 +135,24 @@ gateway.internal.server.error=Internal server error
gateway.service.not.found=Service not found
gateway.request.address.forbidden=Request address is not allowed to access
gateway.request.limit=Request limit exceeded, please try again later
## wfc-auth
auth.invalid.login.parameter=Invalid Login Parameter
auth.authentication.type.not.supported=authentication type not supported
auth.is.healthy=Auth is healthy
auth.password.not.in.specified.range=User password not in specified range
auth.username.not.in.specified.range=Username not in specified range
auth.ip.in.blacklist=Sorry, the access IP has been blacklisted by the system
auth.username.not.exists=Login user: {0} does not exist
auth.user.deleted=Sorry, your account{0} has been deleted
auth.user.blocked=Sorry, your account: {0} has been disabled
auth.login.success=Login successful
auth.logout.success=Logout successful
auth.username.must.in.specified.range=Username length must be between 2 and 20 characters
auth.password.must.in.specified.range=Password length must be between 5 and 50 characters
auth.register.success=Register successful
auth.user.registered=The user has been registered
auth.password.retry.limit.count=Password input error {0} times, account locked for {1} minutes
auth.password.retry.limit.exceed=Password input error {0} times
## wfc-common
common.operate.success=Operation successful

View File

@@ -135,6 +135,24 @@ gateway.internal.server.error=Internal server error
gateway.service.not.found=Service not found
gateway.request.address.forbidden=Request address is not allowed to access
gateway.request.limit=Request limit exceeded, please try again later
## wfc-auth
auth.invalid.login.parameter=Invalid Login Parameter
auth.authentication.type.not.supported=authentication type not supported
auth.is.healthy=Auth is healthy
auth.password.not.in.specified.range=User password not in specified range
auth.username.not.in.specified.range=Username not in specified range
auth.ip.in.blacklist=Sorry, the access IP has been blacklisted by the system
auth.username.not.exists=Login user: {0} does not exist
auth.user.deleted=Sorry, your account{0} has been deleted
auth.user.blocked=Sorry, your account: {0} has been disabled
auth.login.success=Login successful
auth.logout.success=Logout successful
auth.username.must.in.specified.range=Username length must be between 2 and 20 characters
auth.password.must.in.specified.range=Password length must be between 5 and 50 characters
auth.register.success=Register successful
auth.user.registered=The user has been registered
auth.password.retry.limit.count=Password input error {0} times, account locked for {1} minutes
auth.password.retry.limit.exceed=Password input error {0} times
## wfc-common
common.operate.success=Operation successful

View File

@@ -136,6 +136,25 @@ gateway.internal.server.error=内部服务器错误
gateway.service.not.found=服务未找到
gateway.request.address.forbidden=请求地址不允许访问
gateway.request.limit=请求超过最大数,请稍候再试
## wfc-auth
auth.invalid.login.parameter=无效的登录参数
auth.authentication.type.not.supported=不支持的认证类型
auth.is.healthy=认证服务健康
auth.password.not.in.specified.range=用户密码不在指定范围
auth.username.not.in.specified.range=用户名不在指定范围
auth.ip.in.blacklist=很遗憾访问IP已被列入系统黑名单
auth.username.not.exists=登录用户:{0} 不存在
auth.user.deleted=对不起,您的账号:{0} 已被删除
auth.user.blocked.contact.admin=用户已停用,请联系管理员
auth.user.blocked=对不起,您的账号:{0} 已停用
auth.login.success=登录成功
auth.logout.success=退出成功
auth.username.must.in.specified.range=账户长度必须在2到20个字符之间
auth.password.must.in.specified.range=密码长度必须在5到20个字符之间
auth.register.success=注册成功
auth.user.registered=用户已存在
auth.password.retry.limit.count=密码输入错误{0}次,帐户锁定{1}分钟
auth.password.retry.limit.exceed=密码输入错误{0}次
## wfc-common
common.operate.success=操作成功