Skip to content

Commit

Permalink
Polish use getBeanProvider instead of getBeanNamesForType and getBean
Browse files Browse the repository at this point in the history
  • Loading branch information
kse-music committed Oct 18, 2024
1 parent a6ee985 commit 13754cc
Show file tree
Hide file tree
Showing 6 changed files with 31 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -143,10 +143,8 @@ public void setObjectPostProcessor(ObjectPostProcessor<Object> objectPostProcess
}

private AuthenticationEventPublisher getAuthenticationEventPublisher(ApplicationContext context) {
if (context.getBeanNamesForType(AuthenticationEventPublisher.class).length > 0) {
return context.getBean(AuthenticationEventPublisher.class);
}
return this.objectPostProcessor.postProcess(new DefaultAuthenticationEventPublisher());
return context.getBeanProvider(AuthenticationEventPublisher.class)
.getIfUnique(() -> this.objectPostProcessor.postProcess(new DefaultAuthenticationEventPublisher()));
}

@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,10 +142,8 @@ private AuthenticationManager authenticationManager() throws Exception {
}

private AuthenticationEventPublisher getAuthenticationEventPublisher() {
if (this.context.getBeanNamesForType(AuthenticationEventPublisher.class).length > 0) {
return this.context.getBean(AuthenticationEventPublisher.class);
}
return this.objectPostProcessor.postProcess(new DefaultAuthenticationEventPublisher());
return this.context.getBeanProvider(AuthenticationEventPublisher.class)
.getIfUnique(() -> this.objectPostProcessor.postProcess(new DefaultAuthenticationEventPublisher()));
}

