mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-25 11:43:29 +00:00
SEC-1725: Add option to filter-chain to use an explicit request-matcher-ref instead of a "path" attribute.
This commit is contained in:
parent
f883c6e579
commit
b5924db74d
@ -5,7 +5,10 @@ import org.springframework.beans.factory.config.RuntimeBeanReference;
|
|||||||
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
|
||||||
import org.springframework.beans.factory.support.ManagedList;
|
import org.springframework.beans.factory.support.ManagedList;
|
||||||
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
import org.springframework.beans.factory.xml.AbstractSingleBeanDefinitionParser;
|
||||||
|
import org.springframework.beans.factory.xml.BeanDefinitionParser;
|
||||||
|
import org.springframework.beans.factory.xml.ParserContext;
|
||||||
import org.springframework.security.web.SecurityFilterChain;
|
import org.springframework.security.web.SecurityFilterChain;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
import org.springframework.util.StringUtils;
|
import org.springframework.util.StringUtils;
|
||||||
import org.w3c.dom.Element;
|
import org.w3c.dom.Element;
|
||||||
|
|
||||||
@ -14,20 +17,24 @@ import java.util.*;
|
|||||||
/**
|
/**
|
||||||
* @author Luke Taylor
|
* @author Luke Taylor
|
||||||
*/
|
*/
|
||||||
public class FilterChainBeanDefinitionParser extends AbstractSingleBeanDefinitionParser {
|
public class FilterChainBeanDefinitionParser implements BeanDefinitionParser {
|
||||||
|
private static final String ATT_REQUEST_MATCHER_REF = "request-matcher-ref";
|
||||||
|
|
||||||
@Override
|
public BeanDefinition parse(Element elt, ParserContext pc) {
|
||||||
protected Class getBeanClass(Element element) {
|
|
||||||
return SecurityFilterChain.class;
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void doParse(Element elt, BeanDefinitionBuilder builder) {
|
|
||||||
MatcherType matcherType = MatcherType.fromElement(elt);
|
MatcherType matcherType = MatcherType.fromElement(elt);
|
||||||
String path = elt.getAttribute(HttpSecurityBeanDefinitionParser.ATT_PATH_PATTERN);
|
String path = elt.getAttribute(HttpSecurityBeanDefinitionParser.ATT_PATH_PATTERN);
|
||||||
|
String requestMatcher = elt.getAttribute(ATT_REQUEST_MATCHER_REF);
|
||||||
String filters = elt.getAttribute(HttpSecurityBeanDefinitionParser.ATT_FILTERS);
|
String filters = elt.getAttribute(HttpSecurityBeanDefinitionParser.ATT_FILTERS);
|
||||||
|
|
||||||
builder.addConstructorArgValue(matcherType.createMatcher(path, null));
|
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(SecurityFilterChain.class);
|
||||||
|
|
||||||
|
if (StringUtils.hasText(path)) {
|
||||||
|
Assert.isTrue(!StringUtils.hasText(requestMatcher), "");
|
||||||
|
builder.addConstructorArgValue(matcherType.createMatcher(path, null));
|
||||||
|
} else {
|
||||||
|
Assert.isTrue(StringUtils.hasText(requestMatcher), "");
|
||||||
|
builder.addConstructorArgReference(requestMatcher);
|
||||||
|
}
|
||||||
|
|
||||||
if (filters.equals(HttpSecurityBeanDefinitionParser.OPT_FILTERS_NONE)) {
|
if (filters.equals(HttpSecurityBeanDefinitionParser.OPT_FILTERS_NONE)) {
|
||||||
builder.addConstructorArgValue(Collections.EMPTY_LIST);
|
builder.addConstructorArgValue(Collections.EMPTY_LIST);
|
||||||
@ -41,5 +48,7 @@ public class FilterChainBeanDefinitionParser extends AbstractSingleBeanDefinitio
|
|||||||
|
|
||||||
builder.addConstructorArgValue(filterChain);
|
builder.addConstructorArgValue(filterChain);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return builder.getBeanDefinition();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -446,7 +446,7 @@ filter-chain =
|
|||||||
## Used within to define a specific URL pattern and the list of filters which apply to the URLs matching that pattern. When multiple filter-chain elements are assembled in a list in order to configure a FilterChainProxy, the most specific patterns must be placed at the top of the list, with most general ones at the bottom.
|
## Used within to define a specific URL pattern and the list of filters which apply to the URLs matching that pattern. When multiple filter-chain elements are assembled in a list in order to configure a FilterChainProxy, the most specific patterns must be placed at the top of the list, with most general ones at the bottom.
|
||||||
element filter-chain {filter-chain.attlist, empty}
|
element filter-chain {filter-chain.attlist, empty}
|
||||||
filter-chain.attlist &=
|
filter-chain.attlist &=
|
||||||
attribute pattern {xsd:token}
|
(attribute pattern {xsd:token} | attribute request-matcher-ref {xsd:token})
|
||||||
filter-chain.attlist &=
|
filter-chain.attlist &=
|
||||||
attribute filters {xsd:token}
|
attribute filters {xsd:token}
|
||||||
|
|
||||||
|
@ -1006,7 +1006,8 @@
|
|||||||
<xs:attributeGroup ref="security:filter-chain.attlist"/>
|
<xs:attributeGroup ref="security:filter-chain.attlist"/>
|
||||||
</xs:complexType></xs:element>
|
</xs:complexType></xs:element>
|
||||||
<xs:attributeGroup name="filter-chain.attlist">
|
<xs:attributeGroup name="filter-chain.attlist">
|
||||||
<xs:attribute name="pattern" use="required" type="xs:token"/>
|
<xs:attribute name="pattern" type="xs:token"/>
|
||||||
|
<xs:attribute name="request-matcher-ref" type="xs:token"/>
|
||||||
<xs:attribute name="filters" use="required" type="xs:token"/>
|
<xs:attribute name="filters" use="required" type="xs:token"/>
|
||||||
</xs:attributeGroup>
|
</xs:attributeGroup>
|
||||||
<xs:element name="filter-security-metadata-source"><xs:annotation>
|
<xs:element name="filter-security-metadata-source"><xs:annotation>
|
||||||
|
@ -47,10 +47,14 @@
|
|||||||
|
|
||||||
<bean id="mockNotAFilter" class="org.springframework.security.web.util.AnyRequestMatcher"/>
|
<bean id="mockNotAFilter" class="org.springframework.security.web.util.AnyRequestMatcher"/>
|
||||||
|
|
||||||
|
<bean id="fooMatcher" class="org.springframework.security.web.util.AntPathRequestMatcher">
|
||||||
|
<constructor-arg value="/foo/**"/>
|
||||||
|
</bean>
|
||||||
|
|
||||||
<bean id="filterChain" class="org.springframework.security.web.FilterChainProxy">
|
<bean id="filterChain" class="org.springframework.security.web.FilterChainProxy">
|
||||||
<constructor-arg>
|
<constructor-arg>
|
||||||
<util:list>
|
<util:list>
|
||||||
<sec:filter-chain pattern="/foo/**" filters="mockFilter"/>
|
<sec:filter-chain request-matcher-ref="fooMatcher" filters="mockFilter"/>
|
||||||
<sec:filter-chain pattern="/some/other/path/**" filters="mockFilter"/>
|
<sec:filter-chain pattern="/some/other/path/**" filters="mockFilter"/>
|
||||||
<sec:filter-chain pattern="/do/not/filter" filters="none"/>
|
<sec:filter-chain pattern="/do/not/filter" filters="none"/>
|
||||||
</util:list>
|
</util:list>
|
||||||
@ -121,11 +125,7 @@
|
|||||||
<constructor-arg>
|
<constructor-arg>
|
||||||
<list>
|
<list>
|
||||||
<bean class="org.springframework.security.web.SecurityFilterChain">
|
<bean class="org.springframework.security.web.SecurityFilterChain">
|
||||||
<constructor-arg>
|
<constructor-arg ref="fooMatcher"/>
|
||||||
<bean class="org.springframework.security.web.util.AntPathRequestMatcher">
|
|
||||||
<constructor-arg value="/foo/**"/>
|
|
||||||
</bean>
|
|
||||||
</constructor-arg>
|
|
||||||
<constructor-arg>
|
<constructor-arg>
|
||||||
<list>
|
<list>
|
||||||
<ref local="mockFilter"/>
|
<ref local="mockFilter"/>
|
||||||
|
Loading…
x
Reference in New Issue
Block a user