Skip to content

Commit 13754cc

Browse files
committed
Polish use getBeanProvider instead of getBeanNamesForType and getBean
1 parent a6ee985 commit 13754cc

File tree

6 files changed

+31
-62
lines changed

6 files changed

+31
-62
lines changed

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,8 @@ public void setObjectPostProcessor(ObjectPostProcessor<Object> objectPostProcess
143143
}
144144

145145
private AuthenticationEventPublisher getAuthenticationEventPublisher(ApplicationContext context) {
146-
if (context.getBeanNamesForType(AuthenticationEventPublisher.class).length > 0) {
147-
return context.getBean(AuthenticationEventPublisher.class);
148-
}
149-
return this.objectPostProcessor.postProcess(new DefaultAuthenticationEventPublisher());
146+
return context.getBeanProvider(AuthenticationEventPublisher.class)
147+
.getIfUnique(() -> this.objectPostProcessor.postProcess(new DefaultAuthenticationEventPublisher()));
150148
}
151149

152150
@SuppressWarnings("unchecked")

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

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -142,10 +142,8 @@ private AuthenticationManager authenticationManager() throws Exception {
142142
}
143143

144144
private AuthenticationEventPublisher getAuthenticationEventPublisher() {
145-
if (this.context.getBeanNamesForType(AuthenticationEventPublisher.class).length > 0) {
146-
return this.context.getBean(AuthenticationEventPublisher.class);
147-
}
148-
return this.objectPostProcessor.postProcess(new DefaultAuthenticationEventPublisher());
145+
return this.context.getBeanProvider(AuthenticationEventPublisher.class)
146+
.getIfUnique(() -> this.objectPostProcessor.postProcess(new DefaultAuthenticationEventPublisher()));
149147
}
150148

