Skip to content

Commit 704d567

Browse files
committed
Improve logging for Global Authentication
Closes gh-14663
1 parent 85c3d0a commit 704d567

File tree

2 files changed

+125
-4
lines changed

2 files changed

+125
-4
lines changed

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

+62-2
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,15 @@
1616

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

19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
1925
import org.springframework.context.ApplicationContext;
2026
import org.springframework.core.annotation.Order;
27+
import org.springframework.core.log.LogMessage;
2128
import org.springframework.security.authentication.AuthenticationProvider;
2229
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
2330

@@ -49,16 +56,33 @@ public void init(AuthenticationManagerBuilder auth) throws Exception {
4956

5057
class InitializeAuthenticationProviderManagerConfigurer extends GlobalAuthenticationConfigurerAdapter {
5158

59+
private final Log logger = LogFactory.getLog(getClass());
60+
5261
@Override
5362
public void configure(AuthenticationManagerBuilder auth) {
5463
if (auth.isConfigured()) {
5564
return;
5665
}
57-
AuthenticationProvider authenticationProvider = getBeanOrNull(AuthenticationProvider.class);
58-
if (authenticationProvider == null) {
66+
List<BeanWithName<AuthenticationProvider>> authenticationProviders = getBeansWithName(
67+
AuthenticationProvider.class);
68+
if (authenticationProviders.isEmpty()) {
5969
return;
6070
}
71+
else if (authenticationProviders.size() > 1) {
72+
List<String> beanNames = authenticationProviders.stream().map(BeanWithName::getName).toList();
73+
this.logger.warn(LogMessage.format(
74+
"Found %s AuthenticationProvider beans, with names %s. "
75+
+ "Global Authentication Manager will not be configured with AuthenticationProviders.",
76+
authenticationProviders.size(), beanNames));
77+
return;
78+
}
79+
var authenticationProvider = authenticationProviders.get(0).getBean();
80+
var authenticationProviderBeanName = authenticationProviders.get(0).getName();
81+
6182
auth.authenticationProvider(authenticationProvider);
83+
this.logger.info(LogMessage.format(
84+
"Global AuthenticationManager configured with AuthenticationProvider bean with name %s",
85+
authenticationProviderBeanName));
6286
}
6387

6488
/**
@@ -74,6 +98,42 @@ private <T> T getBeanOrNull(Class<T> type) {
7498
return InitializeAuthenticationProviderBeanManagerConfigurer.this.context.getBean(beanNames[0], type);
7599
}
76100

101+
/**
102+
* @return a list of beans of the requested class, along with their names. If
103+
* there are no registered beans of that type, the list is empty.
104+
*/
105+
private <T> List<BeanWithName<T>> getBeansWithName(Class<T> type) {
106+
List<BeanWithName<T>> beanWithNames = new ArrayList<>();
107+
String[] beanNames = InitializeAuthenticationProviderBeanManagerConfigurer.this.context
108+
.getBeanNamesForType(type);
109+
for (String beanName : beanNames) {
110+
T bean = InitializeAuthenticationProviderBeanManagerConfigurer.this.context.getBean(beanNames[0], type);
111+
beanWithNames.add(new BeanWithName<T>(bean, beanName));
112+
}
113+
return beanWithNames;
114+
}
115+
116+
static class BeanWithName<T> {
117+
118+
private final T bean;
119+
120+
private final String name;
121+
122+
BeanWithName(T bean, String name) {
123+
this.bean = bean;
124+
this.name = name;
125+
}
126+
127+
T getBean() {
128+
return this.bean;
129+
}
130+
131+
String getName() {
132+
return this.name;
133+
}
134+
135+
}
136+
77137
}
78138

79139
}

Diff for: config/src/main/java/org/springframework/security/config/annotation/authentication/configuration/InitializeUserDetailsBeanManagerConfigurer.java

+63-2
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,16 @@
1616

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

19+
import java.util.ArrayList;
20+
import java.util.List;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.apache.commons.logging.LogFactory;
24+
1925
import org.springframework.context.ApplicationContext;
2026
import org.springframework.core.Ordered;
2127
import org.springframework.core.annotation.Order;
28+
import org.springframework.core.log.LogMessage;
2229
import org.springframework.security.authentication.dao.DaoAuthenticationProvider;
2330
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
2431
import org.springframework.security.core.userdetails.UserDetailsPasswordService;
@@ -54,15 +61,31 @@ public void init(AuthenticationManagerBuilder auth) throws Exception {
5461

5562
class InitializeUserDetailsManagerConfigurer extends GlobalAuthenticationConfigurerAdapter {
5663

64+
private final Log logger = LogFactory.getLog(getClass());
65+
5766
@Override
5867
public void configure(AuthenticationManagerBuilder auth) throws Exception {
68+
List<BeanWithName<UserDetailsService>> userDetailsServices = getBeansWithName(UserDetailsService.class);
5969
if (auth.isConfigured()) {
70+
if (!userDetailsServices.isEmpty()) {
71+
this.logger.warn("Global AuthenticationManager configured with an AuthenticationProvider bean. "
72+
+ "UserDetailsService beans will not be used for username/password login.");
73+
}
74+
return;
75+
}
76+
77+
if (userDetailsServices.isEmpty()) {
6078
return;
6179
}
62-
UserDetailsService userDetailsService = getBeanOrNull(UserDetailsService.class);
63-
if (userDetailsService == null) {
80+
else if (userDetailsServices.size() > 1) {
81+
List<String> beanNames = userDetailsServices.stream().map(BeanWithName::getName).toList();
82+
this.logger.warn(LogMessage.format("Found %s UserDetailsService beans, with names %s. "
83+
+ "Global Authentication Manager will not use a UserDetailsService for username/password login.",
84+
userDetailsServices.size(), beanNames));
6485
return;
6586
}
87+
var userDetailsService = userDetailsServices.get(0).getBean();
88+
var userDetailsServiceBeanName = userDetailsServices.get(0).getName();
6689
PasswordEncoder passwordEncoder = getBeanOrNull(PasswordEncoder.class);
6790
UserDetailsPasswordService passwordManager = getBeanOrNull(UserDetailsPasswordService.class);
6891
DaoAuthenticationProvider provider = new DaoAuthenticationProvider();
@@ -75,6 +98,9 @@ public void configure(AuthenticationManagerBuilder auth) throws Exception {
7598
}
7699
provider.afterPropertiesSet();
77100
auth.authenticationProvider(provider);
101+
this.logger.info(LogMessage.format(
102+
"Global AuthenticationManager configured with UserDetailsService bean with name %s",
103+
userDetailsServiceBeanName));
78104
}
79105

80106
/**
@@ -89,6 +115,41 @@ private <T> T getBeanOrNull(Class<T> type) {
89115
return InitializeUserDetailsBeanManagerConfigurer.this.context.getBean(beanNames[0], type);
90116
}
91117

118+
/**
119+
* @return a list of beans of the requested class, along with their names. If
120+
* there are no registered beans of that type, the list is empty.
121+
*/
122+
private <T> List<BeanWithName<T>> getBeansWithName(Class<T> type) {
123+
List<BeanWithName<T>> beanWithNames = new ArrayList<>();
124+
String[] beanNames = InitializeUserDetailsBeanManagerConfigurer.this.context.getBeanNamesForType(type);
125+
for (String beanName : beanNames) {
126+
T bean = InitializeUserDetailsBeanManagerConfigurer.this.context.getBean(beanNames[0], type);
127+
beanWithNames.add(new BeanWithName<T>(bean, beanName));
128+
}
129+
return beanWithNames;
130+
}
131+
132+
static class BeanWithName<T> {
133+
134+
private final T bean;
135+
136+
private final String name;
137+
138+
BeanWithName(T bean, String name) {
139+
this.bean = bean;
140+
this.name = name;
141+
}
142+
143+
T getBean() {
144+
return this.bean;
145+
}
146+
147+
String getName() {
148+
return this.name;
149+
}
150+
151+
}
152+
92153
}
93154

94155
}

0 commit comments

Comments
 (0)