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