google 登入修改

This commit is contained in:
lu7213 committed 2026-11-16 08:26:14 -05:00
1 parent fee0c3243b
commit f3b4d171d7
2 files changed
+59 -2

No files matched your search

@@ -1,13 +1,19 @@
package com.ruoyi.web.controller.system;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.exception.CustomException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.TotpUtils;
import com.ruoyi.framework.web.service.TotpAuthService;
import com.ruoyi.framework.web.service.SysLoginService;
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;
@@ -19,6 +25,8 @@ import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.test.util.ReflectionTestUtils;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.core.Authentication;
import static org.junit.Assert.*;
import static org.mockito.ArgumentMatchers.*;
import static org.mockito.Mockito.*;
@@ -26,6 +34,7 @@ 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;
@@ -37,6 +46,7 @@ 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);
@@ -48,7 +58,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));
service = new TotpAuthService(mapper, cipher, redis, users, mock(UserDetailsServiceImpl.class), googleMapper);
ReflectionTestUtils.setField(service, "issuer", "USDT PAY");
secret = TotpUtils.newSecret();
encrypted = cipher.encrypt(7L, secret);
@@ -114,9 +124,53 @@ 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());
when(mapper.selectSecret(7L)).thenReturn(encrypted);
expectFailure(() -> service.verifyBoundUser(7L, null));
expectFailure(() -> service.verifyBoundUser(7L, ""));
expectFailure(() -> service.verifyBoundUser(7L, currentCode()));
when(mapper.consume(eq(7L), eq(encrypted), anyLong())).thenReturn(1);
service.verifyBoundUser(7L, currentCode());
}
@Test
public void boundUserRequiresSixDigitsAndAtomicConsumption() {
when(mapper.selectSecret(7L)).thenReturn(encrypted);