Make springSecurityHandlerMappingIntrospectorBeanDefinitionRegistryPostProcessor passive

Instead of excluding the bean from AOT processing, we avoid redefining the beans if they are present or in the expected state.

Issue gh-14362
This commit is contained in:
Marcus Hert Da Coregio 2023-12-26 15:58:16 -03:00
parent 778a63a763
commit 92af758f1f
5 changed files with 38 additions and 23 deletions

View File

@ -135,30 +135,34 @@ class WebMvcSecurityConfiguration implements WebMvcConfigurer, ApplicationContex
return; return;
} }
BeanDefinition hmiRequestTransformer = BeanDefinitionBuilder String hmiRequestTransformerBeanName = HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME + "RequestTransformer";
.rootBeanDefinition(HandlerMappingIntrospectorRequestTransformer.class) if (!registry.containsBeanDefinition(hmiRequestTransformerBeanName)) {
.addConstructorArgReference(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME) BeanDefinition hmiRequestTransformer = BeanDefinitionBuilder
.getBeanDefinition(); .rootBeanDefinition(HandlerMappingIntrospectorRequestTransformer.class)
registry.registerBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME + "RequestTransformer", .addConstructorArgReference(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)
hmiRequestTransformer); .getBeanDefinition();
registry.registerBeanDefinition(hmiRequestTransformerBeanName, hmiRequestTransformer);
}
BeanDefinition filterChainProxy = registry BeanDefinition filterChainProxy = registry
.getBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME); .getBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME);
BeanDefinitionBuilder hmiCacheFilterBldr = BeanDefinitionBuilder if (!filterChainProxy.getResolvableType().isInstance(CompositeFilterChainProxy.class)) {
.rootBeanDefinition(HandlerMappingIntrospectorCachFilterFactoryBean.class) BeanDefinitionBuilder hmiCacheFilterBldr = BeanDefinitionBuilder
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE); .rootBeanDefinition(HandlerMappingIntrospectorCacheFilterFactoryBean.class)
.setRole(BeanDefinition.ROLE_INFRASTRUCTURE);
ManagedList<BeanMetadataElement> filters = new ManagedList<>(); ManagedList<BeanMetadataElement> filters = new ManagedList<>();
filters.add(hmiCacheFilterBldr.getBeanDefinition()); filters.add(hmiCacheFilterBldr.getBeanDefinition());
filters.add(filterChainProxy); filters.add(filterChainProxy);
BeanDefinitionBuilder compositeSpringSecurityFilterChainBldr = BeanDefinitionBuilder BeanDefinitionBuilder compositeSpringSecurityFilterChainBldr = BeanDefinitionBuilder
.rootBeanDefinition(CompositeFilterChainProxy.class) .rootBeanDefinition(CompositeFilterChainProxy.class)
.addConstructorArgValue(filters); .addConstructorArgValue(filters);
registry.removeBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME); registry.removeBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME);
registry.registerBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME, registry.registerBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME,
compositeSpringSecurityFilterChainBldr.getBeanDefinition()); compositeSpringSecurityFilterChainBldr.getBeanDefinition());
}
} }
}; };
} }
@ -167,7 +171,7 @@ class WebMvcSecurityConfiguration implements WebMvcConfigurer, ApplicationContex
* {@link FactoryBean} to defer creation of * {@link FactoryBean} to defer creation of
* {@link HandlerMappingIntrospector#createCacheFilter()} * {@link HandlerMappingIntrospector#createCacheFilter()}
*/ */
static class HandlerMappingIntrospectorCachFilterFactoryBean static class HandlerMappingIntrospectorCacheFilterFactoryBean
implements ApplicationContextAware, FactoryBean<Filter> { implements ApplicationContextAware, FactoryBean<Filter> {
private ApplicationContext applicationContext; private ApplicationContext applicationContext;

View File

@ -34,7 +34,11 @@ class WebMvcSecurityConfigurationRuntimeHints implements RuntimeHintsRegistrar {
hints.reflection() hints.reflection()
.registerType(TypeReference .registerType(TypeReference
.of("org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration$CompositeFilterChainProxy"), .of("org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration$CompositeFilterChainProxy"),
MemberCategory.INVOKE_DECLARED_METHODS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
hints.reflection()
.registerType(TypeReference
.of("org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration$HandlerMappingIntrospectorCacheFilterFactoryBean"),
MemberCategory.INVOKE_DECLARED_CONSTRUCTORS);
} }
} }

View File

@ -53,4 +53,12 @@ class WebMvcSecurityConfigurationRuntimeHintsTests {
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints); .withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints);
} }
@Test
void handlerMappingIntrospectorCacheFilterFactoryBeanHasHints() {
assertThat(RuntimeHintsPredicates.reflection()
.onType(TypeReference
.of("org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration$HandlerMappingIntrospectorCacheFilterFactoryBean"))
.withMemberCategory(MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)).accepts(this.hints);
}
} }

View File

@ -50,7 +50,7 @@ class WebTestUtilsTestRuntimeHints implements TestRuntimeHintsRegistrar {
hints.reflection() hints.reflection()
.registerType(TypeReference .registerType(TypeReference
.of("org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration$CompositeFilterChainProxy"), .of("org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration$CompositeFilterChainProxy"),
MemberCategory.INVOKE_DECLARED_METHODS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS); MemberCategory.INVOKE_DECLARED_METHODS);
} }
private void registerCsrfTokenRepositoryHints(RuntimeHints hints) { private void registerCsrfTokenRepositoryHints(RuntimeHints hints) {

View File

@ -62,8 +62,7 @@ class WebTestUtilsTestRuntimeHintsTests {
assertThat(RuntimeHintsPredicates.reflection() assertThat(RuntimeHintsPredicates.reflection()
.onType(TypeReference .onType(TypeReference
.of("org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration$CompositeFilterChainProxy")) .of("org.springframework.security.config.annotation.web.configuration.WebMvcSecurityConfiguration$CompositeFilterChainProxy"))
.withMemberCategories(MemberCategory.INVOKE_DECLARED_METHODS, MemberCategory.INVOKE_DECLARED_CONSTRUCTORS)) .withMemberCategory(MemberCategory.INVOKE_DECLARED_METHODS)).accepts(this.hints);
.accepts(this.hints);
} }
@Test @Test