From a70919b4f7baab856125f36e5bd41f5ee81be680 Mon Sep 17 00:00:00 2001 From: huliguo <2023611923@qq.com> Date: 星期二, 13 五月 2025 09:41:35 +0800 Subject: [PATCH] 修改年份切换字段不为必填 --- src/main/java/com/cl/util/LoginAttemptService.java | 60 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 60 insertions(+), 0 deletions(-) diff --git a/src/main/java/com/cl/util/LoginAttemptService.java b/src/main/java/com/cl/util/LoginAttemptService.java new file mode 100644 index 0000000..8b1e667 --- /dev/null +++ b/src/main/java/com/cl/util/LoginAttemptService.java @@ -0,0 +1,60 @@ +package com.cl.util; + +import org.springframework.context.annotation.Bean; +import org.springframework.scheduling.annotation.Scheduled; + +import java.util.Map; +import java.util.concurrent.ConcurrentHashMap; + +// 使用ConcurrentHashMap作为临时存储 + +public class LoginAttemptService { + private static final int MAX_ATTEMPTS = 5; + private static final long LOCK_TIME_MS = 5 * 60 * 1000; // 5分钟 + + private final Map<String, LoginAttempt> attemptsCache = new ConcurrentHashMap<>(); + + /** + * 是否被锁了 + */ + public boolean isLocked(String phone) { + LoginAttempt attempt = attemptsCache.get(phone); + return attempt != null + && attempt.getLockCount() >= MAX_ATTEMPTS + && System.currentTimeMillis() < attempt.getLastAttemptTime() + LOCK_TIME_MS; + } + + /** + * 添加密码错误记录 + */ + public void recordFailedAttempt(String phone) { + attemptsCache.compute(phone, (k, v) -> { + long now = System.currentTimeMillis(); + if (v == null) { + return new LoginAttempt(1, now); + } + + // 如果上次尝试超过锁定时间,则重置 + if (now - v.getLastAttemptTime() > LOCK_TIME_MS) { + return new LoginAttempt(1, now); + } + + return new LoginAttempt(v.getLockCount() + 1, now); + }); + } + + /** + * 清除 + */ + public void clearAttempts(String phone) { + attemptsCache.remove(phone); + } + + @Scheduled(fixedRate = 60 * 60 * 1000) // 每小时清理一次过期记录 + public void cleanupExpiredAttempts() { + long now = System.currentTimeMillis(); + attemptsCache.entrySet().removeIf(entry -> + now - entry.getValue().getLastAttemptTime() > LOCK_TIME_MS * 2 + ); + } +} \ No newline at end of file -- Gitblit v1.7.1