Skip to content

Commit 0aaaaf9

Browse files
ankurpathakrwinch
authored andcommitted
Improve CsrfBeanDefinitionParser xml parsing
1. CsrfBeanDefinitionParser registers requestDataValueProcessor if not already registered 2. Created Tests in CsrfBeanDefinitionParserTests Fixes: gh-6423
1 parent be7d2a3 commit 0aaaaf9

File tree

3 files changed

+80
-5
lines changed

3 files changed

+80
-5
lines changed

config/src/main/java/org/springframework/security/config/http/CsrfBeanDefinitionParser.java

+8-5
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@
4545
* Parser for the {@code CsrfFilter}.
4646
*
4747
* @author Rob Winch
48+
* @author Ankur Pathak
4849
* @since 3.2
4950
*/
5051
public class CsrfBeanDefinitionParser implements BeanDefinitionParser {
@@ -67,11 +68,13 @@ public BeanDefinition parse(Element element, ParserContext pc) {
6768
boolean webmvcPresent = ClassUtils.isPresent(DISPATCHER_SERVLET_CLASS_NAME,
6869
getClass().getClassLoader());
6970
if (webmvcPresent) {
70-
RootBeanDefinition beanDefinition = new RootBeanDefinition(
71-
CsrfRequestDataValueProcessor.class);
72-
BeanComponentDefinition componentDefinition = new BeanComponentDefinition(
73-
beanDefinition, REQUEST_DATA_VALUE_PROCESSOR);
74-
pc.registerBeanComponent(componentDefinition);
71+
if (!pc.getRegistry().containsBeanDefinition(REQUEST_DATA_VALUE_PROCESSOR)) {
72+
RootBeanDefinition beanDefinition = new RootBeanDefinition(
73+
CsrfRequestDataValueProcessor.class);
74+
BeanComponentDefinition componentDefinition = new BeanComponentDefinition(
75+
beanDefinition, REQUEST_DATA_VALUE_PROCESSOR);
76+
pc.registerBeanComponent(componentDefinition);
77+
}
7578
}
7679

7780
String matcherRef = null;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
* Copyright 2002-2018 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+
* http://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+
package org.springframework.security.config.http;
17+
18+
import org.junit.Test;
19+
20+
import org.springframework.context.support.ClassPathXmlApplicationContext;
21+
22+
/**
23+
* @author Ankur Pathak
24+
*/
25+
public class CsrfBeanDefinitionParserTests {
26+
private static final String CONFIG_LOCATION_PREFIX =
27+
"classpath:org/springframework/security/config/http/CsrfBeanDefinitionParserTests";
28+
29+
@Test
30+
public void registerDataValueProcessorOnlyIfNotRegistered() throws Exception {
31+
try (ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext()) {
32+
context.setAllowBeanDefinitionOverriding(false);
33+
context.setConfigLocation(this.xml("RegisterDataValueProcessorOnyIfNotRegistered"));
34+
context.refresh();
35+
}
36+
}
37+
38+
private String xml(String configName) {
39+
return CONFIG_LOCATION_PREFIX + "-" + configName + ".xml";
40+
}
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xmlns:context="http://www.springframework.org/schema/context"
3+
xmlns:tx="http://www.springframework.org/schema/tx" xmlns:util="http://www.springframework.org/schema/util"
4+
xmlns:security="http://www.springframework.org/schema/security"
5+
xsi:schemaLocation="
6+
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
7+
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
8+
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd
9+
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.1.xsd
10+
http://www.springframework.org/schema/security http://www.springframework.org/schema/security/spring-security.xsd
11+
">
12+
13+
<security:http pattern="/foo/**">
14+
<security:intercept-url pattern="/**" access="hasRole('FOO')" />
15+
<security:http-basic/>
16+
</security:http>
17+
18+
<security:http pattern="/bar/**">
19+
<security:intercept-url pattern="/**" access="hasRole('BAR')" />
20+
<security:http-basic/>
21+
</security:http>
22+
23+
<security:authentication-manager >
24+
<security:authentication-provider>
25+
<security:user-service>
26+
<security:user name="gnu" password="{noop}gnat" authorities="ROLE_FOO" />
27+
</security:user-service>
28+
</security:authentication-provider>
29+
</security:authentication-manager>
30+
31+
</beans>

0 commit comments

Comments
 (0)