Skip to content

Commit 4eec4a6

Browse files
committed
Delay initialization AuthenticationProvider in Global Authentication
1 parent b9d5493 commit 4eec4a6

File tree

1 file changed

+10
-49
lines changed

1 file changed

+10
-49
lines changed

config/src/main/java/org/springframework/security/config/annotation/authentication/configuration/InitializeAuthenticationProviderBeanManagerConfigurer.java

Lines changed: 10 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2024 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.
@@ -16,8 +16,7 @@
1616

1717
package org.springframework.security.config.annotation.authentication.configuration;
1818

19-
import java.util.ArrayList;
20-
import java.util.List;
19+
import java.util.Arrays;
2120

2221
import org.apache.commons.logging.Log;
2322
import org.apache.commons.logging.LogFactory;
@@ -63,62 +62,24 @@ public void configure(AuthenticationManagerBuilder auth) {
6362
if (auth.isConfigured()) {
6463
return;
6564
}
66-
List<BeanWithName<AuthenticationProvider>> authenticationProviders = getBeansWithName(
67-
AuthenticationProvider.class);
68-
if (authenticationProviders.isEmpty()) {
65+
String[] beanNames = InitializeAuthenticationProviderBeanManagerConfigurer.this.context
66+
.getBeanNamesForType(AuthenticationProvider.class);
67+
if (beanNames.length == 0) {
6968
return;
7069
}
71-
else if (authenticationProviders.size() > 1) {
72-
List<String> beanNames = authenticationProviders.stream().map(BeanWithName::getName).toList();
70+
else if (beanNames.length > 1) {
7371
this.logger.info(LogMessage.format("Found %s AuthenticationProvider beans, with names %s. "
7472
+ "Global Authentication Manager will not be configured with AuthenticationProviders. "
7573
+ "Consider publishing a single AuthenticationProvider bean, or wiring your Providers directly "
76-
+ "using the DSL.", authenticationProviders.size(), beanNames));
74+
+ "using the DSL.", beanNames.length, Arrays.toString(beanNames)));
7775
return;
7876
}
79-
AuthenticationProvider authenticationProvider = authenticationProviders.get(0).getBean();
80-
String authenticationProviderBeanName = authenticationProviders.get(0).getName();
81-
77+
AuthenticationProvider authenticationProvider = InitializeAuthenticationProviderBeanManagerConfigurer.this.context
78+
.getBean(beanNames[0], AuthenticationProvider.class);
8279
auth.authenticationProvider(authenticationProvider);
8380
this.logger.info(LogMessage.format(
8481
"Global AuthenticationManager configured with AuthenticationProvider bean with name %s",
85-
authenticationProviderBeanName));
86-
}
87-
88-
/**
89-
* @return a list of beans of the requested class, along with their names. If
90-
* there are no registered beans of that type, the list is empty.
91-
*/
92-
private <T> List<BeanWithName<T>> getBeansWithName(Class<T> type) {
93-
List<BeanWithName<T>> beanWithNames = new ArrayList<>();
94-
String[] beanNames = InitializeAuthenticationProviderBeanManagerConfigurer.this.context
95-
.getBeanNamesForType(type);
96-
for (String beanName : beanNames) {
97-
T bean = InitializeAuthenticationProviderBeanManagerConfigurer.this.context.getBean(beanName, type);
98-
beanWithNames.add(new BeanWithName<T>(bean, beanName));
99-
}
100-
return beanWithNames;
101-
}
102-
103-
static class BeanWithName<T> {
104-
105-
private final T bean;
106-
107-
private final String name;
108-
109-
BeanWithName(T bean, String name) {
110-
this.bean = bean;
111-
this.name = name;
112-
}
113-
114-
T getBean() {
115-
return this.bean;
116-
}
117-
118-
String getName() {
119-
return this.name;
120-
}
121-
82+
beanNames[0]));
12283
}
12384

12485
}

0 commit comments

Comments
 (0)