151149
private void applyDefaultConfigurers(HttpSecurity http) throws Exception {

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

Lines changed: 7 additions & 14 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.
@@ -77,19 +77,12 @@ public final class AuthorizeHttpRequestsConfigurer<H extends HttpSecurityBuilder
7777
*/
7878
public AuthorizeHttpRequestsConfigurer(ApplicationContext context) {
7979
this.registry = new AuthorizationManagerRequestMatcherRegistry(context);
80-
if (context.getBeanNamesForType(AuthorizationEventPublisher.class).length > 0) {
81-
this.publisher = context.getBean(AuthorizationEventPublisher.class);
82-
}
83-
else {
84-
this.publisher = new SpringAuthorizationEventPublisher(context);
85-
}
86-
this.roleHierarchy = SingletonSupplier.of(() -> (context.getBeanNamesForType(RoleHierarchy.class).length > 0)
87-
? context.getBean(RoleHierarchy.class) : new NullRoleHierarchy());
88-
String[] grantedAuthorityDefaultsBeanNames = context.getBeanNamesForType(GrantedAuthorityDefaults.class);
89-
if (grantedAuthorityDefaultsBeanNames.length > 0) {
90-
GrantedAuthorityDefaults grantedAuthorityDefaults = context.getBean(GrantedAuthorityDefaults.class);
91-
this.rolePrefix = grantedAuthorityDefaults.getRolePrefix();
92-
}
80+
this.publisher = context.getBeanProvider(AuthorizationEventPublisher.class)
81+
.getIfUnique(() -> new SpringAuthorizationEventPublisher(context));
82+
this.roleHierarchy = SingletonSupplier
83+
.of(() -> context.getBeanProvider(RoleHierarchy.class).getIfUnique(NullRoleHierarchy::new));
84+
context.getBeanProvider(GrantedAuthorityDefaults.class)
85+
.ifUnique((grantedAuthorityDefaults) -> this.rolePrefix = grantedAuthorityDefaults.getRolePrefix());
9386
ResolvableType type = ResolvableType.forClassWithGenerics(ObjectPostProcessor.class,
9487
ResolvableType.forClassWithGenerics(AuthorizationManager.class, HttpServletRequest.class));
9588
ObjectProvider<ObjectPostProcessor<AuthorizationManager<HttpServletRequest>>> provider = context

config/src/main/java/org/springframework/security/config/annotation/web/configurers/oauth2/server/resource/OAuth2ResourceServerConfigurer.java

Lines changed: 6 additions & 17 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.
@@ -363,12 +363,8 @@ AuthenticationManager getAuthenticationManager(H http) {
363363

364364
BearerTokenResolver getBearerTokenResolver() {
365365
if (this.bearerTokenResolver == null) {
366-
if (this.context.getBeanNamesForType(BearerTokenResolver.class).length > 0) {
367-
this.bearerTokenResolver = this.context.getBean(BearerTokenResolver.class);
368-
}
369-
else {
370-
this.bearerTokenResolver = new DefaultBearerTokenResolver();
371-
}
366+
this.bearerTokenResolver = this.context.getBeanProvider(BearerTokenResolver.class)
367+
.getIfUnique(DefaultBearerTokenResolver::new);
372368
}
373369
return this.bearerTokenResolver;
374370
}
@@ -422,12 +418,8 @@ public OAuth2ResourceServerConfigurer<H> and() {
422418

423419
Converter<Jwt, ? extends AbstractAuthenticationToken> getJwtAuthenticationConverter() {
424420
if (this.jwtAuthenticationConverter == null) {
425-
if (this.context.getBeanNamesForType(JwtAuthenticationConverter.class).length > 0) {
426-
this.jwtAuthenticationConverter = this.context.getBean(JwtAuthenticationConverter.class);
427-
}
428-
else {
429-
this.jwtAuthenticationConverter = new JwtAuthenticationConverter();
430-
}
421+
this.jwtAuthenticationConverter = this.context.getBeanProvider(JwtAuthenticationConverter.class)
422+
.getIfUnique(JwtAuthenticationConverter::new);
431423
}
432424
return this.jwtAuthenticationConverter;
433425
}
@@ -527,10 +519,7 @@ OpaqueTokenAuthenticationConverter getAuthenticationConverter() {
527519
if (this.authenticationConverter != null) {
528520
return this.authenticationConverter;
529521
}
530-
if (this.context.getBeanNamesForType(OpaqueTokenAuthenticationConverter.class).length > 0) {
531-
return this.context.getBean(OpaqueTokenAuthenticationConverter.class);
532-
}
533-
return null;
522+
return this.context.getBeanProvider(OpaqueTokenAuthenticationConverter.class).getIfUnique(() -> null);
534523
}
535524

536525
AuthenticationProvider getAuthenticationProvider() {

config/src/main/java/org/springframework/security/config/annotation/web/socket/MessageMatcherAuthorizationManagerConfiguration.java

Lines changed: 10 additions & 6 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.
@@ -29,11 +29,15 @@ final class MessageMatcherAuthorizationManagerConfiguration {
2929
@Scope("prototype")
3030
MessageMatcherDelegatingAuthorizationManager.Builder messageAuthorizationManagerBuilder(
3131
ApplicationContext context) {
32-
return MessageMatcherDelegatingAuthorizationManager.builder()
33-
.simpDestPathMatcher(
34-
() -> (context.getBeanNamesForType(SimpAnnotationMethodMessageHandler.class).length > 0)
35-
? context.getBean(SimpAnnotationMethodMessageHandler.class).getPathMatcher()
36-
: new AntPathMatcher());
32+
return MessageMatcherDelegatingAuthorizationManager.builder().simpDestPathMatcher(() -> {
33+
SimpAnnotationMethodMessageHandler messageHandler = context
34+
.getBeanProvider(SimpAnnotationMethodMessageHandler.class)
35+
.getIfUnique();
36+
if (messageHandler == null) {
37+
return new AntPathMatcher();
38+
}
39+
return messageHandler.getPathMatcher();
40+
});
3741
}
3842

3943
}

config/src/main/java/org/springframework/security/config/web/server/ServerHttpSecurity.java

Lines changed: 4 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
import java.util.Iterator;
3030
import java.util.List;
3131
import java.util.Map;
32+
import java.util.Objects;
3233
import java.util.UUID;
3334
import java.util.function.Consumer;
3435
import java.util.function.Function;
@@ -1836,13 +1837,6 @@ private <T> T getBeanOrNull(String beanName, Class<T> requiredClass) {
18361837
}
18371838
}
18381839

1839-
private <T> String[] getBeanNamesForTypeOrEmpty(Class<T> beanClass) {
1840-
if (this.context == null) {
1841-
return new String[0];
1842-
}
1843-
return this.context.getBeanNamesForType(beanClass);
1844-
}
1845-
18461840
protected void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
18471841
this.context = applicationContext;
18481842
}
@@ -5376,16 +5370,9 @@ protected ReactiveJwtDecoder getJwtDecoder() {
53765370
}
53775371

53785372
protected Converter<Jwt, ? extends Mono<? extends AbstractAuthenticationToken>> getJwtAuthenticationConverter() {
5379-
if (this.jwtAuthenticationConverter != null) {
5380-
return this.jwtAuthenticationConverter;
5381-
}
5382-
5383-
if (getBeanNamesForTypeOrEmpty(ReactiveJwtAuthenticationConverter.class).length > 0) {
5384-
return getBean(ReactiveJwtAuthenticationConverter.class);
5385-
}
5386-
else {
5387-
return new ReactiveJwtAuthenticationConverter();
5388-
}
5373+
return Objects.requireNonNullElseGet(this.jwtAuthenticationConverter,
5374+
() -> ServerHttpSecurity.this.context.getBeanProvider(ReactiveJwtAuthenticationConverter.class)
5375+
.getIfUnique(ReactiveJwtAuthenticationConverter::new));
53895376
}
53905377

53915378
private ReactiveAuthenticationManager getAuthenticationManager() {

0 commit comments

Comments
 (0)