Skip to content

Commit e618fc4

Browse files
ngocnhan-tran1996jzheaux
authored andcommitted
Favor ObjectProvider
Closes gh-15805
1 parent 9dda65a commit e618fc4

24 files changed

+106
-231
lines changed

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

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 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.
@@ -28,7 +28,6 @@
2828
import org.springframework.aop.framework.ProxyFactoryBean;
2929
import org.springframework.aop.target.LazyInitTargetSource;
3030
import org.springframework.beans.factory.BeanFactoryUtils;
31-
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3231
import org.springframework.beans.factory.annotation.Autowired;
3332
import org.springframework.context.ApplicationContext;
3433
import org.springframework.context.ConfigurableApplicationContext;
@@ -57,6 +56,7 @@
5756
* Exports the authentication {@link Configuration}
5857
*
5958
* @author Rob Winch
59+
* @author Ngoc Nhan
6060
* @since 3.2
6161
*
6262
*/
@@ -197,15 +197,6 @@ private AuthenticationManager getAuthenticationManagerBean() {
197197
return lazyBean(AuthenticationManager.class);
198198
}
199199

200-
private static <T> T getBeanOrNull(ApplicationContext applicationContext, Class<T> type) {
201-
try {
202-
return applicationContext.getBean(type);
203-
}
204-
catch (NoSuchBeanDefinitionException notFound) {
205-
return null;
206-
}
207-
}
208-
209200
private static class EnableGlobalAuthenticationAutowiredConfigurer extends GlobalAuthenticationConfigurerAdapter {
210201

211202
private final ApplicationContext context;
@@ -330,12 +321,9 @@ private PasswordEncoder getPasswordEncoder() {
330321
if (this.passwordEncoder != null) {
331322
return this.passwordEncoder;
332323
}
333-
PasswordEncoder passwordEncoder = getBeanOrNull(this.applicationContext, PasswordEncoder.class);
334-
if (passwordEncoder == null) {
335-
passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
336-
}
337-
this.passwordEncoder = passwordEncoder;
338-
return passwordEncoder;
324+
this.passwordEncoder = this.applicationContext.getBeanProvider(PasswordEncoder.class)
325+
.getIfUnique(PasswordEncoderFactories::createDelegatingPasswordEncoder);
326+
return this.passwordEncoder;
339327
}
340328

341329
@Override

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
* {@link PasswordEncoder} is defined will wire this up too.
4040
*
4141
* @author Rob Winch
42+
* @author Ngoc Nhan
4243
* @since 4.1
4344
*/
4445
@Order(InitializeUserDetailsBeanManagerConfigurer.DEFAULT_ORDER)
@@ -121,11 +122,7 @@ else if (userDetailsServices.size() > 1) {
121122
* component, null otherwise.
122123
*/
123124
private <T> T getBeanOrNull(Class<T> type) {
124-
String[] beanNames = InitializeUserDetailsBeanManagerConfigurer.this.context.getBeanNamesForType(type);
125-
if (beanNames.length != 1) {
126-
return null;
127-
}
128-
return InitializeUserDetailsBeanManagerConfigurer.this.context.getBean(beanNames[0], type);
125+
return InitializeUserDetailsBeanManagerConfigurer.this.context.getBeanProvider(type).getIfUnique();
129126
}
130127

131128
/**

config/src/main/java/org/springframework/security/config/annotation/method/configuration/GlobalMethodSecurityConfiguration.java

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 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.
@@ -27,7 +27,6 @@
2727
import org.springframework.beans.BeansException;
2828
import org.springframework.beans.factory.BeanFactory;
2929
import org.springframework.beans.factory.BeanFactoryAware;
30-
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3130
import org.springframework.beans.factory.SmartInitializingSingleton;
3231
import org.springframework.beans.factory.annotation.Autowired;
3332
import org.springframework.beans.factory.config.BeanDefinition;
@@ -84,6 +83,7 @@
8483
*
8584
* @author Rob Winch
8685
* @author Eddú Meléndez
86+
* @author Ngoc Nhan
8787
* @since 3.2
8888
* @see EnableGlobalMethodSecurity
8989
* @deprecated Use {@link PrePostMethodSecurityConfiguration},
@@ -168,33 +168,28 @@ public void afterSingletonsInstantiated() {
168168
catch (Exception ex) {
169169
throw new RuntimeException(ex);
170170
}
171-
PermissionEvaluator permissionEvaluator = getSingleBeanOrNull(PermissionEvaluator.class);
171+
PermissionEvaluator permissionEvaluator = getBeanOrNull(PermissionEvaluator.class);
172172
if (permissionEvaluator != null) {
173173
this.defaultMethodExpressionHandler.setPermissionEvaluator(permissionEvaluator);
174174
}
175-
RoleHierarchy roleHierarchy = getSingleBeanOrNull(RoleHierarchy.class);
175+
RoleHierarchy roleHierarchy = getBeanOrNull(RoleHierarchy.class);
176176
if (roleHierarchy != null) {
177177
this.defaultMethodExpressionHandler.setRoleHierarchy(roleHierarchy);
178178
}
179-
AuthenticationTrustResolver trustResolver = getSingleBeanOrNull(AuthenticationTrustResolver.class);
179+
AuthenticationTrustResolver trustResolver = getBeanOrNull(AuthenticationTrustResolver.class);
180180
if (trustResolver != null) {
181181
this.defaultMethodExpressionHandler.setTrustResolver(trustResolver);
182182
}
183-
GrantedAuthorityDefaults grantedAuthorityDefaults = getSingleBeanOrNull(GrantedAuthorityDefaults.class);
183+
GrantedAuthorityDefaults grantedAuthorityDefaults = getBeanOrNull(GrantedAuthorityDefaults.class);
184184
if (grantedAuthorityDefaults != null) {
185185
this.defaultMethodExpressionHandler.setDefaultRolePrefix(grantedAuthorityDefaults.getRolePrefix());
186186
}
187187

188188
this.defaultMethodExpressionHandler = this.objectPostProcessor.postProcess(this.defaultMethodExpressionHandler);
189189
}
190190

191-
private <T> T getSingleBeanOrNull(Class<T> type) {
192-
try {
193-
return this.context.getBean(type);
194-
}
195-
catch (NoSuchBeanDefinitionException ex) {
196-
}
197-
return null;
191+
private <T> T getBeanOrNull(Class<T> type) {
192+
return this.context.getBeanProvider(type).getIfUnique();
198193
}
199194

200195
private void initializeMethodSecurityInterceptor() throws Exception {
@@ -262,7 +257,7 @@ protected AccessDecisionManager accessDecisionManager() {
262257
decisionVoters.add(new Jsr250Voter());
263258
}
264259
RoleVoter roleVoter = new RoleVoter();
265-
GrantedAuthorityDefaults grantedAuthorityDefaults = getSingleBeanOrNull(GrantedAuthorityDefaults.class);
260+
GrantedAuthorityDefaults grantedAuthorityDefaults = getBeanOrNull(GrantedAuthorityDefaults.class);
266261
if (grantedAuthorityDefaults != null) {
267262
roleVoter.setRolePrefix(grantedAuthorityDefaults.getRolePrefix());
268263
}
@@ -373,7 +368,7 @@ public MethodSecurityMetadataSource methodSecurityMetadataSource() {
373368
sources.add(new SecuredAnnotationSecurityMetadataSource());
374369
}
375370
if (isJsr250Enabled) {
376-
GrantedAuthorityDefaults grantedAuthorityDefaults = getSingleBeanOrNull(GrantedAuthorityDefaults.class);
371+
GrantedAuthorityDefaults grantedAuthorityDefaults = getBeanOrNull(GrantedAuthorityDefaults.class);
377372
Jsr250MethodSecurityMetadataSource jsr250MethodSecurityMetadataSource = this.context
378373
.getBean(Jsr250MethodSecurityMetadataSource.class);
379374
if (grantedAuthorityDefaults != null) {

config/src/main/java/org/springframework/security/config/annotation/rsocket/RSocketSecurity.java

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2019-2021 the original author or authors.
2+
* Copyright 2019-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.
@@ -108,6 +108,7 @@
108108
* @author Luis Felipe Vega
109109
* @author Manuel Tejeda
110110
* @author Ebert Toribio
111+
* @author Ngoc Nhan
111112
* @since 5.2
112113
*/
113114
public class RSocketSecurity {
@@ -238,15 +239,12 @@ private <T> T getBeanOrNull(Class<T> beanClass) {
238239
return getBeanOrNull(ResolvableType.forClass(beanClass));
239240
}
240241

242+
@SuppressWarnings("unchecked")
241243
private <T> T getBeanOrNull(ResolvableType type) {
242244
if (this.context == null) {
243245
return null;
244246
}
245-
String[] names = this.context.getBeanNamesForType(type);
246-
if (names.length == 1) {
247-
return (T) this.context.getBean(names[0]);
248-
}
249-
return null;
247+
return (T) this.context.getBeanProvider(type).getIfUnique();
250248
}
251249

252250
protected void setApplicationContext(ApplicationContext applicationContext) throws BeansException {

config/src/main/java/org/springframework/security/config/annotation/web/builders/HttpSecurity.java

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,6 +139,7 @@
139139
*
140140
* @author Rob Winch
141141
* @author Joe Grandja
142+
* @author Ngoc Nhan
142143
* @since 3.2
143144
* @see EnableWebSecurity
144145
*/
@@ -3721,12 +3722,7 @@ private <C extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSec
37213722
}
37223723

37233724
private ObservationRegistry getObservationRegistry() {
3724-
ApplicationContext context = getContext();
3725-
String[] names = context.getBeanNamesForType(ObservationRegistry.class);
3726-
if (names.length == 1) {
3727-
return (ObservationRegistry) context.getBean(names[0]);
3728-
}
3729-
return ObservationRegistry.NOOP;
3725+
return getContext().getBeanProvider(ObservationRegistry.class).getIfUnique(() -> ObservationRegistry.NOOP);
37303726
}
37313727

37323728
/**

config/src/main/java/org/springframework/security/config/annotation/web/configuration/HttpSecurityConfiguration.java

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
import java.util.List;
2121
import java.util.Map;
2222

23-
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2423
import org.springframework.beans.factory.annotation.Autowired;
2524
import org.springframework.context.ApplicationContext;
2625
import org.springframework.context.annotation.Bean;
@@ -56,6 +55,7 @@
5655
*
5756
* @author Eleftheria Stein
5857
* @author Jinwoo Bae
58+
* @author Ngoc Nhan
5959
* @since 5.4
6060
*/
6161
@Configuration(proxyBeanMethods = false)
@@ -226,21 +226,9 @@ private PasswordEncoder getPasswordEncoder() {
226226
if (this.passwordEncoder != null) {
227227
return this.passwordEncoder;
228228
}
229-
PasswordEncoder passwordEncoder = getBeanOrNull(PasswordEncoder.class);
230-
if (passwordEncoder == null) {
231-
passwordEncoder = PasswordEncoderFactories.createDelegatingPasswordEncoder();
232-
}
233-
this.passwordEncoder = passwordEncoder;
234-
return passwordEncoder;
235-
}
236-
237-
private <T> T getBeanOrNull(Class<T> type) {
238-
try {
239-
return this.applicationContext.getBean(type);
240-
}
241-
catch (NoSuchBeanDefinitionException ex) {
242-
return null;
243-
}
229+
this.passwordEncoder = this.applicationContext.getBeanProvider(PasswordEncoder.class)
230+
.getIfUnique(PasswordEncoderFactories::createDelegatingPasswordEncoder);
231+
return this.passwordEncoder;
244232
}
245233

246234
@Override

config/src/main/java/org/springframework/security/config/annotation/web/configurers/ExpressionUrlAuthorizationConfigurer.java

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2022 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.
@@ -75,6 +75,7 @@
7575
* @param <H> the type of {@link HttpSecurityBuilder} that is being configured
7676
* @author Rob Winch
7777
* @author Yanming Zhou
78+
* @author Ngoc Nhan
7879
* @since 3.2
7980
* @see org.springframework.security.config.annotation.web.builders.HttpSecurity#authorizeRequests()
8081
* @deprecated Use {@link AuthorizeHttpRequestsConfigurer} instead
@@ -106,10 +107,9 @@ public final class ExpressionUrlAuthorizationConfigurer<H extends HttpSecurityBu
106107
* @see HttpSecurity#authorizeRequests()
107108
*/
108109
public ExpressionUrlAuthorizationConfigurer(ApplicationContext context) {
109-
String[] grantedAuthorityDefaultsBeanNames = context.getBeanNamesForType(GrantedAuthorityDefaults.class);
110-
if (grantedAuthorityDefaultsBeanNames.length == 1) {
111-
GrantedAuthorityDefaults grantedAuthorityDefaults = context.getBean(grantedAuthorityDefaultsBeanNames[0],
112-
GrantedAuthorityDefaults.class);
110+
GrantedAuthorityDefaults grantedAuthorityDefaults = context.getBeanProvider(GrantedAuthorityDefaults.class)
111+
.getIfUnique();
112+
if (grantedAuthorityDefaults != null) {
113113
this.rolePrefix = grantedAuthorityDefaults.getRolePrefix();
114114
}
115115
else {
@@ -167,22 +167,11 @@ private SecurityExpressionHandler<FilterInvocation> getExpressionHandler(H http)
167167
}
168168
ApplicationContext context = http.getSharedObject(ApplicationContext.class);
169169
if (context != null) {
170-
String[] roleHiearchyBeanNames = context.getBeanNamesForType(RoleHierarchy.class);
171-
if (roleHiearchyBeanNames.length == 1) {
172-
defaultHandler.setRoleHierarchy(context.getBean(roleHiearchyBeanNames[0], RoleHierarchy.class));
173-
}
174-
String[] grantedAuthorityDefaultsBeanNames = context.getBeanNamesForType(GrantedAuthorityDefaults.class);
175-
if (grantedAuthorityDefaultsBeanNames.length == 1) {
176-
GrantedAuthorityDefaults grantedAuthorityDefaults = context
177-
.getBean(grantedAuthorityDefaultsBeanNames[0], GrantedAuthorityDefaults.class);
178-
defaultHandler.setDefaultRolePrefix(grantedAuthorityDefaults.getRolePrefix());
179-
}
180-
String[] permissionEvaluatorBeanNames = context.getBeanNamesForType(PermissionEvaluator.class);
181-
if (permissionEvaluatorBeanNames.length == 1) {
182-
PermissionEvaluator permissionEvaluator = context.getBean(permissionEvaluatorBeanNames[0],
183-
PermissionEvaluator.class);
184-
defaultHandler.setPermissionEvaluator(permissionEvaluator);
185-
}
170+
context.getBeanProvider(RoleHierarchy.class).ifUnique(defaultHandler::setRoleHierarchy);
171+
context.getBeanProvider(GrantedAuthorityDefaults.class)
172+
.ifUnique((grantedAuthorityDefaults) -> defaultHandler
173+
.setDefaultRolePrefix(grantedAuthorityDefaults.getRolePrefix()));
174+
context.getBeanProvider(PermissionEvaluator.class).ifUnique(defaultHandler::setPermissionEvaluator);
186175
}
187176
this.expressionHandler = postProcess(defaultHandler);
188177
return this.expressionHandler;

config/src/main/java/org/springframework/security/config/annotation/web/configurers/RememberMeConfigurer.java

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2023 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.
@@ -18,7 +18,6 @@
1818

1919
import java.util.UUID;
2020

21-
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2221
import org.springframework.context.ApplicationContext;
2322
import org.springframework.security.authentication.AuthenticationManager;
2423
import org.springframework.security.authentication.RememberMeAuthenticationProvider;
@@ -78,6 +77,7 @@
7877
*
7978
* @author Rob Winch
8079
* @author Eddú Meléndez
80+
* @author Ngoc Nhan
8181
* @since 3.2
8282
*/
8383
public final class RememberMeConfigurer<H extends HttpSecurityBuilder<H>>
@@ -444,20 +444,12 @@ private <C> C getSharedOrBean(H http, Class<C> type) {
444444
if (shared != null) {
445445
return shared;
446446
}
447-
return getBeanOrNull(type);
448-
}
449447

450-
private <T> T getBeanOrNull(Class<T> type) {
451448
ApplicationContext context = getBuilder().getSharedObject(ApplicationContext.class);
452449
if (context == null) {
453450
return null;
454451
}
455-
try {
456-
return context.getBean(type);
457-
}
458-
catch (NoSuchBeanDefinitionException ex) {
459-
return null;
460-
}
452+
return context.getBeanProvider(type).getIfUnique();
461453
}
462454

463455
}

config/src/main/java/org/springframework/security/config/annotation/web/configurers/RequestCacheConfigurer.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 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.
@@ -20,7 +20,6 @@
2020
import java.util.Collections;
2121
import java.util.List;
2222

23-
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
2423
import org.springframework.context.ApplicationContext;
2524
import org.springframework.http.MediaType;
2625
import org.springframework.security.config.annotation.web.HttpSecurityBuilder;
@@ -67,6 +66,7 @@
6766
* </ul>
6867
*
6968
* @author Rob Winch
69+
* @author Ngoc Nhan
7070
* @since 3.2
7171
* @see RequestCache
7272
*/
@@ -134,12 +134,8 @@ private <T> T getBeanOrNull(Class<T> type) {
134134
if (context == null) {
135135
return null;
136136
}
137-
try {
138-
return context.getBean(type);
139-
}
140-
catch (NoSuchBeanDefinitionException ex) {
141-
return null;
142-
}
137+
138+
return context.getBeanProvider(type).getIfUnique();
143139
}
144140

145141
@SuppressWarnings("unchecked")

0 commit comments

Comments
 (0)