feat(auth): P2-2 Spring Security 无状态 JWT 认证链路 + UserSession 引擎上下文适配
- jjwt 0.12.6 HS256;密钥配置化(EIP4_AUTH_JWT_SECRET),启动期强度校验,不入仓库 - 密码兼容:复刻 framework EncryptUtil 无盐 MD5 + 常量时间比对(侦察证据见报告) - JwtAuthFilter:SecurityContext + 引擎 UserSession(静态 ThreadLocal)请求级 set/finally clear,16线程×200次并发隔离单测 - SecurityConfig 双链:prod 严格 401 / 非 prod 保留对拍测试通道(单测断言生产关闭) - EntityController.buildCtx 三级优先(asUser > JWT > ADMIN),修复 JWT ctx 被 ADMIN 默认覆盖 - 实测:登录签发/过滤 16 行与 DB 吻合/伪造过期全 401;ci.sh 全绿;手册新增 §5.29-5.31 Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
This commit is contained in:
@@ -130,25 +130,41 @@ public class EntityController {
|
||||
}
|
||||
|
||||
/**
|
||||
* 构造调用上下文。无 asUser → ADMIN 视角(不触发过滤);有 asUser → 非 ADMIN 视角,
|
||||
* 把 CURRENTUSER 与 ROLES(List)放入 Properties,交由引擎按 SYS_FILTERRULE 规则过滤。
|
||||
* 构造调用上下文,优先级:
|
||||
* <ol>
|
||||
* <li><b>显式 asUser 查询参</b>(C2 对拍测试通道,CI 依赖)→ 非 ADMIN 视角,触发行级过滤。</li>
|
||||
* <li><b>JWT 已绑定的引擎上下文</b>:{@code JwtAuthFilter} 已把 JWT claims 写入
|
||||
* 请求级 {@code UserSession}(CURRENTUSER=真实 userId + ROLES)。无 asUser 时
|
||||
* 直接复用该绑定上下文,使<strong>JWT roles 替代 asUser/roles 参数</strong>驱动行级过滤。</li>
|
||||
* <li><b>都没有</b>(无令牌、无 asUser)→ ADMIN 视角,不过滤(与原默认行为一致)。</li>
|
||||
* </ol>
|
||||
*/
|
||||
private Properties buildCtx(String asUser, String roles) {
|
||||
if (asUser == null || asUser.isBlank()) {
|
||||
return UserSession.getAdmin();
|
||||
}
|
||||
Properties ctx = new Properties();
|
||||
ctx.setProperty(LoggedInUser.CURRENTUSER, asUser);
|
||||
List<String> roleList = new ArrayList<>();
|
||||
if (roles != null && !roles.isBlank()) {
|
||||
for (String r : roles.split(",")) {
|
||||
if (!r.isBlank()) {
|
||||
roleList.add(r.trim());
|
||||
// 1) 显式 asUser 测试通道优先(保持 C2 既有行为不变)
|
||||
if (asUser != null && !asUser.isBlank()) {
|
||||
Properties ctx = new Properties();
|
||||
ctx.setProperty(LoggedInUser.CURRENTUSER, asUser);
|
||||
List<String> roleList = new ArrayList<>();
|
||||
if (roles != null && !roles.isBlank()) {
|
||||
for (String r : roles.split(",")) {
|
||||
if (!r.isBlank()) {
|
||||
roleList.add(r.trim());
|
||||
}
|
||||
}
|
||||
}
|
||||
ctx.put(LoggedInUser.ROLES, roleList);
|
||||
return ctx;
|
||||
}
|
||||
ctx.put(LoggedInUser.ROLES, roleList);
|
||||
return ctx;
|
||||
// 2) JWT 过滤器已绑定的请求级上下文(非 ADMIN 真实用户)→ 复用,驱动行级过滤
|
||||
Properties bound = UserSession.get();
|
||||
if (bound != null) {
|
||||
String cu = bound.getProperty(LoggedInUser.CURRENTUSER);
|
||||
if (cu != null && !cu.isBlank() && !LoggedInUser.ADMIN.trim().equals(cu.trim())) {
|
||||
return bound;
|
||||
}
|
||||
}
|
||||
// 3) 默认 ADMIN 视角(无令牌、无 asUser)
|
||||
return UserSession.getAdmin();
|
||||
}
|
||||
|
||||
/** GeneralPOJO 值存于内部数组(非 bean getter),手工展开为平面 map 供 JSON 序列化。 */
|
||||
|
||||
Reference in New Issue
Block a user