fix(backend): 改为后台配置注册限流阈值

This commit is contained in:
Zhou Weidong committed 2026-10-17 23:29:25 +08:00
1 parent 78fb99530d
commit f1aaaee814
8 files changed
+79 -39

No files matched your search

@@ -4,6 +4,7 @@ package com.fruitshorts.app.service;
import com.fruitshorts.common.exception.ErrorCode;
import com.fruitshorts.common.exception.ServiceException;
import com.fruitshorts.framework.security.JwtTokenProvider;
import com.fruitshorts.system.service.ConfigService;
import com.fruitshorts.user.dal.entity.AppUser;
import com.fruitshorts.user.dal.entity.AppUserCredential;
import com.fruitshorts.user.dal.entity.AppUserOtp;
@@ -25,7 +26,6 @@ import com.fruitshorts.user.vo.TokenRespVO;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.test.util.ReflectionTestUtils;
import java.time.Duration;
import java.time.LocalDateTime;
@@ -58,6 +58,7 @@ class AppAuthServiceTest {
private CredentialHasher hasher;
private JwtTokenProvider tokenProvider;
private PasswordEncoder passwordEncoder;
private ConfigService configService;
private AppAuthService authService;
@BeforeEach
@@ -71,8 +72,11 @@ class AppAuthServiceTest {
hasher = mock(CredentialHasher.class);
tokenProvider = mock(JwtTokenProvider.class);
passwordEncoder = mock(PasswordEncoder.class);
configService = mock(ConfigService.class);
given(configService.get(anyString(), anyString()))
.willAnswer(invocation -> invocation.getArgument(1));
authService = new AppAuthService(userMapper, credentialMapper, sessionMapper, consentMapper,
deviceMapper, otpService, hasher, tokenProvider, passwordEncoder);
deviceMapper, otpService, hasher, tokenProvider, passwordEncoder, configService);
}
@Test
@@ -187,6 +191,19 @@ class AppAuthServiceTest {
.hasMessage("该设备注册过于频繁,请稍后再试");
}
@Test
void should_fallback_to_default_limit_when_config_is_not_numeric() {
setRegisterLimits(5, 3);
given(configService.get("app.register.max-per-ip-per-day", "5")).willReturn("invalid");
given(userMapper.selectCount(any())).willReturn(5L);
assertThatThrownBy(() -> authService.register(emailRegister("abcd1234", null), "203.0.113.8"))
.isInstanceOf(ServiceException.class)
.satisfies(ex -> assertThat(((ServiceException) ex).getCode())
.isEqualTo(ErrorCode.TOO_MANY_REQUESTS.getCode()))
.hasMessageContaining("当前网络注册过于频繁");
}
@Test
void should_not_limit_by_ip_when_ip_limit_is_zero() {
stubHappyRegister();
@@ -299,12 +316,14 @@ class AppAuthServiceTest {
given(otpHasher.hashOtp(any())).willReturn("hash-of-something-else");
AppOtpService realOtpService = new AppOtpService(otpMapper, mock(OtpSender.class), otpHasher);
return new AppAuthService(userMapper, credentialMapper, sessionMapper, consentMapper,
deviceMapper, realOtpService, hasher, tokenProvider, passwordEncoder);
deviceMapper, realOtpService, hasher, tokenProvider, passwordEncoder, configService);
}
private void setRegisterLimits(int perIp, int perDevice) {
ReflectionTestUtils.setField(authService, "maxRegisterPerIpPerDay", perIp);
ReflectionTestUtils.setField(authService, "maxRegisterPerDevicePerDay", perDevice);
given(configService.get("app.register.max-per-ip-per-day", "5"))
.willReturn(String.valueOf(perIp));
given(configService.get("app.register.max-per-device-per-day", "3"))
.willReturn(String.valueOf(perDevice));
}
private RegisterReqVO emailRegister(String password, String code) {