feat(护栏): 鉴权覆盖审计从"只 WARN"升级为增量即拒绝启动
SecurityConfig 是 anyRequest().permitAll(),方法级 require* 只有 82 处对 401 个 handler, 所以"某个端点没被任何守卫前缀覆盖"这件事只能靠启动自检发现。审计器原先对 uncovered 只打一行 WARN,等于谁忘了登记也不会有人被打断。 改成:存量 uncovered 落 29 条快照进版本控制,profile=server 下**增量**非空即 IllegalStateException (锚点 [api-guard-coverage-breach])。只拦增量、不拦存量,是为了不重演 10-07 那种"两台起不来"的 41 分钟宕机;判 server profile 复用 JwtService 的同一口径,不另立一套。
This commit is contained in:
@@ -0,0 +1,106 @@
|
||||
package com.nanri.aiimage.config;
|
||||
|
||||
import org.springframework.beans.factory.support.GenericBeanDefinition;
|
||||
import org.springframework.context.annotation.ClassPathScanningCandidateComponentProvider;
|
||||
import org.springframework.context.support.GenericApplicationContext;
|
||||
import org.springframework.core.annotation.AnnotatedElementUtils;
|
||||
import org.springframework.core.type.filter.AnnotationTypeFilter;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashSet;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
/**
|
||||
* 不连库、不起容器,把「Spring MVC 真正会登记的映射」原样枚举出来。
|
||||
*
|
||||
* <p>为什么不用正则扫源码:{@link ApiGuardCoverageAuditor} 判的是 {@code handlerMapping.getPatternValues()},
|
||||
* 类级 {@code @RequestMapping} 与方法级路径的拼接、{@code {"/a","/b"}} 多值、方法级空路径这些细节由 Spring 自己算。
|
||||
* 自己抄一份正则就会和真实映射慢慢脱钩——而存量快照一旦比真实集合少一条,
|
||||
* 现网就变成"新增未设防端点 ⇒ 拒绝启动"的假事故(两台同时起不来=重启窗口事故重演)。
|
||||
*
|
||||
* <p>做法:把控制器注册成 bean <b>定义</b> 并标成懒加载,再让真实的
|
||||
* {@link RequestMappingHandlerMapping#afterPropertiesSet()} 去发现它们。
|
||||
* {@code preInstantiateSingletons} 跳过懒加载定义 ⇒ 控制器不被实例化(它们的构造器要 DataSource/Redis),
|
||||
* 登记的 HandlerMethod 只持 beanName ⇒ 不需要 DB / Redis / RocketMQ,也不需要 {@code @SpringBootTest}。
|
||||
*
|
||||
* <p>这里刻意只用 {@code getMethod().getDeclaringClass()},不碰 {@code HandlerMethod#getBeanType()}——
|
||||
* 后者会把 bean 解析出来,就把"不实例化"这个前提破坏了。
|
||||
*/
|
||||
final class MappedPathProbe {
|
||||
|
||||
static final String BASE_PACKAGE = "com.nanri.aiimage";
|
||||
|
||||
private MappedPathProbe() {
|
||||
}
|
||||
|
||||
/** Spring 登记的全部映射(路径模式 × 处理器方法),与现网 handlerMapping.getHandlerMethods() 同口径。 */
|
||||
static Map<RequestMappingInfo, String> handlerMethods() {
|
||||
GenericApplicationContext ctx = new GenericApplicationContext();
|
||||
for (Class<?> controller : controllerClasses()) {
|
||||
GenericBeanDefinition def = new GenericBeanDefinition();
|
||||
def.setBeanClass(controller);
|
||||
def.setLazyInit(true);
|
||||
ctx.registerBeanDefinition("probe-" + controller.getName(), def);
|
||||
}
|
||||
ctx.refresh();
|
||||
RequestMappingHandlerMapping mapping = new RequestMappingHandlerMapping();
|
||||
mapping.setApplicationContext(ctx);
|
||||
mapping.afterPropertiesSet();
|
||||
Map<RequestMappingInfo, String> out = new java.util.LinkedHashMap<>();
|
||||
try {
|
||||
mapping.getHandlerMethods().forEach((info, handlerMethod) -> out.put(info,
|
||||
handlerMethod.getMethod().getDeclaringClass().getSimpleName() + "#" + handlerMethod.getMethod().getName()));
|
||||
} finally {
|
||||
ctx.close();
|
||||
}
|
||||
return out;
|
||||
}
|
||||
|
||||
/** @return 去重后的请求路径模式(审计器的分母单位)。 */
|
||||
static Set<String> mappedPaths() {
|
||||
Set<String> paths = new LinkedHashSet<>();
|
||||
handlerMethods().keySet().forEach(info -> paths.addAll(info.getPatternValues()));
|
||||
return paths;
|
||||
}
|
||||
|
||||
/** 「路径 -> 处理器」逐条清单,供与现网 handler 数对账、也供快照生成用。 */
|
||||
static List<String> mappingLines() {
|
||||
List<String> out = new ArrayList<>();
|
||||
handlerMethods().forEach((info, handler) ->
|
||||
info.getPatternValues().forEach(p -> out.add(p + " -> " + handler)));
|
||||
out.sort(java.util.Comparator.naturalOrder());
|
||||
return out;
|
||||
}
|
||||
|
||||
/** 控制器类清单:判据与 {@code RequestMappingHandlerMapping#isHandler} 对齐。 */
|
||||
static List<Class<?>> controllerClasses() {
|
||||
ClassPathScanningCandidateComponentProvider scanner =
|
||||
new ClassPathScanningCandidateComponentProvider(false);
|
||||
scanner.addIncludeFilter(new AnnotationTypeFilter(RestController.class));
|
||||
scanner.addIncludeFilter(new AnnotationTypeFilter(Controller.class));
|
||||
List<Class<?>> out = new ArrayList<>();
|
||||
scanner.findCandidateComponents(BASE_PACKAGE).forEach(cd -> {
|
||||
try {
|
||||
Class<?> type = Class.forName(cd.getBeanClassName());
|
||||
boolean handler = AnnotatedElementUtils.hasAnnotation(type, Controller.class)
|
||||
|| AnnotatedElementUtils.hasAnnotation(type, RequestMapping.class);
|
||||
// @ControllerAdvice / @RestControllerAdvice 不是 handler,MVC 不会为它登记映射
|
||||
boolean advice = AnnotatedElementUtils.hasAnnotation(type, ControllerAdvice.class);
|
||||
if (handler && !advice) {
|
||||
out.add(type);
|
||||
}
|
||||
} catch (ClassNotFoundException e) {
|
||||
throw new IllegalStateException("控制器类加载失败:" + cd.getBeanClassName(), e);
|
||||
}
|
||||
});
|
||||
return out;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user