Delay initialization AuthenticationProvider in Global Authentication

This commit is contained in:
DingHao 2024-11-08 16:40:20 +08:00 committed by Josh Cummings
parent 4b41f8cb5b
commit e8ba039a61
1 changed files with 10 additions and 49 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2024 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.
@ -16,8 +16,7 @@
package org.springframework.security.config.annotation.authentication.configuration; package org.springframework.security.config.annotation.authentication.configuration;
import java.util.ArrayList; import java.util.Arrays;
import java.util.List;
import org.apache.commons.logging.Log; import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; import org.apache.commons.logging.LogFactory;
@ -63,62 +62,24 @@ class InitializeAuthenticationProviderBeanManagerConfigurer extends GlobalAuthen
if (auth.isConfigured()) { if (auth.isConfigured()) {
return; return;
} }
List<BeanWithName<AuthenticationProvider>> authenticationProviders = getBeansWithName( String[] beanNames = InitializeAuthenticationProviderBeanManagerConfigurer.this.context
AuthenticationProvider.class); .getBeanNamesForType(AuthenticationProvider.class);
if (authenticationProviders.isEmpty()) { if (beanNames.length == 0) {
return; return;
} }
else if (authenticationProviders.size() > 1) { else if (beanNames.length > 1) {
List<String> beanNames = authenticationProviders.stream().map(BeanWithName::getName).toList();
this.logger.info(LogMessage.format("Found %s AuthenticationProvider beans, with names %s. " this.logger.info(LogMessage.format("Found %s AuthenticationProvider beans, with names %s. "
+ "Global Authentication Manager will not be configured with AuthenticationProviders. " + "Global Authentication Manager will not be configured with AuthenticationProviders. "
+ "Consider publishing a single AuthenticationProvider bean, or wiring your Providers directly " + "Consider publishing a single AuthenticationProvider bean, or wiring your Providers directly "
+ "using the DSL.", authenticationProviders.size(), beanNames)); + "using the DSL.", beanNames.length, Arrays.toString(beanNames)));
return; return;
} }
AuthenticationProvider authenticationProvider = authenticationProviders.get(0).getBean(); AuthenticationProvider authenticationProvider = InitializeAuthenticationProviderBeanManagerConfigurer.this.context
String authenticationProviderBeanName = authenticationProviders.get(0).getName(); .getBean(beanNames[0], AuthenticationProvider.class);
auth.authenticationProvider(authenticationProvider); auth.authenticationProvider(authenticationProvider);
this.logger.info(LogMessage.format( this.logger.info(LogMessage.format(
"Global AuthenticationManager configured with AuthenticationProvider bean with name %s", "Global AuthenticationManager configured with AuthenticationProvider bean with name %s",
authenticationProviderBeanName)); beanNames[0]));
}
/**
* @return a list of beans of the requested class, along with their names. If
* there are no registered beans of that type, the list is empty.
*/
private <T> List<BeanWithName<T>> getBeansWithName(Class<T> type) {
List<BeanWithName<T>> beanWithNames = new ArrayList<>();
String[] beanNames = InitializeAuthenticationProviderBeanManagerConfigurer.this.context
.getBeanNamesForType(type);
for (String beanName : beanNames) {
T bean = InitializeAuthenticationProviderBeanManagerConfigurer.this.context.getBean(beanName, type);
beanWithNames.add(new BeanWithName<T>(bean, beanName));
}
return beanWithNames;
}
static class BeanWithName<T> {
private final T bean;
private final String name;
BeanWithName(T bean, String name) {
this.bean = bean;
this.name = name;
}
T getBean() {
return this.bean;
}
String getName() {
return this.name;
}
} }
} }