Skip to content

Commit 5302fb7

Browse files
yukihanejzheaux
authored andcommitted
ProviderManager Uses CollectionUtils#contains
Closes gh-8689
1 parent 27e1c58 commit 5302fb7

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

core/src/main/java/org/springframework/security/authentication/ProviderManager.java

+2-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import org.springframework.security.core.CredentialsContainer;
3131
import org.springframework.security.core.SpringSecurityMessageSource;
3232
import org.springframework.util.Assert;
33+
import org.springframework.util.CollectionUtils;
3334

3435
/**
3536
* Iterates an {@link Authentication} request through a list of
@@ -145,7 +146,7 @@ private void checkState() {
145146
throw new IllegalArgumentException(
146147
"A parent AuthenticationManager or a list "
147148
+ "of AuthenticationProviders is required");
148-
} else if (providers.contains(null)) {
149+
} else if (CollectionUtils.contains(providers.iterator(), null)) {
149150
throw new IllegalArgumentException(
150151
"providers list cannot contain null values");
151152
}

core/src/test/java/org/springframework/security/authentication/ProviderManagerTests.java

+25
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,31 @@ public void testStartupFailsIfProvidersNotSetAsVarargs() {
102102
new ProviderManager((AuthenticationProvider) null);
103103
}
104104

105+
@Test(expected = IllegalArgumentException.class)
106+
public void testStartupFailsIfProvidersContainNullElement() {
107+
new ProviderManager(Arrays.asList(mock(AuthenticationProvider.class), null));
108+
}
109+
110+
@Test
111+
public void testUsingNullNotPermittedList() {
112+
// imitated Java9 List.of(e) object, which disallows null elements and
113+
// throws NPE when contains(null) called
114+
List<AuthenticationProvider> providers = new ArrayList<AuthenticationProvider>() {
115+
private static final long serialVersionUID = 1L;
116+
117+
@Override
118+
public boolean contains(Object o) {
119+
if (o == null) {
120+
throw new NullPointerException();
121+
}
122+
return super.contains(o);
123+
}
124+
};
125+
126+
providers.add(mock(AuthenticationProvider.class));
127+
new ProviderManager(providers);
128+
}
129+
105130
@Test
106131
public void detailsAreNotSetOnAuthenticationTokenIfAlreadySetByProvider() {
107132
Object requestDetails = "(Request Details)";

0 commit comments

Comments
 (0)