|
| 1 | +/* |
| 2 | + * Copyright 2002-2016 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.security.config.http; |
| 18 | + |
| 19 | +import java.util.List; |
| 20 | +import java.util.Map; |
| 21 | + |
| 22 | +import jakarta.servlet.http.HttpServletRequest; |
| 23 | +import org.w3c.dom.Element; |
| 24 | + |
| 25 | +import org.springframework.beans.BeanMetadataElement; |
| 26 | +import org.springframework.beans.factory.config.BeanDefinition; |
| 27 | +import org.springframework.beans.factory.config.RuntimeBeanReference; |
| 28 | +import org.springframework.beans.factory.parsing.BeanComponentDefinition; |
| 29 | +import org.springframework.beans.factory.support.BeanDefinitionBuilder; |
| 30 | +import org.springframework.beans.factory.support.ManagedMap; |
| 31 | +import org.springframework.beans.factory.xml.AbstractBeanDefinitionParser; |
| 32 | +import org.springframework.beans.factory.xml.BeanDefinitionParser; |
| 33 | +import org.springframework.beans.factory.xml.ParserContext; |
| 34 | +import org.springframework.beans.factory.xml.XmlReaderContext; |
| 35 | +import org.springframework.security.authorization.AuthenticatedAuthorizationManager; |
| 36 | +import org.springframework.security.authorization.AuthorizationManager; |
| 37 | +import org.springframework.security.config.Elements; |
| 38 | +import org.springframework.security.web.access.expression.DefaultHttpSecurityExpressionHandler; |
| 39 | +import org.springframework.security.web.access.expression.WebExpressionAuthorizationManager; |
| 40 | +import org.springframework.security.web.access.intercept.AuthorizationFilter; |
| 41 | +import org.springframework.security.web.access.intercept.RequestAuthorizationContext; |
| 42 | +import org.springframework.security.web.access.intercept.RequestMatcherDelegatingAuthorizationManager; |
| 43 | +import org.springframework.security.web.util.matcher.AnyRequestMatcher; |
| 44 | +import org.springframework.security.web.util.matcher.RequestMatcher; |
| 45 | +import org.springframework.util.StringUtils; |
| 46 | +import org.springframework.util.xml.DomUtils; |
| 47 | + |
| 48 | +class AuthorizationFilterParser implements BeanDefinitionParser { |
| 49 | + |
| 50 | + private static final String ATT_USE_EXPRESSIONS = "use-expressions"; |
| 51 | + |
| 52 | + private static final String ATT_HTTP_METHOD = "method"; |
| 53 | + |
| 54 | + private static final String ATT_PATTERN = "pattern"; |
| 55 | + |
| 56 | + private static final String ATT_ACCESS = "access"; |
| 57 | + |
| 58 | + private static final String ATT_SERVLET_PATH = "servlet-path"; |
| 59 | + |
| 60 | + private String authorizationManagerRef; |
| 61 | + |
| 62 | + @Override |
| 63 | + public BeanDefinition parse(Element element, ParserContext parserContext) { |
| 64 | + if (!isUseExpressions(element)) { |
| 65 | + parserContext.getReaderContext().error("AuthorizationManager must be used with `use-expressions=\"true\"", |
| 66 | + element); |
| 67 | + return null; |
| 68 | + } |
| 69 | + this.authorizationManagerRef = createAuthorizationManager(element, parserContext); |
| 70 | + BeanDefinitionBuilder filterBuilder = BeanDefinitionBuilder.rootBeanDefinition(AuthorizationFilter.class); |
| 71 | + filterBuilder.getRawBeanDefinition().setSource(parserContext.extractSource(element)); |
| 72 | + BeanDefinition filter = filterBuilder.addConstructorArgReference(this.authorizationManagerRef) |
| 73 | + .getBeanDefinition(); |
| 74 | + String id = element.getAttribute(AbstractBeanDefinitionParser.ID_ATTRIBUTE); |
| 75 | + if (StringUtils.hasText(id)) { |
| 76 | + parserContext.registerComponent(new BeanComponentDefinition(filter, id)); |
| 77 | + parserContext.getRegistry().registerBeanDefinition(id, filter); |
| 78 | + } |
| 79 | + return filter; |
| 80 | + } |
| 81 | + |
| 82 | + String getAuthorizationManagerRef() { |
| 83 | + return this.authorizationManagerRef; |
| 84 | + } |
| 85 | + |
| 86 | + private String createAuthorizationManager(Element element, ParserContext parserContext) { |
| 87 | + XmlReaderContext context = parserContext.getReaderContext(); |
| 88 | + String authorizationManagerRef = element.getAttribute("authorization-manager-ref"); |
| 89 | + if (StringUtils.hasText(authorizationManagerRef)) { |
| 90 | + return authorizationManagerRef; |
| 91 | + } |
| 92 | + Element expressionHandlerElt = DomUtils.getChildElementByTagName(element, Elements.EXPRESSION_HANDLER); |
| 93 | + String expressionHandlerRef = (expressionHandlerElt != null) ? expressionHandlerElt.getAttribute("ref") : null; |
| 94 | + if (expressionHandlerRef == null) { |
| 95 | + expressionHandlerRef = registerDefaultExpressionHandler(parserContext); |
| 96 | + } |
| 97 | + MatcherType matcherType = MatcherType.fromElement(element); |
| 98 | + ManagedMap<BeanMetadataElement, BeanDefinition> matcherToExpression = new ManagedMap<>(); |
| 99 | + List<Element> interceptMessages = DomUtils.getChildElementsByTagName(element, Elements.INTERCEPT_URL); |
| 100 | + for (Element interceptMessage : interceptMessages) { |
| 101 | + String accessExpression = interceptMessage.getAttribute(ATT_ACCESS); |
| 102 | + BeanDefinitionBuilder authorizationManager = BeanDefinitionBuilder |
| 103 | + .rootBeanDefinition(WebExpressionAuthorizationManager.class); |
| 104 | + authorizationManager.addPropertyReference("expressionHandler", expressionHandlerRef); |
| 105 | + authorizationManager.addConstructorArgValue(accessExpression); |
| 106 | + BeanMetadataElement matcher = createMatcher(matcherType, interceptMessage, parserContext); |
| 107 | + matcherToExpression.put(matcher, authorizationManager.getBeanDefinition()); |
| 108 | + } |
| 109 | + BeanDefinitionBuilder mds = BeanDefinitionBuilder |
| 110 | + .rootBeanDefinition(RequestMatcherDelegatingAuthorizationManagerFactory.class); |
| 111 | + mds.setFactoryMethod("createRequestMatcherDelegatingAuthorizationManager"); |
| 112 | + mds.addConstructorArgValue(matcherToExpression); |
| 113 | + return context.registerWithGeneratedName(mds.getBeanDefinition()); |
| 114 | + } |
| 115 | + |
| 116 | + private BeanMetadataElement createMatcher(MatcherType matcherType, Element urlElt, ParserContext parserContext) { |
| 117 | + String path = urlElt.getAttribute(ATT_PATTERN); |
| 118 | + String matcherRef = urlElt.getAttribute(HttpSecurityBeanDefinitionParser.ATT_REQUEST_MATCHER_REF); |
| 119 | + boolean hasMatcherRef = StringUtils.hasText(matcherRef); |
| 120 | + if (!hasMatcherRef && !StringUtils.hasText(path)) { |
| 121 | + parserContext.getReaderContext().error("path attribute cannot be empty or null", urlElt); |
| 122 | + } |
| 123 | + String method = urlElt.getAttribute(ATT_HTTP_METHOD); |
| 124 | + if (!StringUtils.hasText(method)) { |
| 125 | + method = null; |
| 126 | + } |
| 127 | + String servletPath = urlElt.getAttribute(ATT_SERVLET_PATH); |
| 128 | + if (!StringUtils.hasText(servletPath)) { |
| 129 | + servletPath = null; |
| 130 | + } |
| 131 | + else if (!MatcherType.mvc.equals(matcherType)) { |
| 132 | + parserContext.getReaderContext().error( |
| 133 | + ATT_SERVLET_PATH + " is not applicable for request-matcher: '" + matcherType.name() + "'", urlElt); |
| 134 | + } |
| 135 | + return hasMatcherRef ? new RuntimeBeanReference(matcherRef) |
| 136 | + : matcherType.createMatcher(parserContext, path, method, servletPath); |
| 137 | + } |
| 138 | + |
| 139 | + String registerDefaultExpressionHandler(ParserContext pc) { |
| 140 | + BeanDefinition expressionHandler = GrantedAuthorityDefaultsParserUtils.registerWithDefaultRolePrefix(pc, |
| 141 | + DefaultWebSecurityExpressionHandlerBeanFactory.class); |
| 142 | + String expressionHandlerRef = pc.getReaderContext().generateBeanName(expressionHandler); |
| 143 | + pc.registerBeanComponent(new BeanComponentDefinition(expressionHandler, expressionHandlerRef)); |
| 144 | + return expressionHandlerRef; |
| 145 | + } |
| 146 | + |
| 147 | + boolean isUseExpressions(Element elt) { |
| 148 | + String useExpressions = elt.getAttribute(ATT_USE_EXPRESSIONS); |
| 149 | + return !StringUtils.hasText(useExpressions) || "true".equals(useExpressions); |
| 150 | + } |
| 151 | + |
| 152 | + private static class RequestMatcherDelegatingAuthorizationManagerFactory { |
| 153 | + |
| 154 | + private static AuthorizationManager<HttpServletRequest> createRequestMatcherDelegatingAuthorizationManager( |
| 155 | + Map<RequestMatcher, AuthorizationManager<RequestAuthorizationContext>> beans) { |
| 156 | + RequestMatcherDelegatingAuthorizationManager.Builder builder = RequestMatcherDelegatingAuthorizationManager |
| 157 | + .builder(); |
| 158 | + for (Map.Entry<RequestMatcher, AuthorizationManager<RequestAuthorizationContext>> entry : beans |
| 159 | + .entrySet()) { |
| 160 | + builder.add(entry.getKey(), entry.getValue()); |
| 161 | + } |
| 162 | + return builder.add(AnyRequestMatcher.INSTANCE, AuthenticatedAuthorizationManager.authenticated()).build(); |
| 163 | + } |
| 164 | + |
| 165 | + } |
| 166 | + |
| 167 | + static class DefaultWebSecurityExpressionHandlerBeanFactory |
| 168 | + extends GrantedAuthorityDefaultsParserUtils.AbstractGrantedAuthorityDefaultsBeanFactory { |
| 169 | + |
| 170 | + private DefaultHttpSecurityExpressionHandler handler = new DefaultHttpSecurityExpressionHandler(); |
| 171 | + |
| 172 | + @Override |
| 173 | + public DefaultHttpSecurityExpressionHandler getBean() { |
| 174 | + this.handler.setDefaultRolePrefix(this.rolePrefix); |
| 175 | + return this.handler; |
| 176 | + } |
| 177 | + |
| 178 | + } |
| 179 | + |
| 180 | +} |
0 commit comments