mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-06-28 23:02:15 +00:00
Improve AuthenticationManagerBeanDefinitionParser XML parsing
Closes gh-7282
This commit is contained in:
parent
fdd017d935
commit
61284ce22d
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2018 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -57,6 +57,8 @@ public class AuthenticationManagerBeanDefinitionParser implements BeanDefinition
|
|||||||
|
|
||||||
private static final String ATT_ERASE_CREDENTIALS = "erase-credentials";
|
private static final String ATT_ERASE_CREDENTIALS = "erase-credentials";
|
||||||
|
|
||||||
|
private static final String AUTHENTICATION_EVENT_PUBLISHER_BEAN_NAME = "defaultAuthenticationEventPublisher";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public BeanDefinition parse(Element element, ParserContext pc) {
|
public BeanDefinition parse(Element element, ParserContext pc) {
|
||||||
String id = element.getAttribute("id");
|
String id = element.getAttribute("id");
|
||||||
@ -86,11 +88,15 @@ public class AuthenticationManagerBeanDefinitionParser implements BeanDefinition
|
|||||||
if ("false".equals(element.getAttribute(ATT_ERASE_CREDENTIALS))) {
|
if ("false".equals(element.getAttribute(ATT_ERASE_CREDENTIALS))) {
|
||||||
providerManagerBldr.addPropertyValue("eraseCredentialsAfterAuthentication", false);
|
providerManagerBldr.addPropertyValue("eraseCredentialsAfterAuthentication", false);
|
||||||
}
|
}
|
||||||
// Add the default event publisher
|
|
||||||
BeanDefinition publisher = new RootBeanDefinition(DefaultAuthenticationEventPublisher.class);
|
if (!pc.getRegistry().containsBeanDefinition(AUTHENTICATION_EVENT_PUBLISHER_BEAN_NAME)) {
|
||||||
String pubId = pc.getReaderContext().generateBeanName(publisher);
|
// Add the default event publisher to the context
|
||||||
pc.registerBeanComponent(new BeanComponentDefinition(publisher, pubId));
|
BeanDefinition publisher = new RootBeanDefinition(DefaultAuthenticationEventPublisher.class);
|
||||||
providerManagerBldr.addPropertyReference("authenticationEventPublisher", pubId);
|
pc.registerBeanComponent(new BeanComponentDefinition(publisher, AUTHENTICATION_EVENT_PUBLISHER_BEAN_NAME));
|
||||||
|
}
|
||||||
|
|
||||||
|
providerManagerBldr.addPropertyReference("authenticationEventPublisher",
|
||||||
|
AUTHENTICATION_EVENT_PUBLISHER_BEAN_NAME);
|
||||||
pc.registerBeanComponent(new BeanComponentDefinition(providerManagerBldr.getBeanDefinition(), id));
|
pc.registerBeanComponent(new BeanComponentDefinition(providerManagerBldr.getBeanDefinition(), id));
|
||||||
if (StringUtils.hasText(alias)) {
|
if (StringUtils.hasText(alias)) {
|
||||||
pc.getRegistry().registerAlias(id, alias);
|
pc.getRegistry().registerAlias(id, alias);
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
/*
|
/*
|
||||||
* Copyright 2002-2016 the original author or authors.
|
* Copyright 2002-2021 the original author or authors.
|
||||||
*
|
*
|
||||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
@ -25,6 +25,7 @@ import org.junit.Test;
|
|||||||
import org.springframework.beans.factory.annotation.Autowired;
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
import org.springframework.context.ConfigurableApplicationContext;
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
|
import org.springframework.security.authentication.AuthenticationEventPublisher;
|
||||||
import org.springframework.security.authentication.AuthenticationProvider;
|
import org.springframework.security.authentication.AuthenticationProvider;
|
||||||
import org.springframework.security.authentication.DefaultAuthenticationEventPublisher;
|
import org.springframework.security.authentication.DefaultAuthenticationEventPublisher;
|
||||||
import org.springframework.security.authentication.ProviderManager;
|
import org.springframework.security.authentication.ProviderManager;
|
||||||
@ -54,6 +55,17 @@ public class AuthenticationManagerBeanDefinitionParserTests {
|
|||||||
+ "</authentication-manager>";
|
+ "</authentication-manager>";
|
||||||
// @formatter:on
|
// @formatter:on
|
||||||
|
|
||||||
|
// Issue #7282
|
||||||
|
// @formatter:off
|
||||||
|
private static final String CONTEXT_MULTI = "<authentication-manager id='amSecondary'>"
|
||||||
|
+ " <authentication-provider>"
|
||||||
|
+ " <user-service>"
|
||||||
|
+ " <user name='john' password='{noop}doe' authorities='ROLE_C,ROLE_D' />"
|
||||||
|
+ " </user-service>"
|
||||||
|
+ " </authentication-provider>"
|
||||||
|
+ "</authentication-manager>";
|
||||||
|
// @formatter:on
|
||||||
|
|
||||||
@Rule
|
@Rule
|
||||||
public final SpringTestRule spring = new SpringTestRule();
|
public final SpringTestRule spring = new SpringTestRule();
|
||||||
|
|
||||||
@ -64,6 +76,18 @@ public class AuthenticationManagerBeanDefinitionParserTests {
|
|||||||
assertThat(context.getBeansOfType(AuthenticationProvider.class)).hasSize(1);
|
assertThat(context.getBeansOfType(AuthenticationProvider.class)).hasSize(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void eventPublishersAreRegisteredAsTopLevelBeans() {
|
||||||
|
ConfigurableApplicationContext context = this.spring.context(CONTEXT).getContext();
|
||||||
|
assertThat(context.getBeansOfType(AuthenticationEventPublisher.class)).hasSize(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onlyOneEventPublisherIsRegisteredForMultipleAuthenticationManagers() {
|
||||||
|
ConfigurableApplicationContext context = this.spring.context(CONTEXT + '\n' + CONTEXT_MULTI).getContext();
|
||||||
|
assertThat(context.getBeansOfType(AuthenticationEventPublisher.class)).hasSize(1);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void eventsArePublishedByDefault() throws Exception {
|
public void eventsArePublishedByDefault() throws Exception {
|
||||||
ConfigurableApplicationContext appContext = this.spring.context(CONTEXT).getContext();
|
ConfigurableApplicationContext appContext = this.spring.context(CONTEXT).getContext();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user