private void applyDefaultConfigurers(HttpSecurity http) throws Exception {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -77,19 +77,12 @@ public final class AuthorizeHttpRequestsConfigurer<H extends HttpSecurityBuilder
*/
public AuthorizeHttpRequestsConfigurer(ApplicationContext context) {
this.registry = new AuthorizationManagerRequestMatcherRegistry(context);
if (context.getBeanNamesForType(AuthorizationEventPublisher.class).length > 0) {
this.publisher = context.getBean(AuthorizationEventPublisher.class);
}
else {
this.publisher = new SpringAuthorizationEventPublisher(context);
}
this.roleHierarchy = SingletonSupplier.of(() -> (context.getBeanNamesForType(RoleHierarchy.class).length > 0)
? context.getBean(RoleHierarchy.class) : new NullRoleHierarchy());
String[] grantedAuthorityDefaultsBeanNames = context.getBeanNamesForType(GrantedAuthorityDefaults.class);
if (grantedAuthorityDefaultsBeanNames.length > 0) {
GrantedAuthorityDefaults grantedAuthorityDefaults = context.getBean(GrantedAuthorityDefaults.class);
this.rolePrefix = grantedAuthorityDefaults.getRolePrefix();
}
this.publisher = context.getBeanProvider(AuthorizationEventPublisher.class)
.getIfUnique(() -> new SpringAuthorizationEventPublisher(context));
this.roleHierarchy = SingletonSupplier
.of(() -> context.getBeanProvider(RoleHierarchy.class).getIfUnique(NullRoleHierarchy::new));
context.getBeanProvider(GrantedAuthorityDefaults.class)
.ifUnique((grantedAuthorityDefaults) -> this.rolePrefix = grantedAuthorityDefaults.getRolePrefix());
ResolvableType type = ResolvableType.forClassWithGenerics(ObjectPostProcessor.class,
ResolvableType.forClassWithGenerics(AuthorizationManager.class, HttpServletRequest.class));
ObjectProvider<ObjectPostProcessor<AuthorizationManager<HttpServletRequest>>> provider = context
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2023 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -363,12 +363,8 @@ AuthenticationManager getAuthenticationManager(H http) {

BearerTokenResolver getBearerTokenResolver() {
if (this.bearerTokenResolver == null) {
if (this.context.getBeanNamesForType(BearerTokenResolver.class).length > 0) {
this.bearerTokenResolver = this.context.getBean(BearerTokenResolver.class);
}
else {
this.bearerTokenResolver = new DefaultBearerTokenResolver();
}
this.bearerTokenResolver = this.context.getBeanProvider(BearerTokenResolver.class)
.getIfUnique(DefaultBearerTokenResolver::new);
}
return this.bearerTokenResolver;
}
Expand Down Expand Up @@ -422,12 +418,8 @@ public OAuth2ResourceServerConfigurer<H> and() {

Converter<Jwt, ? extends AbstractAuthenticationToken> getJwtAuthenticationConverter() {
if (this.jwtAuthenticationConverter == null) {
if (this.context.getBeanNamesForType(JwtAuthenticationConverter.class).length > 0) {
this.jwtAuthenticationConverter = this.context.getBean(JwtAuthenticationConverter.class);
}
else {
this.jwtAuthenticationConverter = new JwtAuthenticationConverter();
}
this.jwtAuthenticationConverter = this.context.getBeanProvider(JwtAuthenticationConverter.class)
.getIfUnique(JwtAuthenticationConverter::new);
}
return this.jwtAuthenticationConverter;
}
Expand Down Expand Up @@ -527,10 +519,7 @@ OpaqueTokenAuthenticationConverter getAuthenticationConverter() {
if (this.authenticationConverter != null) {
return this.authenticationConverter;
}
if (this.context.getBeanNamesForType(OpaqueTokenAuthenticationConverter.class).length > 0) {
return this.context.getBean(OpaqueTokenAuthenticationConverter.class);
}
return null;
return this.context.getBeanProvider(OpaqueTokenAuthenticationConverter.class).getIfUnique(() -> null);
}

AuthenticationProvider getAuthenticationProvider() {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2002-2022 the original author or authors.
* Copyright 2002-2024 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
Expand Down Expand Up @@ -29,11 +29,15 @@ final class MessageMatcherAuthorizationManagerConfiguration {
@Scope("prototype")
MessageMatcherDelegatingAuthorizationManager.Builder messageAuthorizationManagerBuilder(
ApplicationContext context) {
return MessageMatcherDelegatingAuthorizationManager.builder()
.simpDestPathMatcher(
() -> (context.getBeanNamesForType(SimpAnnotationMethodMessageHandler.class).length > 0)
? context.getBean(SimpAnnotationMethodMessageHandler.class).getPathMatcher()
: new AntPathMatcher());
return MessageMatcherDelegatingAuthorizationManager.builder().simpDestPathMatcher(() -> {
SimpAnnotationMethodMessageHandler messageHandler = context
.getBeanProvider(SimpAnnotationMethodMessageHandler.class)
.getIfUnique();
if (messageHandler == null) {
return new AntPathMatcher();
}
return messageHandler.getPathMatcher();
});
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.UUID;
import java.util.function.Consumer;
import java.util.function.Function;
Expand Down Expand Up @@ -1836,13 +1837,6 @@ private <T> T getBeanOrNull(String beanName, Class<T> requiredClass) {
}
}

private <T> String[] getBeanNamesForTypeOrEmpty(Class<T> beanClass) {
if (this.context == null) {
return new String[0];
}
return this.context.getBeanNamesForType(beanClass);
}

protected void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
this.context = applicationContext;
}
Expand Down Expand Up @@ -5376,16 +5370,9 @@ protected ReactiveJwtDecoder getJwtDecoder() {
}

protected Converter<Jwt, ? extends Mono<? extends AbstractAuthenticationToken>> getJwtAuthenticationConverter() {
if (this.jwtAuthenticationConverter != null) {
return this.jwtAuthenticationConverter;
}

if (getBeanNamesForTypeOrEmpty(ReactiveJwtAuthenticationConverter.class).length > 0) {
return getBean(ReactiveJwtAuthenticationConverter.class);
}
else {
return new ReactiveJwtAuthenticationConverter();
}
return Objects.requireNonNullElseGet(this.jwtAuthenticationConverter,
() -> ServerHttpSecurity.this.context.getBeanProvider(ReactiveJwtAuthenticationConverter.class)
.getIfUnique(ReactiveJwtAuthenticationConverter::new));
}

private ReactiveAuthenticationManager getAuthenticationManager() {
Expand Down

0 comments on commit 13754cc

Please sign in to comment.