实现JWT身份认证机制,新增JWT认证过滤器和服务,更新链接生成接口以支持JWT验证,删除旧的用户控制器,添加JWT认证文档,增强错误处理和日志记录。

This commit is contained in:
zyh
2025-08-25 21:26:16 +08:00
parent 3f01d8590a
commit 7317866f98
54 changed files with 551 additions and 163 deletions

View File

@@ -46,46 +46,43 @@ public class LinkGenerationService {
@Transactional
public Mono<GenerateResult> generateLinks(Long operatorId, String operatorType,
Long targetAccountId,
int times, int perTimeQuantity) {
return Mono.fromCallable(() -> doGenerate(operatorId, operatorType, targetAccountId, times, perTimeQuantity))
return Mono.fromCallable(() -> doGenerate(operatorId, operatorType, times, perTimeQuantity))
.subscribeOn(Schedulers.boundedElastic());
}
private GenerateResult doGenerate(Long operatorId, String operatorType,
Long targetAccountId,
int times, int perTimeQuantity) {
if (times <= 0 || perTimeQuantity <= 0) {
throw new IllegalArgumentException("times 与 perTimeQuantity 必须为正整数");
}
UserAccount target = userAccountMapper.findById(targetAccountId);
if (target == null) {
throw new IllegalArgumentException("目标账户不存在");
// 获取操作者账户信息
UserAccount operator = userAccountMapper.findById(operatorId);
if (operator == null) {
throw new IllegalArgumentException("操作者账户不存在");
}
boolean isAdminOperator = "ADMIN".equalsIgnoreCase(operatorType);
if (!isAdminOperator && !"AGENT".equalsIgnoreCase(operatorType)) {
throw new IllegalArgumentException("非法操作者类型");
}
if (!"AGENT".equalsIgnoreCase(target.getUserType())) {
throw new IllegalArgumentException("仅支持为代理账户生成链接");
}
long needPoints = (long) times * (long) perTimeQuantity;
if (log.isDebugEnabled()) {
log.debug("generateLinks operatorId={} operatorType={} targetAccountId={} times={} perTimeQuantity={} needPoints={} expireHours={}",
operatorId, operatorType, targetAccountId, times, perTimeQuantity, needPoints, expireHours);
log.debug("generateLinks operatorId={} operatorType={} times={} perTimeQuantity={} needPoints={} expireHours={}",
operatorId, operatorType, times, perTimeQuantity, needPoints, expireHours);
}
if (!isAdminOperator) {
// 代理商自操作,需扣点判断
long balance = target.getPointsBalance() == null ? 0L : target.getPointsBalance();
long balance = operator.getPointsBalance() == null ? 0L : operator.getPointsBalance();
if (balance < needPoints) {
throw new IllegalStateException("点数不足");
}
}
LinkBatch batch = new LinkBatch();
batch.setAgentId(target.getId());
batch.setAgentId(operator.getId());
batch.setQuantity(times);
batch.setTimes(times);
batch.setBatchSize(perTimeQuantity);
@@ -98,7 +95,7 @@ public class LinkGenerationService {
for (int i = 0; i < times; i++) {
LinkTask t = new LinkTask();
t.setBatchId(batch.getId());
t.setAgentId(target.getId());
t.setAgentId(operator.getId());
t.setCodeNo(generateCodeNo());
t.setTokenHash(DigestUtils.sha256Hex(generateToken()));
t.setExpireAt(expireAt);
@@ -109,11 +106,11 @@ public class LinkGenerationService {
if (!isAdminOperator) {
// 扣点流水 + 账户余额
long before = target.getPointsBalance() == null ? 0L : target.getPointsBalance();
long before = operator.getPointsBalance() == null ? 0L : operator.getPointsBalance();
long after = before - needPoints;
AgentPointsTx tx = new AgentPointsTx();
tx.setAccountId(target.getId());
tx.setAccountId(operator.getId());
tx.setType("DEDUCT");
tx.setBeforePoints(before);
tx.setDeltaPoints(-needPoints);
@@ -124,7 +121,7 @@ public class LinkGenerationService {
agentPointsTxMapper.insert(tx);
UserAccount patch = new UserAccount();
patch.setId(target.getId());
patch.setId(operator.getId());
patch.setPointsBalance(after);
userAccountMapper.update(patch);
}