|
29 | 29 | import org.springframework.beans.BeanMetadataElement;
|
30 | 30 | import org.springframework.beans.BeansException;
|
31 | 31 | import org.springframework.beans.factory.FactoryBean;
|
32 |
| -import org.springframework.beans.factory.aot.BeanRegistrationExcludeFilter; |
33 | 32 | import org.springframework.beans.factory.config.BeanDefinition;
|
34 | 33 | import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
|
35 | 34 | import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
36 | 35 | import org.springframework.beans.factory.support.BeanDefinitionRegistry;
|
37 | 36 | import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor;
|
38 | 37 | import org.springframework.beans.factory.support.ManagedList;
|
39 |
| -import org.springframework.beans.factory.support.RegisteredBean; |
40 | 38 | import org.springframework.context.ApplicationContext;
|
41 | 39 | import org.springframework.context.ApplicationContextAware;
|
42 | 40 | import org.springframework.context.annotation.Bean;
|
@@ -113,86 +111,67 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
|
113 | 111 | }
|
114 | 112 | }
|
115 | 113 |
|
116 |
| - @Bean |
117 |
| - static SpringSecurityHandlerMappingIntrospectorBeanDefinitionRegistryPostProcessor springSecurityHandlerMappingIntrospectorBeanDefinitionRegistryPostProcessor() { |
118 |
| - return new SpringSecurityHandlerMappingIntrospectorBeanDefinitionRegistryPostProcessor(); |
119 |
| - } |
120 |
| - |
121 | 114 | /**
|
122 |
| - * Used to ensure Spring MVC request matching is cached. Creates a |
123 |
| - * {@link BeanDefinitionRegistryPostProcessor} that detects if a bean named |
| 115 | + * Used to ensure Spring MVC request matching is cached. |
| 116 | + * |
| 117 | + * Creates a {@link BeanDefinitionRegistryPostProcessor} that detects if a bean named |
124 | 118 | * HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME is defined. If so, it moves the
|
125 | 119 | * AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME to another bean name
|
126 | 120 | * and then adds a {@link CompositeFilter} that contains
|
127 | 121 | * {@link HandlerMappingIntrospector#createCacheFilter()} and the original
|
128 | 122 | * FilterChainProxy under the original Bean name.
|
129 |
| - * |
130 | 123 | * @return
|
131 | 124 | */
|
132 |
| - static class SpringSecurityHandlerMappingIntrospectorBeanDefinitionRegistryPostProcessor |
133 |
| - implements BeanDefinitionRegistryPostProcessor { |
134 |
| - |
135 |
| - @Override |
136 |
| - public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { |
137 |
| - if (!registry.containsBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)) { |
138 |
| - return; |
| 125 | + @Bean |
| 126 | + static BeanDefinitionRegistryPostProcessor springSecurityHandlerMappingIntrospectorBeanDefinitionRegistryPostProcessor() { |
| 127 | + return new BeanDefinitionRegistryPostProcessor() { |
| 128 | + @Override |
| 129 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
139 | 130 | }
|
140 | 131 |
|
141 |
| - BeanDefinition hmiRequestTransformer = BeanDefinitionBuilder |
142 |
| - .rootBeanDefinition(HandlerMappingIntrospectorRequestTransformer.class) |
143 |
| - .addConstructorArgReference(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME) |
144 |
| - .getBeanDefinition(); |
145 |
| - registry.registerBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME + "RequestTransformer", |
146 |
| - hmiRequestTransformer); |
147 |
| - |
148 |
| - BeanDefinition filterChainProxy = registry |
149 |
| - .getBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME); |
150 |
| - |
151 |
| - BeanDefinitionBuilder hmiCacheFilterBldr = BeanDefinitionBuilder |
152 |
| - .rootBeanDefinition(HandlerMappingIntrospectorCachFilterFactoryBean.class) |
153 |
| - .setRole(BeanDefinition.ROLE_INFRASTRUCTURE); |
154 |
| - |
155 |
| - ManagedList<BeanMetadataElement> filters = new ManagedList<>(); |
156 |
| - filters.add(hmiCacheFilterBldr.getBeanDefinition()); |
157 |
| - filters.add(filterChainProxy); |
158 |
| - BeanDefinitionBuilder compositeSpringSecurityFilterChainBldr = BeanDefinitionBuilder |
159 |
| - .rootBeanDefinition(CompositeFilterChainProxy.class) |
160 |
| - .addConstructorArgValue(filters); |
161 |
| - |
162 |
| - registry.removeBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME); |
163 |
| - registry.registerBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME, |
164 |
| - compositeSpringSecurityFilterChainBldr.getBeanDefinition()); |
165 |
| - } |
166 |
| - |
167 |
| - @Override |
168 |
| - public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 132 | + @Override |
| 133 | + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { |
| 134 | + if (!registry.containsBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)) { |
| 135 | + return; |
| 136 | + } |
169 | 137 |
|
170 |
| - } |
| 138 | + String hmiRequestTransformerBeanName = HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME + "RequestTransformer"; |
| 139 | + if (!registry.containsBeanDefinition(hmiRequestTransformerBeanName)) { |
| 140 | + BeanDefinition hmiRequestTransformer = BeanDefinitionBuilder |
| 141 | + .rootBeanDefinition(HandlerMappingIntrospectorRequestTransformer.class) |
| 142 | + .addConstructorArgReference(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME) |
| 143 | + .getBeanDefinition(); |
| 144 | + registry.registerBeanDefinition(hmiRequestTransformerBeanName, hmiRequestTransformer); |
| 145 | + } |
171 | 146 |
|
172 |
| - } |
| 147 | + BeanDefinition filterChainProxy = registry |
| 148 | + .getBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME); |
173 | 149 |
|
174 |
| - /** |
175 |
| - * Used to exclude the |
176 |
| - * {@link SpringSecurityHandlerMappingIntrospectorBeanDefinitionRegistryPostProcessor} |
177 |
| - * from AOT processing. See <a href= |
178 |
| - * "https://github.com/spring-projects/spring-security/issues/14362">gh-14362</a> |
179 |
| - */ |
180 |
| - static class SpringSecurityHandlerMappingIntrospectorBeanRegistrationExcludeFilter |
181 |
| - implements BeanRegistrationExcludeFilter { |
| 150 | + if (!filterChainProxy.getResolvableType().isInstance(CompositeFilterChainProxy.class)) { |
| 151 | + BeanDefinitionBuilder hmiCacheFilterBldr = BeanDefinitionBuilder |
| 152 | + .rootBeanDefinition(HandlerMappingIntrospectorCacheFilterFactoryBean.class) |
| 153 | + .setRole(BeanDefinition.ROLE_INFRASTRUCTURE); |
182 | 154 |
|
183 |
| - @Override |
184 |
| - public boolean isExcludedFromAotProcessing(RegisteredBean registeredBean) { |
185 |
| - Class<?> beanClass = registeredBean.getBeanClass(); |
186 |
| - return SpringSecurityHandlerMappingIntrospectorBeanDefinitionRegistryPostProcessor.class == beanClass; |
187 |
| - } |
| 155 | + ManagedList<BeanMetadataElement> filters = new ManagedList<>(); |
| 156 | + filters.add(hmiCacheFilterBldr.getBeanDefinition()); |
| 157 | + filters.add(filterChainProxy); |
| 158 | + BeanDefinitionBuilder compositeSpringSecurityFilterChainBldr = BeanDefinitionBuilder |
| 159 | + .rootBeanDefinition(CompositeFilterChainProxy.class) |
| 160 | + .addConstructorArgValue(filters); |
188 | 161 |
|
| 162 | + registry.removeBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME); |
| 163 | + registry.registerBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME, |
| 164 | + compositeSpringSecurityFilterChainBldr.getBeanDefinition()); |
| 165 | + } |
| 166 | + } |
| 167 | + }; |
189 | 168 | }
|
190 | 169 |
|
191 | 170 | /**
|
192 | 171 | * {@link FactoryBean} to defer creation of
|
193 | 172 | * {@link HandlerMappingIntrospector#createCacheFilter()}
|
194 | 173 | */
|
195 |
| - static class HandlerMappingIntrospectorCachFilterFactoryBean |
| 174 | + static class HandlerMappingIntrospectorCacheFilterFactoryBean |
196 | 175 | implements ApplicationContextAware, FactoryBean<Filter> {
|
197 | 176 |
|
198 | 177 | private ApplicationContext applicationContext;
|
|
0 commit comments