#!/usr/bin/env python3 # 将重编译后的 LoginController.class + 修复后的前端文件注入好包 target/cas.war # 其余条目原样保留,确保 WAR 结构/压缩方式不变。 import os, zipfile, shutil ROOT = r"D:\SoftProject\yunhai\CasSso\cas-overlay-template-5.3" BASE = os.path.join(ROOT, "target", "cas.war") NEW = os.path.join(ROOT, "target", "cas.war.new") CLASSES_DIR = os.path.join(ROOT, "target", "cas", "WEB-INF", "classes", "org", "apereo", "cas", "web", "custom", "controller") FRONT_JS = os.path.join(ROOT, "src", "main", "webapp", "front", "js", "conf.js") FRONT_IDX = os.path.join(ROOT, "src", "main", "webapp", "front", "index.html") replace = { "WEB-INF/classes/org/apereo/cas/web/custom/controller/LoginController.class": os.path.join(CLASSES_DIR, "LoginController.class"), "WEB-INF/classes/org/apereo/cas/web/custom/controller/LoginController$LocalAuthenticationResult.class": os.path.join(CLASSES_DIR, "LoginController$LocalAuthenticationResult.class"), "front/js/conf.js": FRONT_JS, "front/index.html": FRONT_IDX, } # 校验待替换源文件存在 for k, v in replace.items(): if not os.path.isfile(v): raise SystemExit("缺少源文件: %s -> %s" % (k, v)) with zipfile.ZipFile(BASE, "r") as z: names = set(z.namelist()) # 确认条目存在(打印实际命中) for k in replace: print("命中" if k in names else "缺失(!)", k) for k in list(replace): if k not in names: raise SystemExit("基包中找不到条目: " + k) with zipfile.ZipFile(NEW, "w", zipfile.ZIP_DEFLATED) as out: for info in z.infolist(): if info.filename in replace: data = open(replace[info.filename], "rb").read() ni = zipfile.ZipInfo(info.filename) ni.date_time = info.date_time ni.external_attr = info.external_attr ni.compress_type = zipfile.ZIP_DEFLATED out.writestr(ni, data) print("替换:", info.filename, "->", len(data), "bytes") else: out.writestr(info, z.read(info.filename)) print("写盘:", NEW) # 校验新包 with zipfile.ZipFile(NEW, "r") as z: bad = z.testzip() if bad: raise SystemExit("WAR 损坏条目: " + bad) nl = z.namelist() assert "WEB-INF/lib/" in nl or any(n.startswith("WEB-INF/lib/") for n in nl), "缺少 WEB-INF/lib" mf = z.read("META-INF/MANIFEST.MF").decode("utf-8", "replace") print("MANIFEST Main-Class:", [l for l in mf.splitlines() if l.startswith("Main-Class")]) for k in replace: print("存在" if k in nl else "丢失(!)", k) # 原子替换 bak = BASE + ".bak7_31" if not os.path.exists(bak): shutil.move(BASE, bak) shutil.move(NEW, BASE) print("已生成新 target/cas.war, 旧包备份为 cas.war.bak7_31")