qqq
This commit is contained in:
@@ -1,8 +1,10 @@
|
||||
package com.yunhai.bschclassmanage.aspect;
|
||||
|
||||
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||
import com.yunhai.bschclassmanage.model.tool.UserSysRole;
|
||||
import com.yunhai.bschclassmanage.utils.OtherUtil;
|
||||
import com.yunhai.bschclassmanage.utils.UserContext;
|
||||
import com.yunhai.platform.auth.BoaUserContext;
|
||||
import com.yunhai.platform.utils.model.BoaUser;
|
||||
import org.aspectj.lang.ProceedingJoinPoint;
|
||||
import org.aspectj.lang.annotation.Around;
|
||||
@@ -15,6 +17,11 @@ import org.springframework.web.context.request.RequestContextHolder;
|
||||
import org.springframework.web.context.request.ServletRequestAttributes;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
@Aspect
|
||||
@Component
|
||||
@Order(1)
|
||||
@@ -22,9 +29,19 @@ public class ControllerUserContextAspect {
|
||||
|
||||
private static final Logger log = LoggerFactory.getLogger(ControllerUserContextAspect.class);
|
||||
|
||||
private static final ObjectMapper MAPPER = new ObjectMapper();
|
||||
|
||||
/**
|
||||
* 外部认证框架(yunhai platform)把解析好的 BoaUser 放进 request attribute 的候选 key。
|
||||
* 如果实际 key 不在此列表中,请在配置/网关侧确认后补充,或改用 Header 解析。
|
||||
*/
|
||||
private static final List<String> BOA_USER_ATTR_KEYS = Arrays.asList(
|
||||
"boaUser", "boauser", "currentUser", "user", "authUser", "loginUser"
|
||||
);
|
||||
|
||||
@Around("execution(* com.yunhai.bschclassmanage.controller..*.*(..))")
|
||||
public Object setUserContext(ProceedingJoinPoint joinPoint) throws Throwable {
|
||||
// 1. 从方法参数中提取 BoaUser 和 UserSysRole
|
||||
// 1. 优先从方法参数中提取 BoaUser 和 UserSysRole(兼容旧写法)
|
||||
Object[] args = joinPoint.getArgs();
|
||||
BoaUser boaUser = null;
|
||||
UserSysRole userSysRole = null;
|
||||
@@ -37,7 +54,26 @@ public class ControllerUserContextAspect {
|
||||
}
|
||||
}
|
||||
|
||||
// 2. 填充 UserContext
|
||||
// 2. 参数缺失时,优先从框架静态持有者 BoaUserContext 取身份(等价于原 BoaUser 参数解析器),
|
||||
// 再回退到请求属性 / currentrole,确保去掉方法参数后仍能正确填充 UserContext。
|
||||
if (boaUser == null) {
|
||||
try {
|
||||
boaUser = BoaUserContext.get();
|
||||
} catch (Throwable ignore) {
|
||||
boaUser = null;
|
||||
}
|
||||
}
|
||||
HttpServletRequest request = getCurrentHttpRequest();
|
||||
if (request != null) {
|
||||
if (boaUser == null) {
|
||||
boaUser = resolveBoaUserFromRequest(request);
|
||||
}
|
||||
if (userSysRole == null) {
|
||||
userSysRole = resolveUserSysRoleFromRequest(request);
|
||||
}
|
||||
}
|
||||
|
||||
// 3. 填充 UserContext
|
||||
populateUserContext(boaUser, userSysRole);
|
||||
|
||||
try {
|
||||
@@ -48,6 +84,42 @@ public class ControllerUserContextAspect {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 currentrole(URL 编码,Header 或 Query 参数)解析 UserSysRole。
|
||||
*/
|
||||
private UserSysRole resolveUserSysRoleFromRequest(HttpServletRequest request) {
|
||||
String raw = request.getHeader("currentrole");
|
||||
boolean fromQuery = false;
|
||||
if (raw == null) {
|
||||
raw = request.getParameter("currentrole");
|
||||
fromQuery = true;
|
||||
}
|
||||
if (raw == null) {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
String decoded = fromQuery ? raw : URLDecoder.decode(raw, StandardCharsets.UTF_8.name());
|
||||
return MAPPER.readValue(decoded, UserSysRole.class);
|
||||
} catch (Exception e) {
|
||||
log.warn("解析 currentrole 失败: {}", e.getMessage());
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 从 request attribute(外部框架注入的 BoaUser)回退解析。
|
||||
* 如需要时也可改为从某个固定 Header 解析,请按实际接入方式调整。
|
||||
*/
|
||||
private BoaUser resolveBoaUserFromRequest(HttpServletRequest request) {
|
||||
for (String key : BOA_USER_ATTR_KEYS) {
|
||||
Object attr = request.getAttribute(key);
|
||||
if (attr instanceof BoaUser) {
|
||||
return (BoaUser) attr;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
private HttpServletRequest getCurrentHttpRequest() {
|
||||
ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
|
||||
return attributes != null ? attributes.getRequest() : null;
|
||||
@@ -69,6 +141,8 @@ public class ControllerUserContextAspect {
|
||||
log.debug("UserContext set roleId={}, schId={}", userSysRole.getRoleId(), userSysRole.getSchId());
|
||||
}
|
||||
|
||||
UserContext.setType(boaUser != null ? boaUser.getType() : null);
|
||||
|
||||
String clientIp = OtherUtil.getClientIp();
|
||||
UserContext.setIp(clientIp);
|
||||
log.debug("Client IP set: {}", clientIp);
|
||||
|
||||
Reference in New Issue
Block a user