|
1 | 1 | /*
|
2 |
| - * Copyright 2002-2019 the original author or authors. |
| 2 | + * Copyright 2002-2024 the original author or authors. |
3 | 3 | *
|
4 | 4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
5 | 5 | * you may not use this file except in compliance with the License.
|
|
16 | 16 |
|
17 | 17 | package org.springframework.security.config.annotation.authentication.configuration;
|
18 | 18 |
|
19 |
| -import java.util.ArrayList; |
20 |
| -import java.util.List; |
| 19 | +import java.util.Arrays; |
21 | 20 |
|
22 | 21 | import org.apache.commons.logging.Log;
|
23 | 22 | import org.apache.commons.logging.LogFactory;
|
@@ -63,62 +62,24 @@ public void configure(AuthenticationManagerBuilder auth) {
|
63 | 62 | if (auth.isConfigured()) {
|
64 | 63 | return;
|
65 | 64 | }
|
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) { |
69 | 68 | return;
|
70 | 69 | }
|
71 |
| - else if (authenticationProviders.size() > 1) { |
72 |
| - List<String> beanNames = authenticationProviders.stream().map(BeanWithName::getName).toList(); |
| 70 | + else if (beanNames.length > 1) { |
73 | 71 | this.logger.info(LogMessage.format("Found %s AuthenticationProvider beans, with names %s. "
|
74 | 72 | + "Global Authentication Manager will not be configured with AuthenticationProviders. "
|
75 | 73 | + "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))); |
77 | 75 | return;
|
78 | 76 | }
|
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); |
82 | 79 | auth.authenticationProvider(authenticationProvider);
|
83 | 80 | this.logger.info(LogMessage.format(
|
84 | 81 | "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])); |
122 | 83 | }
|
123 | 84 |
|
124 | 85 | }
|
|
0 commit comments