Skip to content

Commit 5b1221a

Browse files
djecheloneleftherias
authored andcommitted
Improve AuthenticationManagerBeanDefinitionParser XML parsing
Closes gh-7282
1 parent cee42ec commit 5b1221a

File tree

2 files changed

+37
-8
lines changed

2 files changed

+37
-8
lines changed

config/src/main/java/org/springframework/security/config/authentication/AuthenticationManagerBeanDefinitionParser.java

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -52,6 +52,8 @@ public class AuthenticationManagerBeanDefinitionParser implements BeanDefinition
5252
private static final String ATT_REF = "ref";
5353
private static final String ATT_ERASE_CREDENTIALS = "erase-credentials";
5454

55+
private static final String AUTHENTICATION_EVENT_PUBLISHER_BEAN_NAME = "defaultAuthenticationEventPublisher";
56+
5557
public BeanDefinition parse(Element element, ParserContext pc) {
5658
String id = element.getAttribute("id");
5759

@@ -124,12 +126,14 @@ public BeanDefinition parse(Element element, ParserContext pc) {
124126
false);
125127
}
126128

127-
// Add the default event publisher
128-
BeanDefinition publisher = new RootBeanDefinition(
129-
DefaultAuthenticationEventPublisher.class);
130-
String pubId = pc.getReaderContext().generateBeanName(publisher);
131-
pc.registerBeanComponent(new BeanComponentDefinition(publisher, pubId));
132-
providerManagerBldr.addPropertyReference("authenticationEventPublisher", pubId);
129+
if (!pc.getRegistry().containsBeanDefinition(AUTHENTICATION_EVENT_PUBLISHER_BEAN_NAME)) {
130+
// Add the default event publisher to the context
131+
BeanDefinition publisher = new RootBeanDefinition(DefaultAuthenticationEventPublisher.class);
132+
pc.registerBeanComponent(new BeanComponentDefinition(publisher, AUTHENTICATION_EVENT_PUBLISHER_BEAN_NAME));
133+
}
134+
135+
providerManagerBldr.addPropertyReference("authenticationEventPublisher",
136+
AUTHENTICATION_EVENT_PUBLISHER_BEAN_NAME);
133137

134138
pc.registerBeanComponent(new BeanComponentDefinition(providerManagerBldr
135139
.getBeanDefinition(), id));

config/src/test/java/org/springframework/security/config/authentication/AuthenticationManagerBeanDefinitionParserTests.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2016 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -20,6 +20,7 @@
2020
import org.springframework.beans.factory.annotation.Autowired;
2121
import org.springframework.context.ApplicationListener;
2222
import org.springframework.context.ConfigurableApplicationContext;
23+
import org.springframework.security.authentication.AuthenticationEventPublisher;
2324
import org.springframework.security.authentication.AuthenticationProvider;
2425
import org.springframework.security.authentication.DefaultAuthenticationEventPublisher;
2526
import org.springframework.security.authentication.ProviderManager;
@@ -49,6 +50,18 @@ public class AuthenticationManagerBeanDefinitionParserTests {
4950
+ " </user-service>"
5051
+ " </authentication-provider>"
5152
+ "</authentication-manager>";
53+
54+
// Issue #7282
55+
// @formatter:off
56+
private static final String CONTEXT_MULTI = "<authentication-manager id='amSecondary'>"
57+
+ " <authentication-provider>"
58+
+ " <user-service>"
59+
+ " <user name='john' password='{noop}doe' authorities='ROLE_C,ROLE_D' />"
60+
+ " </user-service>"
61+
+ " </authentication-provider>"
62+
+ "</authentication-manager>";
63+
// @formatter:on
64+
5265
@Rule
5366
public final SpringTestRule spring = new SpringTestRule();
5467

@@ -60,6 +73,18 @@ public void providersAreRegisteredAsTopLevelBeans() {
6073
assertThat(context.getBeansOfType(AuthenticationProvider.class)).hasSize(1);
6174
}
6275

76+
@Test
77+
public void eventPublishersAreRegisteredAsTopLevelBeans() {
78+
ConfigurableApplicationContext context = this.spring.context(CONTEXT).getContext();
79+
assertThat(context.getBeansOfType(AuthenticationEventPublisher.class)).hasSize(1);
80+
}
81+
82+
@Test
83+
public void onlyOneEventPublisherIsRegisteredForMultipleAuthenticationManagers() {
84+
ConfigurableApplicationContext context = this.spring.context(CONTEXT + '\n' + CONTEXT_MULTI).getContext();
85+
assertThat(context.getBeansOfType(AuthenticationEventPublisher.class)).hasSize(1);
86+
}
87+
6388
@Test
6489
public void eventsArePublishedByDefault() throws Exception {
6590
ConfigurableApplicationContext appContext = this.spring.context(CONTEXT)

0 commit comments

Comments
 (0)