feat(续期): 凭据领取顺带回发新 JWT——旧客户端忽略未知键故服务端可先行
挂点是现成的:桌面端每 30 分钟带 JWT 领一次凭据(TTL_SECONDS=1800),不必新加往返、 也不必客户端先学会调 refresh。既有 5 个键名逐字不动(旧客户端按 data.get 取值), 用例同时钉住「不许改名/删键」——那会让存量设备当场领不到凭据,比装机率更糟。
This commit is contained in:
+6
@@ -31,6 +31,9 @@ public class ClientCredentialController {
|
||||
private final AdminAuthSupport adminAuthSupport;
|
||||
private final ClientCredentialProperties clientCredentialProperties;
|
||||
|
||||
/** 续期用:领凭据时顺带重签一张 JWT 交回(见 {@link ClientCredentialVo#getJwt()})。 */
|
||||
private final com.nanri.aiimage.common.security.JwtService jwtService;
|
||||
|
||||
@GetMapping("/credentials")
|
||||
@Operation(summary = "领取运行期凭据",
|
||||
description = "以登录态 JWT 换取内部令牌与紫鸟自动化账号;客户端不得落盘。"
|
||||
@@ -52,6 +55,9 @@ public class ClientCredentialController {
|
||||
vo.setZiniaoPassword(password);
|
||||
// MySQL 口令是次要路径,缺席不下发空串即可:客户端用到时才显式失败并点名配置项
|
||||
vo.setMysqlPassword(blankToEmpty(clientCredentialProperties.getMysqlPassword()));
|
||||
// 续期与容忍是两件事:容忍让存量客户端不被 7 天边界掐断,这条把新客户端拉回正常轨道
|
||||
vo.setJwt(jwtService.issue(user.getId(), user.getUsername(),
|
||||
adminAuthSupport.currentDeviceId(request)));
|
||||
// 只记身份不记值:便于事后回答"哪个账号在哪台机器上领过紫鸟口令"
|
||||
log.info("[client-credentials] 下发凭据 userId={} username={} deviceId={}",
|
||||
user.getId(), user.getUsername(), adminAuthSupport.currentDeviceId(request));
|
||||
|
||||
+8
@@ -21,4 +21,12 @@ public class ClientCredentialVo {
|
||||
|
||||
@Schema(description = "MySQL 业务账号口令(客户端直连库用);服务端未配置时为空串")
|
||||
private String mysqlPassword;
|
||||
/**
|
||||
* 会话续期:领凭据这件事本身就把这张 JWT 往后推一格。
|
||||
* 桌面端每 30 分钟领一次(client_credentials TTL_SECONDS=1800),所以不需要新的往返、
|
||||
* 也不需要客户端先学会调 refresh 接口。旧客户端按键取值、忽略本字段,零破坏。
|
||||
*/
|
||||
@Schema(description = "重新签发的登录令牌(存量客户端忽略此键,接住的客户端据此滚动会话)")
|
||||
private String jwt;
|
||||
|
||||
}
|
||||
|
||||
+2
-1
@@ -31,7 +31,8 @@ class ClientCredentialControllerTest {
|
||||
private final HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
|
||||
private ClientCredentialController newController() {
|
||||
return new ClientCredentialController(adminAuthSupport, properties);
|
||||
return new ClientCredentialController(adminAuthSupport, properties,
|
||||
mock(com.nanri.aiimage.common.security.JwtService.class));
|
||||
}
|
||||
|
||||
private void configureAll() {
|
||||
|
||||
+74
@@ -0,0 +1,74 @@
|
||||
package com.nanri.aiimage.modules.clientcredential.controller;
|
||||
|
||||
import com.nanri.aiimage.common.api.ApiResponse;
|
||||
import com.nanri.aiimage.common.model.entity.AdminUserEntity;
|
||||
import com.nanri.aiimage.common.security.AdminAuthSupport;
|
||||
import com.nanri.aiimage.common.security.JwtService;
|
||||
import com.nanri.aiimage.config.ClientCredentialProperties;
|
||||
import com.nanri.aiimage.modules.clientcredential.model.vo.ClientCredentialVo;
|
||||
import jakarta.servlet.http.HttpServletRequest;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import java.util.Arrays;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* 会话续期挂在凭据领取口上:桌面端本来就每 30 分钟带着 JWT 打这里一次
|
||||
* ({@code client_credentials.py} 的 TTL_SECONDS=1800),所以多发一个字段就是现成的续期通道。
|
||||
*
|
||||
* <p>两条底线:既有键名一个字不许改(旧客户端按 {@code data.get(key)} 取值,忽略未知键才零破坏);
|
||||
* 续期不许成为容忍的前提——存量客户端不接这个字段也必须靠容忍活着。</p>
|
||||
*/
|
||||
class ClientCredentialRenewalTest {
|
||||
|
||||
private final AdminAuthSupport adminAuthSupport = mock(AdminAuthSupport.class);
|
||||
private final JwtService jwtService = mock(JwtService.class);
|
||||
private final ClientCredentialProperties properties = new ClientCredentialProperties();
|
||||
private final HttpServletRequest request = mock(HttpServletRequest.class);
|
||||
|
||||
private ClientCredentialController newController() {
|
||||
return new ClientCredentialController(adminAuthSupport, properties, jwtService);
|
||||
}
|
||||
|
||||
private void configureAll() {
|
||||
properties.setZiniaoCompany("company-x");
|
||||
properties.setZiniaoUsername("user-x");
|
||||
properties.setZiniaoPassword("password-x");
|
||||
properties.setMysqlPassword("mysql-pass-x");
|
||||
}
|
||||
|
||||
private ClientCredentialVo handOut() {
|
||||
AdminUserEntity user = new AdminUserEntity();
|
||||
user.setId(7L);
|
||||
user.setUsername("小魏16");
|
||||
when(adminAuthSupport.requireUser(any())).thenReturn(user);
|
||||
when(adminAuthSupport.currentInternalToken()).thenReturn("token-x");
|
||||
when(adminAuthSupport.currentDeviceId(any())).thenReturn("dev-1");
|
||||
when(jwtService.issue(eq(7L), eq("小魏16"), eq("dev-1"))).thenReturn("jwt-fresh");
|
||||
configureAll();
|
||||
ApiResponse<ClientCredentialVo> response = newController().credentials(request);
|
||||
assertThat(response.isSuccess()).isTrue();
|
||||
return response.getData();
|
||||
}
|
||||
|
||||
@Test
|
||||
void freshJwtIsHandedBackAlongsideCredentials() {
|
||||
ClientCredentialVo vo = handOut();
|
||||
assertThat(vo.getJwt()).isEqualTo("jwt-fresh");
|
||||
assertThat(vo.getInternalToken()).isEqualTo("token-x"); // 续期不改写既有载荷
|
||||
}
|
||||
|
||||
@Test
|
||||
void legacyKeyNamesAreUntouched() {
|
||||
// 旧客户端只认这几个键;改名或删键 = 存量设备当场领不到凭据(比装机率更糟:全员炸)
|
||||
assertThat(Arrays.asList(ClientCredentialVo.class.getDeclaredFields()).stream()
|
||||
.map(f -> f.getName()).toList())
|
||||
.contains("internalToken", "ziniaoCompany", "ziniaoUsername", "ziniaoPassword", "mysqlPassword")
|
||||
.doesNotContain("token", "jwtToken");
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user