mirror of
https://github.com/spring-projects/spring-security.git
synced 2025-07-09 11:53:30 +00:00
AuthenticationManagerBeanDefinitionParserTests uses SpringTestContext
Issue: gh-4870
This commit is contained in:
parent
4d8f11a5a9
commit
5cf2883afc
@ -20,14 +20,18 @@ import static org.assertj.core.api.Assertions.*;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.context.ApplicationListener;
|
import org.springframework.context.ApplicationListener;
|
||||||
|
import org.springframework.context.ConfigurableApplicationContext;
|
||||||
import org.springframework.context.support.AbstractXmlApplicationContext;
|
import org.springframework.context.support.AbstractXmlApplicationContext;
|
||||||
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;
|
||||||
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
|
||||||
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
|
import org.springframework.security.authentication.event.AbstractAuthenticationEvent;
|
||||||
|
import org.springframework.security.config.test.SpringTestContext;
|
||||||
|
import org.springframework.security.config.test.SpringTestRule;
|
||||||
import org.springframework.security.config.util.InMemoryXmlApplicationContext;
|
import org.springframework.security.config.util.InMemoryXmlApplicationContext;
|
||||||
import org.springframework.security.util.FieldUtils;
|
import org.springframework.security.util.FieldUtils;
|
||||||
|
|
||||||
@ -42,18 +46,21 @@ public class AuthenticationManagerBeanDefinitionParserTests {
|
|||||||
+ " <user name='bob' password='{noop}bobspassword' authorities='ROLE_A,ROLE_B' />"
|
+ " <user name='bob' password='{noop}bobspassword' authorities='ROLE_A,ROLE_B' />"
|
||||||
+ " </user-service>" + " </authentication-provider>"
|
+ " </user-service>" + " </authentication-provider>"
|
||||||
+ "</authentication-manager>";
|
+ "</authentication-manager>";
|
||||||
private AbstractXmlApplicationContext appContext;
|
@Rule
|
||||||
|
public final SpringTestRule spring = new SpringTestRule();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
// SEC-1225
|
// SEC-1225
|
||||||
public void providersAreRegisteredAsTopLevelBeans() throws Exception {
|
public void providersAreRegisteredAsTopLevelBeans() throws Exception {
|
||||||
setContext(CONTEXT);
|
ConfigurableApplicationContext context = this.spring.context(CONTEXT)
|
||||||
assertThat(appContext.getBeansOfType(AuthenticationProvider.class)).hasSize(1);
|
.getContext();
|
||||||
|
assertThat(context.getBeansOfType(AuthenticationProvider.class)).hasSize(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void eventsArePublishedByDefault() throws Exception {
|
public void eventsArePublishedByDefault() throws Exception {
|
||||||
setContext(CONTEXT);
|
ConfigurableApplicationContext appContext = this.spring.context(CONTEXT)
|
||||||
|
.getContext();
|
||||||
AuthListener listener = new AuthListener();
|
AuthListener listener = new AuthListener();
|
||||||
appContext.addApplicationListener(listener);
|
appContext.addApplicationListener(listener);
|
||||||
|
|
||||||
@ -69,7 +76,8 @@ public class AuthenticationManagerBeanDefinitionParserTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void credentialsAreClearedByDefault() throws Exception {
|
public void credentialsAreClearedByDefault() throws Exception {
|
||||||
setContext(CONTEXT);
|
ConfigurableApplicationContext appContext = this.spring.context(CONTEXT)
|
||||||
|
.getContext();
|
||||||
ProviderManager pm = (ProviderManager) appContext
|
ProviderManager pm = (ProviderManager) appContext
|
||||||
.getBeansOfType(ProviderManager.class).values().toArray()[0];
|
.getBeansOfType(ProviderManager.class).values().toArray()[0];
|
||||||
assertThat(pm.isEraseCredentialsAfterAuthentication()).isTrue();
|
assertThat(pm.isEraseCredentialsAfterAuthentication()).isTrue();
|
||||||
@ -77,22 +85,19 @@ public class AuthenticationManagerBeanDefinitionParserTests {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void clearCredentialsPropertyIsRespected() throws Exception {
|
public void clearCredentialsPropertyIsRespected() throws Exception {
|
||||||
setContext("<authentication-manager erase-credentials='false'/>");
|
ConfigurableApplicationContext appContext = this.spring.context("<authentication-manager erase-credentials='false'/>")
|
||||||
|
.getContext();
|
||||||
ProviderManager pm = (ProviderManager) appContext
|
ProviderManager pm = (ProviderManager) appContext
|
||||||
.getBeansOfType(ProviderManager.class).values().toArray()[0];
|
.getBeansOfType(ProviderManager.class).values().toArray()[0];
|
||||||
assertThat(pm.isEraseCredentialsAfterAuthentication()).isFalse();
|
assertThat(pm.isEraseCredentialsAfterAuthentication()).isFalse();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setContext(String context) {
|
|
||||||
appContext = new InMemoryXmlApplicationContext(context);
|
|
||||||
}
|
|
||||||
|
|
||||||
private static class AuthListener implements
|
private static class AuthListener implements
|
||||||
ApplicationListener<AbstractAuthenticationEvent> {
|
ApplicationListener<AbstractAuthenticationEvent> {
|
||||||
List<AbstractAuthenticationEvent> events = new ArrayList<AbstractAuthenticationEvent>();
|
List<AbstractAuthenticationEvent> events = new ArrayList<AbstractAuthenticationEvent>();
|
||||||
|
|
||||||
public void onApplicationEvent(AbstractAuthenticationEvent event) {
|
public void onApplicationEvent(AbstractAuthenticationEvent event) {
|
||||||
events.add(event);
|
this.events.add(event);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user