|
| 1 | +/* |
| 2 | + * Copyright 2012-2024 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package org.springframework.boot.autoconfigure.security.servlet; |
| 18 | + |
| 19 | +import java.util.Arrays; |
| 20 | +import java.util.List; |
| 21 | + |
| 22 | +import org.springframework.beans.factory.config.ConfigurableListableBeanFactory; |
| 23 | +import org.springframework.boot.autoconfigure.AutoConfiguration; |
| 24 | +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; |
| 25 | +import org.springframework.boot.autoconfigure.condition.ConditionMessage; |
| 26 | +import org.springframework.boot.autoconfigure.condition.ConditionOutcome; |
| 27 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnBean; |
| 28 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnClass; |
| 29 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean; |
| 30 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
| 31 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type; |
| 32 | +import org.springframework.boot.autoconfigure.condition.SpringBootCondition; |
| 33 | +import org.springframework.boot.autoconfigure.web.servlet.WebMvcProperties; |
| 34 | +import org.springframework.boot.context.properties.EnableConfigurationProperties; |
| 35 | +import org.springframework.context.annotation.Bean; |
| 36 | +import org.springframework.context.annotation.ConditionContext; |
| 37 | +import org.springframework.context.annotation.Conditional; |
| 38 | +import org.springframework.context.annotation.Configuration; |
| 39 | +import org.springframework.core.Ordered; |
| 40 | +import org.springframework.core.annotation.Order; |
| 41 | +import org.springframework.core.type.AnnotatedTypeMetadata; |
| 42 | +import org.springframework.security.web.servlet.util.matcher.MvcRequestMatcher; |
| 43 | +import org.springframework.security.web.util.matcher.RequestMatcher; |
| 44 | +import org.springframework.web.servlet.DispatcherServlet; |
| 45 | +import org.springframework.web.servlet.handler.HandlerMappingIntrospector; |
| 46 | + |
| 47 | +/** |
| 48 | + * {@link EnableAutoConfiguration Auto-configuration} for {@link RequestMatcher}. |
| 49 | + * |
| 50 | + * @author Wang Zhiyang |
| 51 | + * @since 3.3.0 |
| 52 | + */ |
| 53 | +@AutoConfiguration(after = SecurityAutoConfiguration.class) |
| 54 | +@ConditionalOnWebApplication(type = Type.SERVLET) |
| 55 | +public class SecurityRequestMatcherAutoConfiguration { |
| 56 | + |
| 57 | + @Configuration(proxyBeanMethods = false) |
| 58 | + @ConditionalOnClass(DispatcherServlet.class) |
| 59 | + @EnableConfigurationProperties(WebMvcProperties.class) |
| 60 | + public static class SecurityMvcRequestMatcherConfiguration { |
| 61 | + |
| 62 | + @Bean |
| 63 | + @ConditionalOnClass({ DispatcherServlet.class, HandlerMappingIntrospector.class }) |
| 64 | + @Conditional(ExactlyOneDispatcherServletCondition.class) |
| 65 | + @ConditionalOnBean(HandlerMappingIntrospector.class) |
| 66 | + @ConditionalOnMissingBean |
| 67 | + MvcRequestMatcher.Builder mvcRequestMatcherBuilder(HandlerMappingIntrospector introspector, |
| 68 | + WebMvcProperties webMvcProperties) { |
| 69 | + String servletPath = webMvcProperties.getServlet().getPath(); |
| 70 | + MvcRequestMatcher.Builder mvc = new MvcRequestMatcher.Builder(introspector); |
| 71 | + return ("/".equals(servletPath)) ? mvc : mvc.servletPath(servletPath); |
| 72 | + } |
| 73 | + |
| 74 | + } |
| 75 | + |
| 76 | + @Order(Ordered.LOWEST_PRECEDENCE - 10) |
| 77 | + private static final class ExactlyOneDispatcherServletCondition extends SpringBootCondition { |
| 78 | + |
| 79 | + @Override |
| 80 | + public ConditionOutcome getMatchOutcome(ConditionContext context, AnnotatedTypeMetadata metadata) { |
| 81 | + |
| 82 | + ConditionMessage.Builder message = ConditionMessage.forCondition("Exactly One DispatcherServlet"); |
| 83 | + ConfigurableListableBeanFactory beanFactory = context.getBeanFactory(); |
| 84 | + List<String> dispatchServletBeans = Arrays.asList(beanFactory.getBeanNamesForType(DispatcherServlet.class)); |
| 85 | + List<String> dispatchServletSingletonBeans = Arrays |
| 86 | + .asList(beanFactory.getBeanNamesForType(DispatcherServlet.class, false, false)); |
| 87 | + if (dispatchServletSingletonBeans.size() < dispatchServletBeans.size()) { |
| 88 | + return ConditionOutcome.noMatch(message.foundExactly("scope not singleton bean")); |
| 89 | + } |
| 90 | + else if (dispatchServletSingletonBeans.size() == 1 && dispatchServletBeans.size() == 1) { |
| 91 | + return ConditionOutcome.match(message.found("single bean").items(dispatchServletBeans.get(0))); |
| 92 | + } |
| 93 | + else if (dispatchServletSingletonBeans.isEmpty()) { |
| 94 | + return ConditionOutcome |
| 95 | + .noMatch(message.foundExactly("non dispatcher servlet bean that scope is singleton")); |
| 96 | + } |
| 97 | + else { |
| 98 | + return ConditionOutcome |
| 99 | + .noMatch(message.found("multiple dispatcher servlet bean that scope is singleton") |
| 100 | + .items(dispatchServletSingletonBeans)); |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments