2
0

fix: 认证模块添加用户检查重复接口

This commit is contained in:
TsMask
2024-11-30 16:24:42 +08:00
parent c362641c21
commit ed35293766
5 changed files with 119 additions and 121 deletions

View File

@@ -37,6 +37,16 @@ public interface RemoteUUserService
@PostMapping("/user/register")
public R<Boolean> registerUserInfo(@RequestBody UUser user, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
* 检查重复用户信息
*
* @param user 用户信息
* @param source 请求来源
* @return 结果
*/
@PostMapping("/user/checkRepeat")
public R<Boolean> checkRepeat(@RequestBody UUser user, @RequestHeader(SecurityConstants.FROM_SOURCE) String source);
/**
* 记录用户登录IP地址和登录时间
*

View File

@@ -37,6 +37,12 @@ public class RemoteUUserFallbackFactory implements FallbackFactory<RemoteUUserSe
return R.fail("注册用户失败:" + throwable.getMessage());
}
@Override
public R<Boolean> checkRepeat(UUser user, String source)
{
return R.fail("检查重复失败:" + throwable.getMessage());
}
@Override
public R<Boolean> recordUserLogin(UUser user, String source)
{

View File

@@ -23,12 +23,11 @@ import javax.servlet.http.HttpServletRequest;
/**
* token 控制
*
*
* @author wfc
*/
@RestController
public class TokenController
{
public class TokenController {
@Autowired
private TokenService tokenService;
@@ -39,14 +38,14 @@ public class TokenController
private ULoginService uLoginService;
@PostMapping("login")
public R<?> login(@RequestBody LoginBody form)
{
if ("u".equals(form.getAuthType())){
public R<?> login(@RequestBody LoginBody form) {
if ("u".equals(form.getAuthType())) {
// 用户登录
LoginUser<UUser> userInfo = uLoginService.login(form.getUsername(), form.getPassword());
// 获取登录token
return R.ok(tokenService.createToken(userInfo));
} if ("sys".equals(form.getAuthType())){
}
if ("sys".equals(form.getAuthType())) {
// 用户登录
LoginUser<SysUser> sysInfo = sysLoginService.login(form.getUsername(), form.getPassword());
// 获取登录token
@@ -57,11 +56,9 @@ public class TokenController
}
@DeleteMapping("logout")
public R<?> logout(HttpServletRequest request)
{
public R<?> logout(HttpServletRequest request) {
String token = SecurityUtils.getToken(request);
if (StringUtils.isNotEmpty(token))
{
if (StringUtils.isNotEmpty(token)) {
String username = JwtUtils.getUserName(token);
// 删除用户缓存记录
AuthUtil.logoutByToken(token);
@@ -72,11 +69,9 @@ public class TokenController
}
@PostMapping("refresh")
public R<?> refresh(HttpServletRequest request)
{
public R<?> refresh(HttpServletRequest request) {
LoginUser loginUser = tokenService.getLoginUser(request);
if (StringUtils.isNotNull(loginUser))
{
if (StringUtils.isNotNull(loginUser)) {
// 刷新令牌有效期
tokenService.refreshToken(loginUser);
return R.ok();
@@ -85,11 +80,25 @@ public class TokenController
}
@PostMapping("register")
public R<?> register(@RequestBody RegisterBody registerBody)
{
sysLoginService.register(registerBody);
return R.ok();
public R<?> register(@RequestBody RegisterBody form) {
if ("u".equals(form.getAuthType())) {
uLoginService.register(form);
return R.ok();
}
if ("sys".equals(form.getAuthType())) {
sysLoginService.register(form);
return R.ok();
}
return R.fail("authentication type not supported");
}
@PostMapping("checkRepeat")
public R<?> checkRepeat(@RequestBody RegisterBody form) {
if ("u".equals(form.getAuthType())) {
uLoginService.checkRepeat(form);
return R.ok();
}
if ("sys".equals(form.getAuthType())) {}
return R.fail("authentication type not supported");
}
}

View File

@@ -1,12 +1,9 @@
package org.wfc.auth.service;
import cn.hutool.core.util.StrUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.wfc.common.core.constant.CacheConstants;
import org.wfc.common.core.constant.Constants;
import org.wfc.common.core.constant.GlobalConstants;
import org.wfc.common.core.constant.SecurityConstants;
import org.wfc.common.core.constant.UserConstants;
import org.wfc.common.core.domain.LoginUser;
@@ -22,23 +19,17 @@ import org.wfc.common.redis.service.RedisService;
import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.system.api.RemoteUserService;
import org.wfc.system.api.domain.SysUser;
import org.wfc.user.api.RemoteUUserService;
import org.wfc.user.api.domain.UUser;
/**
* 登录校验方法
*
*
* @author wfc
*/
@Component
public class SysLoginService
{
public class SysLoginService {
@Autowired
private RemoteUserService remoteUserService;
@Autowired
private RemoteUUserService remoteUUserService;
@Autowired
private SysPasswordService passwordService;
@@ -51,58 +42,49 @@ public class SysLoginService
/**
* 登录
*/
public LoginUser<SysUser> login(String username, String password)
{
public LoginUser<SysUser> login(String username, String password) {
// 用户名或密码为空 错误
if (StringUtils.isAnyBlank(username, password))
{
if (StringUtils.isAnyBlank(username, password)) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
throw new ServiceException("user.password.not.blank");
}
// 密码如果不在指定范围内 错误
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, "用户密码不在指定范围");
throw new ServiceException("用户密码不在指定范围");
}
// 用户名不在指定范围内 错误
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|| username.length() > UserConstants.USERNAME_MAX_LENGTH)
{
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
throw new ServiceException("用户名不在指定范围");
}
// IP黑名单校验
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已被列入系统黑名单");
throw new ServiceException("很遗憾访问IP已被列入系统黑名单");
}
// 查询用户信息
R<LoginUser<SysUser>> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
{
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
throw new ServiceException("登录用户:" + username + " 不存在");
}
if (R.FAIL == userResult.getCode())
{
if (R.FAIL == userResult.getCode()) {
throw new ServiceException(userResult.getMsg());
}
LoginUser<SysUser> userInfo = userResult.getData();
SysUser user = userInfo.getUser();
if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
}
if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{
if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
}
@@ -117,8 +99,7 @@ public class SysLoginService
*
* @param userId 用户ID
*/
public void recordLoginInfo(Long userId)
{
public void recordLoginInfo(Long userId) {
SysUser sysUser = new SysUser();
sysUser.setUserId(userId);
// 更新用户登录IP
@@ -128,31 +109,26 @@ public class SysLoginService
remoteUserService.recordUserLogin(sysUser, SecurityConstants.INNER);
}
public void logout(String loginName)
{
public void logout(String loginName) {
recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
}
/**
* 注册
*/
public void register(RegisterBody registerBody)
{
public void register(RegisterBody registerBody) {
String username = registerBody.getUsername();
String password = registerBody.getPassword();
// 用户名或密码为空 错误
if (StringUtils.isAnyBlank(username, password))
{
if (StringUtils.isAnyBlank(username, password)) {
throw new ServiceException("user.password.not.blank");
}
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|| username.length() > UserConstants.USERNAME_MAX_LENGTH)
{
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
throw new ServiceException("账户长度必须在2到20个字符之间");
}
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH)
{
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
throw new ServiceException("密码长度必须在5到20个字符之间");
}
@@ -161,6 +137,7 @@ public class SysLoginService
sysUser.setUserName(username);
sysUser.setNickName(username);
sysUser.setPassword(SecurityUtils.encryptPassword(password));
// 额外携带
sysUser.setAge(registerBody.getAge());
sysUser.setAddress(registerBody.getAddress());
sysUser.setEmail(registerBody.getEmail());
@@ -168,25 +145,9 @@ public class SysLoginService
sysUser.setSex(registerBody.getSex());
sysUser.setPhonenumber(registerBody.getPhonenumber());
if ("u".equals(registerBody.getAuthType())){
UUser uUser = new UUser();
BeanUtils.copyProperties(sysUser, uUser);
R<?> registerResult = remoteUUserService.registerUserInfo(uUser, SecurityConstants.INNER);
if (R.FAIL == registerResult.getCode())
{
throw new ServiceException(registerResult.getMsg());
}
} if ("sys".equals(registerBody.getAuthType())){
R<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
if (R.FAIL == registerResult.getCode())
{
throw new ServiceException(registerResult.getMsg());
}
}
// 注册成功删除redis
if (StrUtil.isNotBlank(registerBody.getEmail())) {
redisService.deleteObject(GlobalConstants.CAPTCHA_CODE_KEY + registerBody.getEmail());
R<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
if (R.FAIL == registerResult.getCode()) {
throw new ServiceException(registerResult.getMsg());
}
recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
}

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.constant.SecurityConstants;
import org.wfc.common.core.constant.UserConstants;
import org.wfc.common.core.domain.LoginUser;
import org.wfc.common.core.domain.R;
import org.wfc.common.core.enums.UserStatus;
import org.wfc.common.core.exception.ServiceException;
@@ -13,20 +14,19 @@ import org.wfc.common.core.text.Convert;
import org.wfc.common.core.utils.DateUtils;
import org.wfc.common.core.utils.StringUtils;
import org.wfc.common.core.utils.ip.IpUtils;
import org.wfc.common.core.web.form.RegisterBody;
import org.wfc.common.redis.service.RedisService;
import org.wfc.common.security.utils.SecurityUtils;
import org.wfc.common.core.domain.LoginUser;
import org.wfc.user.api.RemoteUUserService;
import org.wfc.user.api.domain.UUser;
/**
* 用户平台-登录校验方法
*
*
* @author wfc
*/
@Component
public class ULoginService
{
public class ULoginService {
@Autowired
private RemoteUUserService remoteUserService;
@@ -42,58 +42,49 @@ public class ULoginService
/**
* 登录
*/
public LoginUser<UUser> login(String username, String password)
{
public LoginUser<UUser> login(String username, String password) {
// 用户名或密码为空 错误
if (StringUtils.isAnyBlank(username, password))
{
if (StringUtils.isAnyBlank(username, password)) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户/密码必须填写");
throw new ServiceException("user.password.not.blank");
}
// 密码如果不在指定范围内 错误
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, "用户密码不在指定范围");
throw new ServiceException("用户密码不在指定范围");
}
// 用户名不在指定范围内 错误
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|| username.length() > UserConstants.USERNAME_MAX_LENGTH)
{
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户名不在指定范围");
throw new ServiceException("用户名不在指定范围");
}
// IP黑名单校验
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已被列入系统黑名单");
throw new ServiceException("很遗憾访问IP已被列入系统黑名单");
}
// 查询用户信息
R<LoginUser<UUser>> userResult = remoteUserService.getUserInfo(username, SecurityConstants.INNER);
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData()))
{
if (StringUtils.isNull(userResult) || StringUtils.isNull(userResult.getData())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "登录用户不存在");
throw new ServiceException("登录用户:" + username + " 不存在");
}
if (R.FAIL == userResult.getCode())
{
if (R.FAIL == userResult.getCode()) {
throw new ServiceException(userResult.getMsg());
}
LoginUser<UUser> userInfo = userResult.getData();
UUser user = userInfo.getUser();
if (UserStatus.DELETED.getCode().equals(user.getDelFlag()))
{
if (UserStatus.DELETED.getCode().equals(user.getDelFlag())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "对不起,您的账号已被删除");
throw new ServiceException("对不起,您的账号:" + username + " 已被删除");
}
if (UserStatus.DISABLE.getCode().equals(user.getStatus()))
{
if (UserStatus.DISABLE.getCode().equals(user.getStatus())) {
recordLogService.recordLogininfor(username, Constants.LOGIN_FAIL, "用户已停用,请联系管理员");
throw new ServiceException("对不起,您的账号:" + username + " 已停用");
}
@@ -108,8 +99,7 @@ public class ULoginService
*
* @param userId 用户ID
*/
public void recordLoginInfo(Long userId)
{
public void recordLoginInfo(Long userId) {
UUser sysUser = new UUser();
sysUser.setUserId(userId);
// 更新用户登录IP
@@ -119,43 +109,65 @@ public class ULoginService
remoteUserService.recordUserLogin(sysUser, SecurityConstants.INNER);
}
public void logout(String loginName)
{
public void logout(String loginName) {
recordLogService.recordLogininfor(loginName, Constants.LOGOUT, "退出成功");
}
/**
* 注册
*/
public void register(String username, String password)
{
public void register(RegisterBody registerBody) {
String username = registerBody.getUsername();
String password = registerBody.getPassword();
// 用户名或密码为空 错误
if (StringUtils.isAnyBlank(username, password))
{
if (StringUtils.isAnyBlank(username, password)) {
throw new ServiceException("user.password.not.blank");
}
if (username.length() < UserConstants.USERNAME_MIN_LENGTH
|| username.length() > UserConstants.USERNAME_MAX_LENGTH)
{
|| username.length() > UserConstants.USERNAME_MAX_LENGTH) {
throw new ServiceException("账户长度必须在2到20个字符之间");
}
if (password.length() < UserConstants.PASSWORD_MIN_LENGTH
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH)
{
|| password.length() > UserConstants.PASSWORD_MAX_LENGTH) {
throw new ServiceException("密码长度必须在5到20个字符之间");
}
// 注册用户信息
UUser sysUser = new UUser();
sysUser.setUserName(username);
sysUser.setNickName(username);
sysUser.setPassword(SecurityUtils.encryptPassword(password));
R<?> registerResult = remoteUserService.registerUserInfo(sysUser, SecurityConstants.INNER);
UUser user = new UUser();
user.setUserName(username);
user.setNickName(username);
user.setPassword(SecurityUtils.encryptPassword(password));
// 额外携带
user.setFullName(registerBody.getFullName());
user.setAge(registerBody.getAge());
user.setSex(registerBody.getSex());
user.setAddress(registerBody.getAddress());
user.setEmail(registerBody.getEmail());
user.setPhonenumber(registerBody.getPhonenumber());
if (R.FAIL == registerResult.getCode())
{
R<?> registerResult = remoteUserService.registerUserInfo(user, SecurityConstants.INNER);
if (R.FAIL == registerResult.getCode()) {
throw new ServiceException(registerResult.getMsg());
}
recordLogService.recordLogininfor(username, Constants.REGISTER, "注册成功");
}
/**
* 检查重复
*/
public void checkRepeat(RegisterBody registerBody) {
UUser user = new UUser();
user.setUserName(registerBody.getUsername());
user.setPhonenumber(registerBody.getPhonenumber());
user.setEmail(registerBody.getEmail());
R<Boolean> result = remoteUserService.checkRepeat(user, SecurityConstants.INNER );
if (R.FAIL == result.getCode()) {
throw new ServiceException(result.getMsg());
}
Boolean isRepeat = result.getData();
if (isRepeat) {
throw new ServiceException("用户已存在");
}
}
}