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);
|
||||
|
||||
// 加载预注入的 HTML(图片已base64内联,无跨域问题)
|
||||
splashWindow.loadURL(`data:text/html;charset=utf-8,${encodeURIComponent(splashHtml)}`);
|
||||
splashWindow.once('ready-to-show', () => splashWindow?.show());
|
||||
@@ -477,7 +476,7 @@ app.whenReady().then(() => {
|
||||
setTimeout(() => {
|
||||
startSpringBoot();
|
||||
}, 200);
|
||||
|
||||
|
||||
// setTimeout(() => {
|
||||
// openAppIfNotOpened();
|
||||
// }, 200);
|
||||
|
||||
@@ -22,7 +22,6 @@ const electronAPI = {
|
||||
// 添加日志相关 API
|
||||
getLogDates: () => ipcRenderer.invoke('get-log-dates'),
|
||||
readLogFile: (logDate: string) => ipcRenderer.invoke('read-log-file', logDate),
|
||||
|
||||
// 关闭行为配置 API
|
||||
getCloseAction: () => ipcRenderer.invoke('get-close-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 {splashApi} from './api/splash'
|
||||
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 {getSettings} from './utils/settings'
|
||||
import LoginDialog from './components/auth/LoginDialog.vue'
|
||||
@@ -221,7 +221,7 @@ async function handleLoginSuccess(data: {
|
||||
vipExpireTime.value = data.expireTime ? new Date(data.expireTime) : null
|
||||
accountType.value = data.accountType || 'trial'
|
||||
deviceTrialExpired.value = data.deviceTrialExpired || false
|
||||
registerTime.value = data.registerTime || ''
|
||||
registerTime.value = getRegisterTimeFromToken(data.token)
|
||||
|
||||
const deviceId = await getOrCreateDeviceId()
|
||||
await deviceApi.register({
|
||||
@@ -322,7 +322,7 @@ async function checkAuth() {
|
||||
userPermissions.value = res.data.permissions || ''
|
||||
deviceTrialExpired.value = res.data.deviceTrialExpired || false
|
||||
accountType.value = res.data.accountType || 'trial'
|
||||
registerTime.value = res.data.registerTime || ''
|
||||
registerTime.value = getRegisterTimeFromToken(token)
|
||||
|
||||
if (res.data.expireTime) {
|
||||
vipExpireTime.value = new Date(res.data.expireTime)
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
export type HttpMethod = 'GET' | 'POST' | 'DELETE';
|
||||
const RUOYI_BASE = 'http://8.138.23.49:8085';
|
||||
// const RUOYI_BASE = 'http://192.168.1.89:8085';
|
||||
//const RUOYI_BASE = 'http://192.168.1.89:8085';
|
||||
export const CONFIG = {
|
||||
CLIENT_BASE: 'http://localhost:8081',
|
||||
RUOYI_BASE,
|
||||
|
||||
@@ -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("username", username)
|
||||
.claim("clientId", clientId)
|
||||
.claim("registerTime", account.getCreateTime())
|
||||
.signWith(SignatureAlgorithm.RS256, jwtRsaKeyService.getPrivateKey())
|
||||
.compact();
|
||||
|
||||
@@ -274,46 +275,38 @@ public class ClientAccountController extends BaseController {
|
||||
* 新账号注册赠送3天VIP试用期
|
||||
*/
|
||||
@PostMapping("/register")
|
||||
public AjaxResult register(@RequestBody Map<String, String> registerData) {
|
||||
String username = registerData.get("username");
|
||||
String password = registerData.get("password");
|
||||
String deviceId = registerData.get("deviceId");
|
||||
|
||||
ClientAccount clientAccount = new ClientAccount();
|
||||
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));
|
||||
public AjaxResult register(@RequestBody Map<String, String> data) {
|
||||
String username = data.get("username");
|
||||
ClientAccount account = new ClientAccount();
|
||||
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));
|
||||
|
||||
int result = clientAccountService.insertClientAccount(clientAccount);
|
||||
if (result <= 0) {
|
||||
return AjaxResult.error("注册失败");
|
||||
}
|
||||
if (clientAccountService.insertClientAccount(account) <= 0) return AjaxResult.error("注册失败");
|
||||
|
||||
account = clientAccountService.selectClientAccountByUsername(username);
|
||||
String token = Jwts.builder()
|
||||
.setHeaderParam("kid", jwtRsaKeyService.getKeyId())
|
||||
.setSubject(clientAccount.getUsername())
|
||||
.setSubject(username)
|
||||
.setIssuedAt(new Date())
|
||||
.setExpiration(new Date(System.currentTimeMillis() + JWT_EXPIRATION))
|
||||
.claim("accountId", clientAccount.getId())
|
||||
.claim("accountId", account.getId())
|
||||
.claim("username", username)
|
||||
.claim("clientId", deviceId)
|
||||
.claim("clientId", data.get("deviceId"))
|
||||
.claim("registerTime", account.getCreateTime())
|
||||
.signWith(SignatureAlgorithm.RS256, jwtRsaKeyService.getPrivateKey())
|
||||
.compact();
|
||||
|
||||
return AjaxResult.success(Map.of(
|
||||
"token", token,
|
||||
"permissions", clientAccount.getPermissions(),
|
||||
"accountName", clientAccount.getAccountName(),
|
||||
"expireTime", clientAccount.getExpireTime(),
|
||||
"accountType", clientAccount.getAccountType(),
|
||||
"registerTime", clientAccount.getCreateTime()
|
||||
));
|
||||
return AjaxResult.success(Map.of("token", token, "permissions", account.getPermissions(),
|
||||
"accountName", account.getAccountName(), "expireTime", account.getExpireTime(),
|
||||
"accountType", account.getAccountType(), "registerTime", account.getCreateTime()));
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -42,7 +42,6 @@ public class MarkController {
|
||||
return AjaxResult.success("获取成功", token);
|
||||
}
|
||||
|
||||
// Token 不存在或不可用,重新注册新账号
|
||||
token = markService.reg();
|
||||
return AjaxResult.success("注册成功", token);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user