|
18 | 18 |
|
19 | 19 | import java.util.List;
|
20 | 20 |
|
| 21 | +import jakarta.servlet.Filter; |
| 22 | +import jakarta.servlet.http.HttpServletRequest; |
| 23 | + |
| 24 | +import org.springframework.beans.BeanMetadataElement; |
21 | 25 | import org.springframework.beans.BeansException;
|
| 26 | +import org.springframework.beans.factory.FactoryBean; |
| 27 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 28 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 29 | +import org.springframework.beans.factory.config.RuntimeBeanReference; |
| 30 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 31 | +import org.springframework.beans.factory.support.BeanDefinitionRegistry; |
| 32 | +import org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor; |
| 33 | +import org.springframework.beans.factory.support.ManagedList; |
22 | 34 | import org.springframework.context.ApplicationContext;
|
23 | 35 | import org.springframework.context.ApplicationContextAware;
|
24 | 36 | import org.springframework.context.annotation.Bean;
|
25 | 37 | import org.springframework.context.expression.BeanFactoryResolver;
|
26 | 38 | import org.springframework.expression.BeanResolver;
|
27 | 39 | import org.springframework.security.core.context.SecurityContextHolder;
|
28 | 40 | import org.springframework.security.core.context.SecurityContextHolderStrategy;
|
| 41 | +import org.springframework.security.web.FilterChainProxy; |
| 42 | +import org.springframework.security.web.SecurityFilterChain; |
| 43 | +import org.springframework.security.web.access.HandlerMappingIntrospectorRequestTransformer; |
| 44 | +import org.springframework.security.web.context.AbstractSecurityWebApplicationInitializer; |
29 | 45 | import org.springframework.security.web.method.annotation.AuthenticationPrincipalArgumentResolver;
|
30 | 46 | import org.springframework.security.web.method.annotation.CsrfTokenArgumentResolver;
|
31 | 47 | import org.springframework.security.web.method.annotation.CurrentSecurityContextArgumentResolver;
|
32 | 48 | import org.springframework.security.web.servlet.support.csrf.CsrfRequestDataValueProcessor;
|
| 49 | +import org.springframework.web.filter.CompositeFilter; |
33 | 50 | import org.springframework.web.method.support.HandlerMethodArgumentResolver;
|
34 | 51 | import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
35 | 52 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
|
| 53 | +import org.springframework.web.servlet.handler.HandlerMappingIntrospector; |
36 | 54 | import org.springframework.web.servlet.support.RequestDataValueProcessor;
|
37 | 55 |
|
38 | 56 | /**
|
|
50 | 68 | */
|
51 | 69 | class WebMvcSecurityConfiguration implements WebMvcConfigurer, ApplicationContextAware {
|
52 | 70 |
|
| 71 | + private static final String HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME = "mvcHandlerMappingIntrospector"; |
| 72 | + |
53 | 73 | private BeanResolver beanResolver;
|
54 | 74 |
|
55 | 75 | private SecurityContextHolderStrategy securityContextHolderStrategy = SecurityContextHolder
|
@@ -84,4 +104,146 @@ public void setApplicationContext(ApplicationContext applicationContext) throws
|
84 | 104 | }
|
85 | 105 | }
|
86 | 106 |
|
| 107 | + /** |
| 108 | + * Used to ensure Spring MVC request matching is cached. |
| 109 | + * |
| 110 | + * Creates a {@link BeanDefinitionRegistryPostProcessor} that detects if a bean named |
| 111 | + * HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME is defined. If so, it moves the |
| 112 | + * AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME to another bean name |
| 113 | + * and then adds a {@link CompositeFilter} that contains |
| 114 | + * {@link HandlerMappingIntrospector#createCacheFilter()} and the original |
| 115 | + * FilterChainProxy under the original Bean name. |
| 116 | + * @return |
| 117 | + */ |
| 118 | + @Bean |
| 119 | + static BeanDefinitionRegistryPostProcessor springSecurityHandlerMappingIntrospectorBeanDefinitionRegistryPostProcessor() { |
| 120 | + return new BeanDefinitionRegistryPostProcessor() { |
| 121 | + @Override |
| 122 | + public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException { |
| 123 | + } |
| 124 | + |
| 125 | + @Override |
| 126 | + public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException { |
| 127 | + if (!registry.containsBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME)) { |
| 128 | + return; |
| 129 | + } |
| 130 | + |
| 131 | + BeanDefinition hmiRequestTransformer = BeanDefinitionBuilder |
| 132 | + .rootBeanDefinition(HandlerMappingIntrospectorRequestTransformer.class) |
| 133 | + .addConstructorArgReference(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME) |
| 134 | + .getBeanDefinition(); |
| 135 | + registry.registerBeanDefinition(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME + "RequestTransformer", |
| 136 | + hmiRequestTransformer); |
| 137 | + |
| 138 | + String filterChainProxyBeanName = AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME |
| 139 | + + "Proxy"; |
| 140 | + BeanDefinition filterChainProxy = registry |
| 141 | + .getBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME); |
| 142 | + registry.registerBeanDefinition(filterChainProxyBeanName, filterChainProxy); |
| 143 | + |
| 144 | + BeanDefinitionBuilder hmiCacheFilterBldr = BeanDefinitionBuilder |
| 145 | + .rootBeanDefinition(HandlerMappingIntrospectorCachFilterFactoryBean.class) |
| 146 | + .setRole(BeanDefinition.ROLE_INFRASTRUCTURE); |
| 147 | + |
| 148 | + ManagedList<BeanMetadataElement> filters = new ManagedList<>(); |
| 149 | + filters.add(hmiCacheFilterBldr.getBeanDefinition()); |
| 150 | + filters.add(new RuntimeBeanReference(filterChainProxyBeanName)); |
| 151 | + BeanDefinitionBuilder compositeSpringSecurityFilterChainBldr = BeanDefinitionBuilder |
| 152 | + .rootBeanDefinition(SpringSecurityFilterCompositeFilter.class) |
| 153 | + .addConstructorArgValue(filters); |
| 154 | + |
| 155 | + registry.removeBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME); |
| 156 | + registry.registerBeanDefinition(AbstractSecurityWebApplicationInitializer.DEFAULT_FILTER_NAME, |
| 157 | + compositeSpringSecurityFilterChainBldr.getBeanDefinition()); |
| 158 | + } |
| 159 | + }; |
| 160 | + } |
| 161 | + |
| 162 | + /** |
| 163 | + * {@link FactoryBean} to defer creation of |
| 164 | + * {@link HandlerMappingIntrospector#createCacheFilter()} |
| 165 | + */ |
| 166 | + static class HandlerMappingIntrospectorCachFilterFactoryBean |
| 167 | + implements ApplicationContextAware, FactoryBean<Filter> { |
| 168 | + |
| 169 | + private ApplicationContext applicationContext; |
| 170 | + |
| 171 | + @Override |
| 172 | + public void setApplicationContext(ApplicationContext applicationContext) { |
| 173 | + this.applicationContext = applicationContext; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public Filter getObject() throws Exception { |
| 178 | + HandlerMappingIntrospector handlerMappingIntrospector = this.applicationContext |
| 179 | + .getBean(HANDLER_MAPPING_INTROSPECTOR_BEAN_NAME, HandlerMappingIntrospector.class); |
| 180 | + return handlerMappingIntrospector.createCacheFilter(); |
| 181 | + } |
| 182 | + |
| 183 | + @Override |
| 184 | + public Class<?> getObjectType() { |
| 185 | + return Filter.class; |
| 186 | + } |
| 187 | + |
| 188 | + } |
| 189 | + |
| 190 | + /** |
| 191 | + * Extension to {@link CompositeFilter} to expose private methods used by Spring |
| 192 | + * Security's test support |
| 193 | + */ |
| 194 | + static class SpringSecurityFilterCompositeFilter extends CompositeFilter { |
| 195 | + |
| 196 | + private FilterChainProxy springSecurityFilterChain; |
| 197 | + |
| 198 | + SpringSecurityFilterCompositeFilter(List<? extends Filter> filters) { |
| 199 | + setFilters(filters); // for the parent |
| 200 | + } |
| 201 | + |
| 202 | + @Override |
| 203 | + public void setFilters(List<? extends Filter> filters) { |
| 204 | + super.setFilters(filters); |
| 205 | + this.springSecurityFilterChain = findFilterChainProxy(filters); |
| 206 | + } |
| 207 | + |
| 208 | + /** |
| 209 | + * Used through reflection by Spring Security's Test support to lookup the |
| 210 | + * FilterChainProxy Filters for a specific HttpServletRequest. |
| 211 | + * @param request |
| 212 | + * @return |
| 213 | + */ |
| 214 | + private List<? extends Filter> getFilters(HttpServletRequest request) { |
| 215 | + List<SecurityFilterChain> filterChains = getFilterChainProxy().getFilterChains(); |
| 216 | + for (SecurityFilterChain chain : filterChains) { |
| 217 | + if (chain.matches(request)) { |
| 218 | + return chain.getFilters(); |
| 219 | + } |
| 220 | + } |
| 221 | + return null; |
| 222 | + } |
| 223 | + |
| 224 | + /** |
| 225 | + * Used by Spring Security's Test support to find the FilterChainProxy |
| 226 | + * @return |
| 227 | + */ |
| 228 | + private FilterChainProxy getFilterChainProxy() { |
| 229 | + return this.springSecurityFilterChain; |
| 230 | + } |
| 231 | + |
| 232 | + /** |
| 233 | + * Find the FilterChainProxy in a List of Filter |
| 234 | + * @param filters |
| 235 | + * @return non-null FilterChainProxy |
| 236 | + * @throws IllegalStateException if the FilterChainProxy cannot be found |
| 237 | + */ |
| 238 | + private static FilterChainProxy findFilterChainProxy(List<? extends Filter> filters) { |
| 239 | + for (Filter filter : filters) { |
| 240 | + if (filter instanceof FilterChainProxy fcp) { |
| 241 | + return fcp; |
| 242 | + } |
| 243 | + } |
| 244 | + throw new IllegalStateException("Couldn't find FilterChainProxy in " + filters); |
| 245 | + } |
| 246 | + |
| 247 | + } |
| 248 | + |
87 | 249 | }
|
0 commit comments