40 lines
2.9 KiB
Java
40 lines
2.9 KiB
Java
package com.moyi.controlplane.auth;
|
|||
|
|
|
||
|
|
import com.moyi.controlplane.gateway.GatewayLimitException;
|
||
|
|
import java.util.List;
|
||
|
|
import java.util.Map;
|
||
|
|
import org.springframework.jdbc.core.JdbcTemplate;
|
||
|
|
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
|
||
|
|
import org.springframework.transaction.TransactionDefinition;
|
||
|
|
import org.springframework.transaction.support.TransactionTemplate;
|
||
|
|
|
||
|
|
/** Bounds expensive password work for known AND unknown users; counters survive failed-login rollbacks. */
|
||
|
|
final class LoginAttemptLimiter {
|
||
|
|
private final JdbcTemplate jdbc;
|
||
|
|
private final TransactionTemplate transactions;
|
||
|
|
|
||
|
|
LoginAttemptLimiter(JdbcTemplate jdbc) {
|
||
|
|
this.jdbc = jdbc;
|
||
|
|
if (jdbc.getDataSource() == null) { transactions = null; return; } // Isolated mock-only constructors.
|
||
|
|
transactions = new TransactionTemplate(new DataSourceTransactionManager(jdbc.getDataSource()));
|
||
|
|
transactions.setPropagationBehavior(TransactionDefinition.PROPAGATION_REQUIRES_NEW);
|
||
|
|
}
|
||
|
|
|
||
|
|
void check(String normalizedAccount, String sourceIp) {
|
||
|
|
if (transactions == null) throw new IllegalStateException("Login limiter requires a transactional data source");
|
||
|
|
int retry = transactions.execute(status -> {
|
||
|
|
var limits = Map.of(AuthService.hashSecret("login:account:" + normalizedAccount), 10,
|
||
|
|
AuthService.hashSecret("login:source:" + (sourceIp == null || sourceIp.isBlank() ? "unknown" : sourceIp)), 50);
|
||
|
|
int remaining = 0;
|
||
|
|
// Stable ordering prevents cross-account/source deadlocks on concurrent replicas.
|
||
|
|
for (String bucket : limits.keySet().stream().sorted().toList()) {
|
||
|
|
int limit = limits.get(bucket);
|
||
|
|
var row = jdbc.queryForMap("INSERT INTO auth_login_windows(bucket_hash,attempts) VALUES (?,1) ON CONFLICT(bucket_hash) DO UPDATE SET attempts=CASE WHEN auth_login_windows.expires_at<=clock_timestamp() THEN 1 ELSE LEAST(auth_login_windows.attempts+1,?) END,window_started_at=CASE WHEN auth_login_windows.expires_at<=clock_timestamp() THEN clock_timestamp() ELSE auth_login_windows.window_started_at END,expires_at=CASE WHEN auth_login_windows.expires_at<=clock_timestamp() THEN clock_timestamp()+interval '60 seconds' ELSE auth_login_windows.expires_at END RETURNING attempts,GREATEST(1,CEIL(EXTRACT(EPOCH FROM (expires_at-clock_timestamp()))))::integer AS retry", bucket, limit + 1);
|
||
|
|
if (((Number) row.get("attempts")).intValue() > limit) remaining = Math.max(remaining, ((Number) row.get("retry")).intValue());
|
||
|
|
}
|
||
|
|
jdbc.update("DELETE FROM auth_login_windows WHERE bucket_hash IN (SELECT bucket_hash FROM auth_login_windows WHERE expires_at<clock_timestamp()-interval '5 minutes' LIMIT 500)");
|
||
|
|
return remaining;
|
||
|
|
});
|
||
|
|
if (retry > 0) throw new GatewayLimitException("AUTH_RATE_LIMITED", "登录尝试过于频繁,请稍后重试", 429, retry);
|
||
|
|
}
|
||
|
|
}
|