Skip to content

Commit 77d318d

Browse files
committed
fix(middleware): 修复首次访问验证码逻辑
- 添加了文章页面访问路径的判断逻辑 - 修正了首次访问验证码检查条件,排除文章页面 - 优化
1 parent 534250c commit 77d318d

File tree

3 files changed

+20
-6
lines changed

3 files changed

+20
-6
lines changed

src/main/java/org/b3log/symphony/processor/middleware/AnonymousViewCheckMidware.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ public void handle(final RequestContext context) {
175175

176176
final Request request = context.getRequest();
177177
final String requestURI = context.requestURI();
178-
final boolean firstVisitArticle = requestURI.startsWith(Latkes.getContextPath() + "/article/");
178+
final boolean firstVisitArticle = requestURI.startsWith("/article/");
179179
JSONObject currentUser = Sessions.getUser();
180180
try {
181181
currentUser = ApiProcessor.getUserByKey(context.param("apiKey"));

src/main/java/org/b3log/symphony/service/CronMgmtService.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,19 @@ public void start() {
308308
}
309309
}, delay, 1 * 60 * 1000, TimeUnit.MILLISECONDS);
310310
delay += 2000;
311+
312+
// 清理防火墙计数过期桶,避免 Map/BANNED 长期膨胀
313+
Symphonys.SCHEDULED_EXECUTOR_SERVICE.scheduleAtFixedRate(() -> {
314+
try {
315+
final long bucket = System.currentTimeMillis() / TimeUnit.MINUTES.toMillis(1);
316+
org.b3log.symphony.util.Firewall.cleanupOldBuckets(bucket);
317+
} catch (final Exception e) {
318+
LOGGER.log(Level.ERROR, "Executes firewall cleanup failed", e);
319+
} finally {
320+
Stopwatchs.release();
321+
}
322+
}, delay, 5 * 60 * 1000, TimeUnit.MILLISECONDS);
323+
delay += 2000;
311324
}
312325

313326
/**

src/main/java/org/b3log/symphony/util/Firewall.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import java.util.Set;
2929
import java.util.concurrent.ConcurrentHashMap;
3030
import java.util.concurrent.TimeUnit;
31+
import java.util.concurrent.atomic.LongAdder;
3132

3233
/**
3334
* Lightweight CC firewall: if an IP exceeds {@link #THRESHOLD} requests within a minute, ban it via ipset.
@@ -87,7 +88,7 @@ public static boolean recordAndMaybeBan(final String ip) {
8788
if (existing == null || existing.bucket != nowBucket) {
8889
return new Counter(nowBucket, 1);
8990
}
90-
existing.count++;
91+
existing.count.increment();
9192
return existing;
9293
});
9394

@@ -96,7 +97,7 @@ public static boolean recordAndMaybeBan(final String ip) {
9697
cleanupOldBuckets(nowBucket);
9798
}
9899

99-
if (counter.count > threshold && BANNED.add(ip)) {
100+
if (counter.count.sum() > threshold && BANNED.add(ip)) {
100101
// Run ban asynchronously on a virtual thread to keep request path light.
101102
Thread.startVirtualThread(() -> {
102103
try {
@@ -114,7 +115,7 @@ public static boolean recordAndMaybeBan(final String ip) {
114115
return !BANNED.contains(ip);
115116
}
116117

117-
private static void cleanupOldBuckets(final long currentBucket) {
118+
public static void cleanupOldBuckets(final long currentBucket) {
118119
COUNTERS.entrySet().removeIf(entry -> entry.getValue().bucket != currentBucket);
119120
}
120121

@@ -150,11 +151,11 @@ public static int getDefaultThreshold() {
150151

151152
private static final class Counter {
152153
private final long bucket;
153-
private int count;
154+
private final LongAdder count = new LongAdder();
154155

155156
private Counter(final long bucket, final int count) {
156157
this.bucket = bucket;
157-
this.count = count;
158+
this.count.add(count);
158159
}
159160
}
160161
}

0 commit comments

Comments
 (0)