From f3b4d171d773b0be0a823c83cce5f1730b1964a3 Mon Sep 17 00:00:00 2001 From: luqingjie <544309509@qq.com> Date: Mon, 16 Nov 2026 08:26:14 -0500 Subject: [PATCH] =?UTF-8?q?google=20=E7=99=BB=E5=85=A5=E4=BF=AE=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- docs/totp-login.md | 5 +- .../system/TotpBindingServiceTest.java | 56 ++++++++++++++++++- 2 files changed, 59 insertions(+), 2 deletions(-) diff --git a/docs/totp-login.md b/docs/totp-login.md index aef3d4c..7a2c8ad 100644 --- a/docs/totp-login.md +++ b/docs/totp-login.md @@ -1,6 +1,8 @@ # Google Authenticator 登录二次验证 -仅提供密码登录和 Google OAuth 登录,不提供独立动态码登录。已绑定 Google Authenticator 的账号,在密码登录(含 `/simple-login`)及 Google 登录交换票据时,必须提供有效且未使用的六位授权码 `totpCode`,否则不签发令牌。未绑定账号无需填写。 +仅提供密码登录和 Google OAuth 登录,不提供独立动态码登录。已绑定 Google 账号或 Google Authenticator 的账号,在密码登录(含 `/simple-login`)及 Google 登录交换票据时,必须提供有效且未使用的六位授权码 `totpCode`,否则不签发令牌。两者均未绑定的账号无需填写。 + +Google 身份绑定与 Authenticator 绑定分别记录在 `sys_user_google_oauth` 和 `sys_user_totp`。仅有 Google 身份绑定、缺少 Authenticator 凭据的旧账号会拒绝登录,需通过已有会话完成认证器绑定或联系管理员核实处理;不会自动生成密钥或仅检查输入非空后放行。 ## 部署 @@ -21,6 +23,7 @@ $totpRng.Dispose() ## 使用 +- Google 和 Authenticator 均未绑定的账号可直接使用密码及图形验证码登录。登录成功后提示前往账号安全完成绑定;选择“稍后处理”或关闭提示可直接进入系统。建议先绑定 Authenticator,再绑定 Google,防止下次登录因缺少动态码凭据被拒绝。 - 先用密码或已绑定 Google 账号登录,打开“个人中心 → 账号安全 → Google Authenticator 动态码”。 - 输入本地密码生成二维码;用 Google Authenticator 扫码,或手动输入页面展示的设置密钥,再输入手机六位动态码确认绑定。二维码在浏览器本地生成。 - 临时绑定十分钟过期,未经验证不会启用。确认绑定所用码已消费,等手机刷新下一组码后再用于登录。 diff --git a/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TotpBindingServiceTest.java b/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TotpBindingServiceTest.java index f94a9fe..7f63cdb 100644 --- a/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TotpBindingServiceTest.java +++ b/ruoyi-admin/src/test/java/com/ruoyi/web/controller/system/TotpBindingServiceTest.java @@ -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 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);