fix(鉴权自检): 审计器按类型注入撞 actuator 第二候选,改显式构造器带 @Qualifier
2026-10-08 把上一版 jar 推到主机B 后容器 CrashLoop 5 次:RequestMappingHandlerMapping 在容器里有两个候选(WebMvc 的 + actuator 的 ControllerEndpointHandlerMapping,后者是前者 子类),按类型注入直接 NoUniqueBeanDefinitionException,而 4044 例单测全绿——因为本仓库 没有一例真正装配过 Spring 容器。 补 ApiGuardCoverageAuditorWiringTest 正反两钉:同一个容器里复现那对 bean,正钉审计器装配 成功且拿到 MVC 那个(拿错会在"0 个端点"上假绿),反钉不带限定符的注入必须真失败 (不钉反例,第二支 bean 一旦不再继承,正例就退化成恒真)。摘掉 @Qualifier 变异实测变红。
This commit is contained in:
+100
@@ -0,0 +1,100 @@
|
||||
package com.nanri.aiimage.config;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.springframework.beans.factory.BeanCreationException;
|
||||
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||
|
||||
import java.lang.reflect.Field;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
import static org.junit.jupiter.api.Assertions.assertSame;
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
/**
|
||||
* {@link ApiGuardCoverageAuditor} 的容器接线测试。
|
||||
*
|
||||
* <p>本仓库 4044 例全是 Mockito 单测,没有一例真正装配过 Spring 容器,所以"构造函数造不造得出来"
|
||||
* 这件事在测试里是空白的。2026-10-08 就是靠这个空白把一支起不来的 jar 推上了主机B:
|
||||
* actuator 也注册了一个 {@code RequestMappingHandlerMapping} 的子类
|
||||
* ({@code ControllerEndpointHandlerMapping}),按类型注入命中 2 个候选 ⇒
|
||||
* {@code NoUniqueBeanDefinitionException} ⇒ 容器不启动,而单测全绿。
|
||||
*
|
||||
* <p>这里在同一个容器里复现那对 bean,正反两钉:
|
||||
* <ol>
|
||||
* <li>正:审计器能装配,且拿到的是 MVC 那一个(不是 actuator 的,后者上没有业务映射,
|
||||
* 自检会在"0 个端点"上安静地报全绿);</li>
|
||||
* <li>反:同一容器里按类型注入(不带限定符)必须真的失败——不钉这一条,
|
||||
* 第二支 bean 只要不再继承 {@code RequestMappingHandlerMapping},上面的"能装配"
|
||||
* 就退化成了恒真断言。</li>
|
||||
* </ol>
|
||||
*/
|
||||
class ApiGuardCoverageAuditorWiringTest {
|
||||
|
||||
/** 跳过 handler 扫描:这里只验接线,不要一个真实的 MVC 上下文。 */
|
||||
static class StubHandlerMapping extends RequestMappingHandlerMapping {
|
||||
@Override
|
||||
public void afterPropertiesSet() {
|
||||
// no-op
|
||||
}
|
||||
}
|
||||
|
||||
/** 与 actuator 的 ControllerEndpointHandlerMapping 同形:它是 RequestMappingHandlerMapping 的子类。 */
|
||||
static class StubEndpointHandlerMapping extends StubHandlerMapping {
|
||||
}
|
||||
|
||||
/** 故意不带限定符的注入方,用来证明容器里的歧义是真的存在。 */
|
||||
static class ByTypeOnlyConsumer {
|
||||
final RequestMappingHandlerMapping mapping;
|
||||
|
||||
ByTypeOnlyConsumer(RequestMappingHandlerMapping mapping) {
|
||||
this.mapping = mapping;
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void auditorWiresWhenActuatorRegistersASecondHandlerMapping() throws Exception {
|
||||
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
|
||||
StubHandlerMapping mvc = new StubHandlerMapping();
|
||||
ctx.registerBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class, () -> mvc);
|
||||
ctx.registerBean("controllerEndpointHandlerMapping", StubEndpointHandlerMapping.class,
|
||||
StubEndpointHandlerMapping::new);
|
||||
ctx.register(ApiGuardCoverageAuditor.class);
|
||||
ctx.refresh();
|
||||
|
||||
// 防恒真前提:这个容器必须确实有 2 个同类型候选,否则下面"装配成功"什么也没说。
|
||||
assertEquals(2, ctx.getBeansOfType(RequestMappingHandlerMapping.class).size(),
|
||||
"容器里没有复现出 2 个 RequestMappingHandlerMapping 候选,本测试已失去意义");
|
||||
|
||||
ApiGuardCoverageAuditor auditor = ctx.getBean(ApiGuardCoverageAuditor.class);
|
||||
assertNotNull(auditor, "审计器没装配起来,线上就是 CrashLoop");
|
||||
assertSame(mvc, injectedMapping(auditor),
|
||||
"必须注入 MVC 那个 handlerMapping;拿到 actuator 的那个时端点集合为空,自检会假绿");
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
void ambiguousInjectionByTypeAloneFailsInTheSameContainer() {
|
||||
try (AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext()) {
|
||||
ctx.registerBean("requestMappingHandlerMapping", RequestMappingHandlerMapping.class,
|
||||
StubHandlerMapping::new);
|
||||
ctx.registerBean("controllerEndpointHandlerMapping", StubEndpointHandlerMapping.class,
|
||||
StubEndpointHandlerMapping::new);
|
||||
ctx.register(ByTypeOnlyConsumer.class);
|
||||
|
||||
BeanCreationException failure = assertThrows(BeanCreationException.class, ctx::refresh,
|
||||
"不带限定符竟然装配成功,说明歧义没复现出来,上一支测试的『成功』是假的");
|
||||
String chain = String.valueOf(failure.getRootCause()) + failure.getMessage();
|
||||
assertTrue(chain.contains("NoUniqueBeanDefinitionException") || chain.contains("expected single matching bean"),
|
||||
() -> "失败原因不是类型歧义,而是别的东西,本测试就没钉住这个坑:" + chain);
|
||||
}
|
||||
}
|
||||
|
||||
private static RequestMappingHandlerMapping injectedMapping(ApiGuardCoverageAuditor auditor) throws Exception {
|
||||
Field field = ApiGuardCoverageAuditor.class.getDeclaredField("handlerMapping");
|
||||
field.setAccessible(true);
|
||||
return (RequestMappingHandlerMapping) field.get(auditor);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user