登入认证修改

This commit is contained in:
lu7213 committed 2026-11-16 09:02:15 -05:00
1 parent f3b4d171d7
commit c12b777a42
1 file changed
+2 -40
@@ -12,8 +12,6 @@ import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.framework.web.service.TotpSecretCipher;
import com.ruoyi.framework.web.service.UserDetailsServiceImpl;
import com.ruoyi.system.mapper.SysUserTotpMapper;
import com.ruoyi.system.mapper.SysUserGoogleOauthMapper;
import com.ruoyi.system.domain.SysUserGoogleOauth;
import com.ruoyi.system.service.ISysUserService;
import java.time.Instant;
import java.util.Base64;
@@ -34,7 +32,6 @@ import static org.mockito.Mockito.*;
/** 可控 MySQL/Redis 替身:覆盖密码校验、临时绑定、并发冲突、防重放和限流失败关闭。 */
public class TotpBindingServiceTest {
private SysUserTotpMapper mapper;
private SysUserGoogleOauthMapper googleMapper;
private StringRedisTemplate redis;
private ValueOperations<String, String> values;
private TotpSecretCipher cipher;
@@ -46,7 +43,6 @@ public class TotpBindingServiceTest {
@SuppressWarnings("unchecked")
public void setup() {
mapper = mock(SysUserTotpMapper.class);
googleMapper = mock(SysUserGoogleOauthMapper.class);
redis = mock(StringRedisTemplate.class);
values = mock(ValueOperations.class);
when(redis.opsForValue()).thenReturn(values);
@@ -58,7 +54,7 @@ public class TotpBindingServiceTest {
user.setPassword(SecurityUtils.encryptPassword("test-only-password"));
when(users.selectUserById(7L)).thenReturn(user);
cipher = new TotpSecretCipher(Base64.getEncoder().encodeToString(new byte[32]));
service = new TotpAuthService(mapper, cipher, redis, users, mock(UserDetailsServiceImpl.class), googleMapper);
service = new TotpAuthService(mapper, cipher, redis, users, mock(UserDetailsServiceImpl.class));
ReflectionTestUtils.setField(service, "issuer", "USDT PAY");
secret = TotpUtils.newSecret();
encrypted = cipher.encrypt(7L, secret);
@@ -124,45 +120,11 @@ public class TotpBindingServiceTest {
@Test
public void unboundUserDoesNotRequireSecondFactor() {
service.verifyBoundUser(7L, null);
verify(googleMapper).selectByUserId("google", 7L);
verify(mapper, never()).consume(anyLong(), anyString(), anyLong());
}
@Test
public void googleBindingWithoutAuthenticatorMustNotBypassVerification() {
when(googleMapper.selectByUserId("google", 7L)).thenReturn(new SysUserGoogleOauth());
expectFailure(() -> service.verifyBoundUser(7L, null));
expectFailure(() -> service.verifyBoundUser(7L, ""));
expectFailure(() -> service.verifyBoundUser(7L, "123456"));
verify(mapper, never()).consume(anyLong(), anyString(), anyLong());
}
/** 即使密码认证成功,Google 已绑定账号也不能从简化登录入口拿到令牌。 */
@Test
public void passwordAuthenticationCannotIssueTokenWithoutRequiredCode() {
when(googleMapper.selectByUserId("google", 7L)).thenReturn(new SysUserGoogleOauth());
SysLoginService login = new SysLoginService();
AuthenticationManager authenticationManager = mock(AuthenticationManager.class);
Authentication authentication = mock(Authentication.class);
LoginUser principal = mock(LoginUser.class);
SysUser user = new SysUser();
user.setUserId(7L);
when(principal.getUser()).thenReturn(user);
when(authentication.getPrincipal()).thenReturn(principal);
when(authenticationManager.authenticate(any(Authentication.class))).thenReturn(authentication);
TokenService tokens = mock(TokenService.class);
ReflectionTestUtils.setField(login, "authenticationManager", authenticationManager);
ReflectionTestUtils.setField(login, "redisCache", mock(RedisCache.class));
ReflectionTestUtils.setField(login, "totpAuthService", service);
ReflectionTestUtils.setField(login, "tokenService", tokens);
expectFailure(() -> login.login("tester", "correct-password", null, "test", false, null));
expectFailure(() -> login.login("tester", "correct-password", null, "test", false, "123456"));
verify(tokens, never()).createToken(any(LoginUser.class));
}
@Test
public void googleBoundUserMustProvideValidUnusedAuthenticatorCode() {
when(googleMapper.selectByUserId("google", 7L)).thenReturn(new SysUserGoogleOauth());
public void authenticatorBoundUserMustProvideValidUnusedAuthenticatorCode() {
when(mapper.selectSecret(7L)).thenReturn(encrypted);
expectFailure(() -> service.verifyBoundUser(7L, null));
expectFailure(() -> service.verifyBoundUser(7L, ""));