- 根包 com.bestsch.classicalchinese 重命名为 com.bestsch.bschclassmanage - 修复 CORS 启动失败:allowCredentials=true 时 allowedOrigins 不能含 "*", 23 个 Controller 的 @CrossOrigin 改用语义等价的 originPatterns - 修复 /api/* 与 /oapi/* 接口 404:API_PATH/OAPI 常量去掉尾斜杠, 消除 Spring Boot 2.7 PathPattern 下的双斜杠(/api//subject/) 导致的匹配失败 - UserSysRoleArgumentResolver 增加从 URL 参数 currentrole 回退解析(便于本地调试), 原请求头 currentrole 注入方式保持不变
76 lines
2.9 KiB
Java
76 lines
2.9 KiB
Java
package com.bestsch.bschclassmanage.aspect;
|
|
|
|
import com.bestsch.bschclassmanage.model.tool.UserSysRole;
|
|
import com.bestsch.bschclassmanage.utils.OtherUtil;
|
|
import com.bestsch.bschclassmanage.utils.UserContext;
|
|
import com.yunhai.platform.utils.model.BoaUser;
|
|
import org.aspectj.lang.ProceedingJoinPoint;
|
|
import org.aspectj.lang.annotation.Around;
|
|
import org.aspectj.lang.annotation.Aspect;
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
import org.springframework.core.annotation.Order;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.context.request.RequestContextHolder;
|
|
import org.springframework.web.context.request.ServletRequestAttributes;
|
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
@Aspect
|
|
@Component
|
|
@Order(1)
|
|
public class ControllerUserContextAspect {
|
|
|
|
private static final Logger log = LoggerFactory.getLogger(ControllerUserContextAspect.class);
|
|
|
|
@Around("execution(* com.bestsch.bschclassmanage.controller..*.*(..))")
|
|
public Object setUserContext(ProceedingJoinPoint joinPoint) throws Throwable {
|
|
// 1. 从方法参数中提取 BoaUser 和 UserSysRole
|
|
Object[] args = joinPoint.getArgs();
|
|
BoaUser boaUser = null;
|
|
UserSysRole userSysRole = null;
|
|
|
|
for (Object arg : args) {
|
|
if (arg instanceof BoaUser) {
|
|
boaUser = (BoaUser) arg;
|
|
} else if (arg instanceof UserSysRole) {
|
|
userSysRole = (UserSysRole) arg;
|
|
}
|
|
}
|
|
|
|
// 2. 填充 UserContext
|
|
populateUserContext(boaUser, userSysRole);
|
|
|
|
try {
|
|
return joinPoint.proceed();
|
|
} finally {
|
|
UserContext.clear();
|
|
log.debug("UserContext cleared for {}", joinPoint.getSignature().toShortString());
|
|
}
|
|
}
|
|
|
|
private HttpServletRequest getCurrentHttpRequest() {
|
|
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
|
return attributes != null ? attributes.getRequest() : null;
|
|
}
|
|
|
|
private void populateUserContext(BoaUser boaUser, UserSysRole userSysRole) {
|
|
if (boaUser != null) {
|
|
UserContext.setUserId(boaUser.getId());
|
|
UserContext.setUserName(boaUser.getName() != null ? boaUser.getName() : "unknown");
|
|
log.debug("UserContext set from parameter/session: id={}, name={}", boaUser.getId(), boaUser.getName());
|
|
} else {
|
|
UserContext.setUserName("anonymous");
|
|
log.warn("No BoaUser found, using anonymous");
|
|
}
|
|
|
|
if (userSysRole != null) {
|
|
UserContext.setRoleId(userSysRole.getRoleId());
|
|
UserContext.setSchId(userSysRole.getSchId());
|
|
log.debug("UserContext set roleId={}, schId={}", userSysRole.getRoleId(), userSysRole.getSchId());
|
|
}
|
|
|
|
String clientIp = OtherUtil.getClientIp();
|
|
UserContext.setIp(clientIp);
|
|
log.debug("Client IP set: {}", clientIp);
|
|
}
|
|
} |