feat(认证/通知): 单设备登录互踢 + 站内通知铃铛系统

- 单设备登录:登录成功即 last-login-wins 绑定 users.machine;非超管旧 token
  在下一次受保护请求抛 4011 下线,超管豁免;仅认 token 内签名 deviceId,不用请求头。
  前端两端接入踢下线跳转(?kicked=1 提示),V118 清空历史 machine
- 站内通知:新增 notification 模块(任务失败扫描 / 密钥欠费 / 下游服务探测三类来源),
  前后台铃铛组件 + 轮询;后台列表按主管数据范围(UserDataScopeSupport)过滤;V116 建表

均已于 2026-09-13 部署上线,此次补提交源码(此前仅存在于已部署 JAR/构建产物中)
This commit is contained in:
2026-09-13 23:08:22 +08:00
parent 41674d1adb
commit 43b1a0a955
53 changed files with 3982 additions and 101 deletions
@@ -14,4 +14,6 @@ public class AuthProperties {
private String cookieName = "aiimage_token";
private boolean cookieSecure = false;
private String cookieSameSite = "Lax";
/** 单设备登录(互踢)总开关:关闭后恢复为多设备同时在线(回滚用)。 */
private boolean singleDeviceEnabled = true;
}
@@ -8,6 +8,7 @@ import com.nanri.aiimage.modules.auth.mapper.LoginUserMapper;
import com.nanri.aiimage.modules.auth.model.dto.LoginRequest;
import com.nanri.aiimage.modules.auth.model.entity.LoginUserEntity;
import com.nanri.aiimage.modules.auth.model.vo.LoginResultVo;
import com.nanri.aiimage.modules.auth.support.DeviceSessionPolicy;
import com.nanri.aiimage.modules.auth.util.WerkzeugPasswordEncoder;
import com.nanri.aiimage.modules.permission.service.PermissionMenuService;
import io.jsonwebtoken.Claims;
@@ -48,32 +49,14 @@ public class AuthService {
}
boolean isAdmin = user.getIsAdmin() != null && user.getIsAdmin() == 1;
// ---- 设备绑定逻辑临时停用(便于多设备 / 浏览器联调)----
// 原逻辑:首次登录写入 machine;设备指纹变化时把 machine 重绑到当前设备。
// 注释期间登录不写、不校验 machine,任何设备均可登录,仅保留日志便于排查。
/*
String stored = user.getMachine() == null ? "" : user.getMachine().trim();
if (stored.isEmpty()) {
// 单设备登录:登录成功即把账号绑定到当前设备(last-login-wins),原设备下一次请求被顶下线
if (authProperties.isSingleDeviceEnabled()) {
DeviceSessionPolicy.logBindOnLogin(user.getMachine(), deviceId, user.getId(), user.getUsername());
loginUserMapper.update(null, new LambdaUpdateWrapper<LoginUserEntity>()
.eq(LoginUserEntity::getId, user.getId())
.set(LoginUserEntity::getMachine, deviceId));
stored = deviceId;
log.info("[auth] first-login bind userId={} device={}", user.getId(), deviceId);
} else if (!stored.equals(deviceId)) {
// 设备指纹变化(换电脑/重装/清理注册表)会锁死账号,密码已验证通过,
// 直接重新绑定到当前设备,避免账号被锁、数据因重建账号而丢失。
loginUserMapper.update(null, new LambdaUpdateWrapper<LoginUserEntity>()
.eq(LoginUserEntity::getId, user.getId())
.set(LoginUserEntity::getMachine, deviceId));
log.warn("[auth] device rebound on login userId={} old={} new={}",
user.getId(), stored, deviceId);
} else {
log.info("[auth] device match userId={} isAdmin={} device={}",
user.getId(), isAdmin, deviceId);
}
*/
log.info("[auth] login (device-bind disabled) userId={} isAdmin={} device={}",
user.getId(), isAdmin, deviceId);
log.info("[auth] login userId={} isAdmin={} device={}", user.getId(), isAdmin, deviceId);
return buildResult(user, deviceId, isAdmin);
}
@@ -97,31 +80,19 @@ public class AuthService {
throw new BusinessException(401, "用户不存在");
}
boolean isAdmin = user.getIsAdmin() != null && user.getIsAdmin() == 1;
String stored = user.getMachine() == null ? "" : user.getMachine().trim();
String device = trim(currentDeviceId);
if (device.isEmpty()) {
// 没传设备 ID 时,回落到 token 内 deviceId
Object claimDevice = claims.get("deviceId");
device = claimDevice == null ? "" : claimDevice.toString().trim();
String claimDeviceId = DeviceSessionPolicy.claimDeviceId(claims);
// 单设备登录:不拦截的话,被顶下线的旧 token 会在这里续期「复活」
if (authProperties.isSingleDeviceEnabled()) {
DeviceSessionPolicy.assertSameDevice(user.getMachine(), claimDeviceId,
DeviceSessionPolicy.isSuperAdmin(user.getRole(), user.getIsAdmin(), user.getCreatedById()),
user.getId(), user.getUsername());
}
// ---- 设备绑定逻辑临时停用:设备不一致不再拒绝登录态,仅保留告警日志 ----
/*
if (!stored.isEmpty() && !device.isEmpty() && !stored.equals(device)) {
if (isAdmin) {
log.warn("[auth] check_login device mismatch but admin bypass userId={} stored={} current={}",
user.getId(), stored, device);
} else {
log.warn("[auth] check_login device mismatch reject userId={} stored={} current={}",
user.getId(), stored, device);
throw new BusinessException(401, "当前设备与首次登录设备不一致");
}
String headerDevice = trim(currentDeviceId);
if (!headerDevice.isEmpty() && !claimDeviceId.isEmpty() && !headerDevice.equals(claimDeviceId)) {
log.warn("[auth] check_login 请求头设备与登录态内设备不一致(以登录态为准)userId={} headerDevice={} loginDevice={}",
user.getId(), headerDevice, claimDeviceId);
}
*/
if (!stored.isEmpty() && !device.isEmpty() && !stored.equals(device)) {
log.warn("[auth] check_login device mismatch (bypass) userId={} stored={} current={}",
user.getId(), stored, device);
}
return buildResult(user, stored.isEmpty() ? device : stored, isAdmin);
return buildResult(user, claimDeviceId, isAdmin);
}
public ResponseCookie buildAuthCookie(String token) {
@@ -0,0 +1,90 @@
package com.nanri.aiimage.modules.auth.support;
import com.nanri.aiimage.common.exception.BusinessException;
import io.jsonwebtoken.Claims;
import lombok.extern.slf4j.Slf4j;
/**
* 单设备登录(互踢)策略。
*
* <p>账号当前绑定的设备存放在 users.machine(登录成功即覆盖,last-login-wins);
* 非超管账号仅允许「token 内签名的 deviceId」与绑定设备一致的请求通过,
* 被新设备顶下线的旧 token 在下一次受保护请求时抛 4011。</p>
*
* <p>校验只认 token 内签名的 deviceId,绝不使用 X-Device-Id 请求头(头是客户端可控的)。</p>
*/
@Slf4j
public final class DeviceSessionPolicy {
/** 账号已在其他设备登录(前端据此提示并下线本设备)。 */
public static final int CODE_KICKED = 4011;
private DeviceSessionPolicy() {
}
/** 计算用户管理角色:super_admin / admin / null(含老数据 role 为空时按 created_by_id 推断)。 */
public static String resolveRole(String role, Integer isAdmin, Long createdById) {
String storedRole = role == null ? "" : role.trim().toLowerCase();
if ("super_admin".equals(storedRole)) {
return "super_admin";
}
if ("admin".equals(storedRole)) {
return "admin";
}
boolean isAdminFlag = isAdmin != null && isAdmin == 1;
if (storedRole.isEmpty() && isAdminFlag) {
return createdById == null ? "super_admin" : "admin";
}
return null;
}
/** 是否超级管理员(互踢的唯一豁免角色)。 */
public static boolean isSuperAdmin(String role, Integer isAdmin, Long createdById) {
return "super_admin".equals(resolveRole(role, isAdmin, createdById));
}
/** 从 JWT claims 中提取签名的设备号;缺失返回空串。 */
public static String claimDeviceId(Claims claims) {
Object raw = claims == null ? null : claims.get("deviceId");
return raw == null ? "" : raw.toString().trim();
}
/**
* 校验请求携带的 token 是否仍属于账号当前绑定的设备。
* 超管豁免;machine 为空(尚未绑定)放行;不匹配抛 4011。
*/
public static void assertSameDevice(String storedMachine, String claimDeviceId, boolean exempt,
Long userId, String username) {
if (exempt) {
return;
}
String bound = storedMachine == null ? "" : storedMachine.trim();
if (bound.isEmpty()) {
// 尚未绑定(首次登录前 / V118 清空后首个登录前),不做限制
return;
}
String claimed = claimDeviceId == null ? "" : claimDeviceId.trim();
if (claimed.isEmpty()) {
log.warn("[auth] 单设备登录校验:token 缺少设备标识,拒绝 userId={} username={}", userId, username);
throw new BusinessException(401, "登录态无效");
}
if (!bound.equals(claimed)) {
log.warn("[auth] 单设备登录拦截:用户 {} 已被其他设备顶下线,token设备={} 当前绑定设备={}",
userId, claimed, bound);
throw new BusinessException(CODE_KICKED, "该账号已在其他设备登录,本设备已下线");
}
}
/** 登录绑定时的中文日志:首次绑定 / 换设备(顶下线)/ 同设备重登,便于线上排查互踢来源。 */
public static void logBindOnLogin(String previousMachine, String deviceId, Long userId, String username) {
String previous = previousMachine == null ? "" : previousMachine.trim();
if (previous.isEmpty()) {
log.info("[auth] 单设备登录:用户 {}({})首次绑定设备 {}", userId, username, deviceId);
} else if (!previous.equals(deviceId)) {
log.warn("[auth] 单设备登录:用户 {}({})在设备 {} 登录,原设备 {} 已被顶下线",
userId, username, deviceId, previous);
} else {
log.info("[auth] 单设备登录:用户 {}({})同设备重新登录 device={}", userId, username, deviceId);
}
}
}