修复:补齐登录状态与自动停用规则
This commit is contained in:
+1
-1
@@ -85,6 +85,7 @@ public class AdminUserController {
|
||||
@RequirePermission("user:disable")
|
||||
public Result<Boolean> updateUserStatus(HttpServletRequest httpRequest, @PathVariable Long id, @RequestBody StatusRequest request) {
|
||||
UserDO currentUser = adminScopeHelper.getCurrentUser(httpRequest);
|
||||
adminScopeHelper.requirePlatformAdmin(currentUser, "仅系统管理员可以停用用户");
|
||||
UserVO targetUser = userService.getUserById(id);
|
||||
if (targetUser == null) {
|
||||
return Result.success(false);
|
||||
@@ -93,7 +94,6 @@ public class AdminUserController {
|
||||
if (Integer.valueOf(4).equals(targetUser.getType())) {
|
||||
throw new RuntimeException("管理员账号不允许停用");
|
||||
}
|
||||
adminScopeHelper.ensureAssignableUserType(currentUser, targetUser.getType());
|
||||
boolean success = userService.updateUserStatus(id, request.getStatus());
|
||||
return Result.success(success);
|
||||
}
|
||||
|
||||
+1
-1
@@ -225,7 +225,7 @@ public class UserController {
|
||||
String nickname = exists ? userDO.getNickname() : null;
|
||||
String message = exists
|
||||
? (nickname != null && !nickname.isEmpty() ? "该账号已注册,昵称:" + nickname : "该账号已注册,可继续登录")
|
||||
: "该账号未注册或已删除";
|
||||
: "该账号不存在";
|
||||
return Result.success(new LoginAccountAvailabilityVO(exists, nickname, message));
|
||||
}
|
||||
|
||||
|
||||
+65
-6
@@ -29,12 +29,14 @@ import com.xiaowang.yuminailcore.vo.ShopSimpleVO;
|
||||
import com.xiaowang.yuminailcore.vo.UserVO;
|
||||
import com.xiaowang.yuminailcore.vo.ShopVO;
|
||||
import lombok.RequiredArgsConstructor;
|
||||
import org.springframework.data.redis.core.StringRedisTemplate;
|
||||
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
import org.springframework.util.StringUtils;
|
||||
|
||||
import java.math.BigDecimal;
|
||||
import java.time.Duration;
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
@@ -58,6 +60,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
private static final String VIP_LEVEL_NORMAL = "普通";
|
||||
private static final String PHONE_REGEX = "^1[3-9]\\d{9}$";
|
||||
private static final String DELETED_PHONE_MARKER = "_deleted_";
|
||||
private static final String LOGIN_FAIL_KEY_PREFIX = "user:login:failed:";
|
||||
private static final int MAX_LOGIN_FAILURES = 5;
|
||||
private static final Duration LOGIN_FAIL_EXPIRE = Duration.ofDays(7);
|
||||
private static final Long SYSTEM_PERMISSION_ASSIGNER_ID = 0L;
|
||||
private static final Set<String> MANAGED_DEFAULT_PERMISSION_CODES = PermissionPolicy.getManagedDefaultPermissionCodes();
|
||||
private static final Map<String, PermissionSeed> DEFAULT_PERMISSION_SEEDS = buildDefaultPermissionSeeds();
|
||||
@@ -71,6 +76,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
private final BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
|
||||
private final VerificationCodeService verificationCodeService;
|
||||
private final UserShopBindingSupport userShopBindingSupport;
|
||||
private final StringRedisTemplate redisTemplate;
|
||||
|
||||
private static Map<String, PermissionSeed> buildDefaultPermissionSeeds() {
|
||||
Map<String, PermissionSeed> seeds = new HashMap<>();
|
||||
@@ -417,6 +423,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
return false;
|
||||
}
|
||||
|
||||
clearLoginFailureCount(userDO.getAccount());
|
||||
userShopBindingSupport.deleteBindingsByUserId(id);
|
||||
LocalDateTime now = LocalDateTime.now();
|
||||
return userMapper.update(
|
||||
@@ -574,7 +581,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
.last("limit 1")
|
||||
);
|
||||
if (userDO == null) {
|
||||
return buildLoginShopSelection(List.of(), false, "该账号未注册或已删除");
|
||||
return buildLoginShopSelection(List.of(), false, "该账号不存在");
|
||||
}
|
||||
if (userDO.getStatus() != null && !Integer.valueOf(1).equals(userDO.getStatus())) {
|
||||
return buildLoginShopSelection(List.of(), false, "该账号已被禁用");
|
||||
@@ -651,7 +658,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
.last("limit 1")
|
||||
);
|
||||
if (userDO == null) {
|
||||
return buildLoginShopSelection(List.of(), false, "该账号未注册或已删除");
|
||||
return buildLoginShopSelection(List.of(), false, "该账号不存在");
|
||||
}
|
||||
if (userDO.getType() == null || userDO.getType() < 2) {
|
||||
return buildLoginShopSelection(List.of(), false, "该账号不是后台管理账号");
|
||||
@@ -692,10 +699,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
}
|
||||
|
||||
if (!passwordEncoder.matches(loginRequestDTO.getPassword(), userDO.getPassword())) {
|
||||
throw new RuntimeException("密码错误");
|
||||
handleLoginPasswordFailure(userDO);
|
||||
}
|
||||
|
||||
Long resolvedShopId = resolveUserLoginShopId(userDO, loginRequestDTO.getShopId());
|
||||
clearLoginFailureCount(userDO.getAccount());
|
||||
return buildLoginResponse(userDO, resolvedShopId, false);
|
||||
}
|
||||
|
||||
@@ -718,6 +726,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
userDO = registerByPhone(phoneLoginRequestDTO.getPhone(), phoneLoginRequestDTO.getShopId());
|
||||
} else {
|
||||
assertUserAvailable(userDO);
|
||||
clearLoginFailureCount(userDO.getAccount());
|
||||
}
|
||||
|
||||
Long resolvedShopId = resolveUserLoginShopId(userDO, phoneLoginRequestDTO.getShopId());
|
||||
@@ -766,7 +775,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
assertLoginAccountAvailable(userDO);
|
||||
|
||||
if (!passwordEncoder.matches(loginRequestDTO.getPassword(), userDO.getPassword())) {
|
||||
throw new RuntimeException("密码错误");
|
||||
handleLoginPasswordFailure(userDO);
|
||||
}
|
||||
|
||||
Integer userType = userDO.getType();
|
||||
@@ -777,6 +786,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
syncDefaultPermissions(userDO.getId(), userType);
|
||||
|
||||
Long resolvedShopId = resolveAdminLoginShopId(userDO, loginRequestDTO.getShopId());
|
||||
clearLoginFailureCount(userDO.getAccount());
|
||||
return buildLoginResponse(userDO, resolvedShopId, false);
|
||||
}
|
||||
|
||||
@@ -1064,7 +1074,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
return false;
|
||||
}
|
||||
Integer normalizedStatus = Integer.valueOf(1).equals(status) ? 1 : 0;
|
||||
return userMapper.update(
|
||||
boolean updated = userMapper.update(
|
||||
null,
|
||||
new LambdaUpdateWrapper<UserDO>()
|
||||
.eq(UserDO::getId, id)
|
||||
@@ -1072,6 +1082,13 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
.set(UserDO::getStatus, normalizedStatus)
|
||||
.set(UserDO::getUpdateTime, LocalDateTime.now())
|
||||
) > 0;
|
||||
if (updated && normalizedStatus == 1) {
|
||||
UserDO latestUser = userMapper.selectById(id);
|
||||
if (latestUser != null) {
|
||||
clearLoginFailureCount(latestUser.getAccount());
|
||||
}
|
||||
}
|
||||
return updated;
|
||||
}
|
||||
|
||||
@Override
|
||||
@@ -1198,13 +1215,55 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
|
||||
|
||||
private void assertLoginAccountAvailable(UserDO userDO) {
|
||||
if (userDO == null || Integer.valueOf(1).equals(userDO.getIsDelete())) {
|
||||
throw new RuntimeException("该账号未注册或已删除");
|
||||
throw new RuntimeException("该账号不存在");
|
||||
}
|
||||
if (userDO.getStatus() != null && !Integer.valueOf(1).equals(userDO.getStatus())) {
|
||||
throw new RuntimeException("该账号已被禁用");
|
||||
}
|
||||
}
|
||||
|
||||
private void handleLoginPasswordFailure(UserDO userDO) {
|
||||
if (userDO == null || !StringUtils.hasText(userDO.getAccount())) {
|
||||
throw new RuntimeException("密码错误");
|
||||
}
|
||||
String failKey = buildLoginFailKey(userDO.getAccount());
|
||||
Long failCount = redisTemplate.opsForValue().increment(failKey);
|
||||
redisTemplate.expire(failKey, LOGIN_FAIL_EXPIRE);
|
||||
long safeFailCount = failCount == null ? 1L : failCount;
|
||||
if (safeFailCount >= MAX_LOGIN_FAILURES) {
|
||||
disableUserByFailedPassword(userDO.getId());
|
||||
redisTemplate.delete(failKey);
|
||||
throw new RuntimeException("密码错误次数过多,账号已被禁用");
|
||||
}
|
||||
long remainingAttempts = MAX_LOGIN_FAILURES - safeFailCount;
|
||||
throw new RuntimeException("密码错误,还可尝试" + remainingAttempts + "次");
|
||||
}
|
||||
|
||||
private void disableUserByFailedPassword(Long userId) {
|
||||
if (userId == null) {
|
||||
return;
|
||||
}
|
||||
userMapper.update(
|
||||
null,
|
||||
new LambdaUpdateWrapper<UserDO>()
|
||||
.eq(UserDO::getId, userId)
|
||||
.eq(UserDO::getIsDelete, 0)
|
||||
.set(UserDO::getStatus, 0)
|
||||
.set(UserDO::getUpdateTime, LocalDateTime.now())
|
||||
);
|
||||
}
|
||||
|
||||
private void clearLoginFailureCount(String account) {
|
||||
if (!StringUtils.hasText(account)) {
|
||||
return;
|
||||
}
|
||||
redisTemplate.delete(buildLoginFailKey(account));
|
||||
}
|
||||
|
||||
private String buildLoginFailKey(String account) {
|
||||
return LOGIN_FAIL_KEY_PREFIX + account.trim();
|
||||
}
|
||||
|
||||
private void validateVerificationCode(String phone, String verificationCode) {
|
||||
if (!StringUtils.hasText(phone) || !StringUtils.hasText(verificationCode)) {
|
||||
throw new RuntimeException("验证码不能为空");
|
||||
|
||||
@@ -26,7 +26,7 @@ public final class PermissionPolicy {
|
||||
private static final List<String> SHOP_MANAGER_DEFAULT_PERMISSION_CODES = List.of(
|
||||
"home:view",
|
||||
"dashboard:view",
|
||||
"user:view", "user:create", "user:edit", "user:disable", "user:unbind", "user:reset_password",
|
||||
"user:view", "user:create", "user:edit", "user:unbind", "user:reset_password",
|
||||
"user:balance:view", "user:balance:manage", "user:vip:manage",
|
||||
"product:view", "product:create", "product:edit", "product:delete", "product:category:manage",
|
||||
"shop:view", "shop:edit", "shop:staff:manage",
|
||||
|
||||
@@ -156,7 +156,6 @@ INNER JOIN t_permission p
|
||||
'user:view',
|
||||
'user:create',
|
||||
'user:edit',
|
||||
'user:disable',
|
||||
'user:unbind',
|
||||
'user:reset_password',
|
||||
'user:balance:view',
|
||||
|
||||
@@ -1186,7 +1186,6 @@ INNER JOIN t_permission p
|
||||
'user:view',
|
||||
'user:create',
|
||||
'user:edit',
|
||||
'user:disable',
|
||||
'user:unbind',
|
||||
'user:reset_password',
|
||||
'user:balance:view',
|
||||
|
||||
Reference in New Issue
Block a user