feat(auth):从JWT令牌中提取注册时间
- 在token工具函数中新增getRegisterTimeFromToken方法 - 修改客户端账户注册逻辑,将创建时间写入JWT令牌- 更新前后端代码以正确传递和解析registerTime字段 - 调整API调用逻辑,优先从令牌中获取注册时间 - 清理部分冗余代码和注释
This commit is contained in:
@@ -467,7 +467,6 @@ app.whenReady().then(() => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
splashWindow.on('closed', () => splashWindow = null);
|
splashWindow.on('closed', () => splashWindow = null);
|
||||||
|
|
||||||
// 加载预注入的 HTML(图片已base64内联,无跨域问题)
|
// 加载预注入的 HTML(图片已base64内联,无跨域问题)
|
||||||
splashWindow.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(splashHtml)}`);
|
splashWindow.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(splashHtml)}`);
|
||||||
splashWindow.once('ready-to-show', () => splashWindow?.show());
|
splashWindow.once('ready-to-show', () => splashWindow?.show());
|
||||||
|
|||||||
@@ -22,7 +22,6 @@ const electronAPI = {
|
|||||||
// 添加日志相关 API
|
// 添加日志相关 API
|
||||||
getLogDates: () => ipcRenderer.invoke('get-log-dates'),
|
getLogDates: () => ipcRenderer.invoke('get-log-dates'),
|
||||||
readLogFile: (logDate: string) => ipcRenderer.invoke('read-log-file', logDate),
|
readLogFile: (logDate: string) => ipcRenderer.invoke('read-log-file', logDate),
|
||||||
|
|
||||||
// 关闭行为配置 API
|
// 关闭行为配置 API
|
||||||
getCloseAction: () => ipcRenderer.invoke('get-close-action'),
|
getCloseAction: () => ipcRenderer.invoke('get-close-action'),
|
||||||
setCloseAction: (action: 'quit' | 'minimize' | 'tray') => ipcRenderer.invoke('set-close-action', action),
|
setCloseAction: (action: 'quit' | 'minimize' | 'tray') => ipcRenderer.invoke('set-close-action', action),
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ import {authApi} from './api/auth'
|
|||||||
import {deviceApi, type DeviceItem, type DeviceQuota} from './api/device'
|
import {deviceApi, type DeviceItem, type DeviceQuota} from './api/device'
|
||||||
import {splashApi} from './api/splash'
|
import {splashApi} from './api/splash'
|
||||||
import {getOrCreateDeviceId} from './utils/deviceId'
|
import {getOrCreateDeviceId} from './utils/deviceId'
|
||||||
import {getToken, setToken, removeToken, getUsernameFromToken, getClientIdFromToken} from './utils/token'
|
import {getToken, setToken, removeToken, getUsernameFromToken, getClientIdFromToken, getRegisterTimeFromToken} from './utils/token'
|
||||||
import {CONFIG} from './api/http'
|
import {CONFIG} from './api/http'
|
||||||
import {getSettings} from './utils/settings'
|
import {getSettings} from './utils/settings'
|
||||||
import LoginDialog from './components/auth/LoginDialog.vue'
|
import LoginDialog from './components/auth/LoginDialog.vue'
|
||||||
@@ -221,7 +221,7 @@ async function handleLoginSuccess(data: {
|
|||||||
vipExpireTime.value = data.expireTime ? new Date(data.expireTime) : null
|
vipExpireTime.value = data.expireTime ? new Date(data.expireTime) : null
|
||||||
accountType.value = data.accountType || 'trial'
|
accountType.value = data.accountType || 'trial'
|
||||||
deviceTrialExpired.value = data.deviceTrialExpired || false
|
deviceTrialExpired.value = data.deviceTrialExpired || false
|
||||||
registerTime.value = data.registerTime || ''
|
registerTime.value = getRegisterTimeFromToken(data.token)
|
||||||
|
|
||||||
const deviceId = await getOrCreateDeviceId()
|
const deviceId = await getOrCreateDeviceId()
|
||||||
await deviceApi.register({
|
await deviceApi.register({
|
||||||
@@ -322,7 +322,7 @@ async function checkAuth() {
|
|||||||
userPermissions.value = res.data.permissions || ''
|
userPermissions.value = res.data.permissions || ''
|
||||||
deviceTrialExpired.value = res.data.deviceTrialExpired || false
|
deviceTrialExpired.value = res.data.deviceTrialExpired || false
|
||||||
accountType.value = res.data.accountType || 'trial'
|
accountType.value = res.data.accountType || 'trial'
|
||||||
registerTime.value = res.data.registerTime || ''
|
registerTime.value = getRegisterTimeFromToken(token)
|
||||||
|
|
||||||
if (res.data.expireTime) {
|
if (res.data.expireTime) {
|
||||||
vipExpireTime.value = new Date(res.data.expireTime)
|
vipExpireTime.value = new Date(res.data.expireTime)
|
||||||
|
|||||||
@@ -35,3 +35,15 @@ export function getClientIdFromToken(token?: string): string {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export function getRegisterTimeFromToken(token?: string): string {
|
||||||
|
try {
|
||||||
|
const t = token || getToken();
|
||||||
|
const payload = JSON.parse(atob(t.split('.')[1]));
|
||||||
|
if (!payload.registerTime) return '';
|
||||||
|
const date = new Date(payload.registerTime);
|
||||||
|
return date.toISOString();
|
||||||
|
} catch {
|
||||||
|
return '';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -195,6 +195,7 @@ public class ClientAccountController extends BaseController {
|
|||||||
.claim("accountId", account.getId())
|
.claim("accountId", account.getId())
|
||||||
.claim("username", username)
|
.claim("username", username)
|
||||||
.claim("clientId", clientId)
|
.claim("clientId", clientId)
|
||||||
|
.claim("registerTime", account.getCreateTime())
|
||||||
.signWith(SignatureAlgorithm.RS256, jwtRsaKeyService.getPrivateKey())
|
.signWith(SignatureAlgorithm.RS256, jwtRsaKeyService.getPrivateKey())
|
||||||
.compact();
|
.compact();
|
||||||
|
|
||||||
@@ -274,46 +275,38 @@ public class ClientAccountController extends BaseController {
|
|||||||
* 新账号注册赠送3天VIP试用期
|
* 新账号注册赠送3天VIP试用期
|
||||||
*/
|
*/
|
||||||
@PostMapping("/register")
|
@PostMapping("/register")
|
||||||
public AjaxResult register(@RequestBody Map<String, String> registerData) {
|
public AjaxResult register(@RequestBody Map<String, String> data) {
|
||||||
String username = registerData.get("username");
|
String username = data.get("username");
|
||||||
String password = registerData.get("password");
|
ClientAccount account = new ClientAccount();
|
||||||
String deviceId = registerData.get("deviceId");
|
account.setUsername(username);
|
||||||
|
account.setAccountName(username);
|
||||||
|
account.setCreateBy("system");
|
||||||
|
account.setCreateTime(new Date());
|
||||||
|
account.setStatus("0");
|
||||||
|
account.setPermissions("{\"amazon\":true,\"rakuten\":true,\"zebra\":true}");
|
||||||
|
account.setPassword(passwordEncoder.encode(data.get("password")));
|
||||||
|
account.setAccountType("trial");
|
||||||
|
account.setDeviceLimit(1);
|
||||||
|
account.setExpireTime(new Date(System.currentTimeMillis() + 3 * 24L * 60 * 60 * 1000));
|
||||||
|
|
||||||
ClientAccount clientAccount = new ClientAccount();
|
if (clientAccountService.insertClientAccount(account) <= 0) return AjaxResult.error("注册失败");
|
||||||
clientAccount.setUsername(username);
|
|
||||||
clientAccount.setAccountName(username);
|
|
||||||
clientAccount.setCreateBy("system");
|
|
||||||
clientAccount.setStatus("0");
|
|
||||||
clientAccount.setPermissions("{\"amazon\":true,\"rakuten\":true,\"zebra\":true}");
|
|
||||||
clientAccount.setPassword(passwordEncoder.encode(password));
|
|
||||||
clientAccount.setAccountType("trial");
|
|
||||||
clientAccount.setDeviceLimit(1);
|
|
||||||
clientAccount.setExpireTime(new Date(System.currentTimeMillis() + 3 * 24L * 60 * 60 * 1000));
|
|
||||||
|
|
||||||
int result = clientAccountService.insertClientAccount(clientAccount);
|
|
||||||
if (result <= 0) {
|
|
||||||
return AjaxResult.error("注册失败");
|
|
||||||
}
|
|
||||||
|
|
||||||
|
account = clientAccountService.selectClientAccountByUsername(username);
|
||||||
String token = Jwts.builder()
|
String token = Jwts.builder()
|
||||||
.setHeaderParam("kid", jwtRsaKeyService.getKeyId())
|
.setHeaderParam("kid", jwtRsaKeyService.getKeyId())
|
||||||
.setSubject(clientAccount.getUsername())
|
.setSubject(username)
|
||||||
.setIssuedAt(new Date())
|
.setIssuedAt(new Date())
|
||||||
.setExpiration(new Date(System.currentTimeMillis() + JWT_EXPIRATION))
|
.setExpiration(new Date(System.currentTimeMillis() + JWT_EXPIRATION))
|
||||||
.claim("accountId", clientAccount.getId())
|
.claim("accountId", account.getId())
|
||||||
.claim("username", username)
|
.claim("username", username)
|
||||||
.claim("clientId", deviceId)
|
.claim("clientId", data.get("deviceId"))
|
||||||
|
.claim("registerTime", account.getCreateTime())
|
||||||
.signWith(SignatureAlgorithm.RS256, jwtRsaKeyService.getPrivateKey())
|
.signWith(SignatureAlgorithm.RS256, jwtRsaKeyService.getPrivateKey())
|
||||||
.compact();
|
.compact();
|
||||||
|
|
||||||
return AjaxResult.success(Map.of(
|
return AjaxResult.success(Map.of("token", token, "permissions", account.getPermissions(),
|
||||||
"token", token,
|
"accountName", account.getAccountName(), "expireTime", account.getExpireTime(),
|
||||||
"permissions", clientAccount.getPermissions(),
|
"accountType", account.getAccountType(), "registerTime", account.getCreateTime()));
|
||||||
"accountName", clientAccount.getAccountName(),
|
|
||||||
"expireTime", clientAccount.getExpireTime(),
|
|
||||||
"accountType", clientAccount.getAccountType(),
|
|
||||||
"registerTime", clientAccount.getCreateTime()
|
|
||||||
));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|||||||
@@ -42,7 +42,6 @@ public class MarkController {
|
|||||||
return AjaxResult.success("获取成功", token);
|
return AjaxResult.success("获取成功", token);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Token 不存在或不可用,重新注册新账号
|
|
||||||
token = markService.reg();
|
token = markService.reg();
|
||||||
return AjaxResult.success("注册成功", token);
|
return AjaxResult.success("注册成功", token);
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user