Skip to content

Commit

Permalink
Polish OAuth2ClientConfiguration
Browse files Browse the repository at this point in the history
  • Loading branch information
thomasdarimont authored and jzheaux committed Sep 30, 2024
1 parent 7fcb42b commit 8b97fdd
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 42 deletions.
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 @@ -29,7 +29,6 @@
import org.springframework.beans.factory.BeanInitializationException;
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
Expand Down Expand Up @@ -118,43 +117,31 @@ OAuth2AuthorizedClientManagerRegistrar authorizedClientManagerRegistrar() {
@Configuration(proxyBeanMethods = false)
static class OAuth2ClientWebMvcSecurityConfiguration implements WebMvcConfigurer {

private OAuth2AuthorizedClientManager authorizedClientManager;
private final OAuth2AuthorizedClientManager authorizedClientManager;

private SecurityContextHolderStrategy securityContextHolderStrategy;
private final ObjectProvider<SecurityContextHolderStrategy> securityContextHolderStrategy;

private OAuth2AuthorizedClientManagerRegistrar authorizedClientManagerRegistrar;
private final OAuth2AuthorizedClientManagerRegistrar authorizedClientManagerRegistrar;

OAuth2ClientWebMvcSecurityConfiguration(ObjectProvider<OAuth2AuthorizedClientManager> authorizedClientManager,
ObjectProvider<SecurityContextHolderStrategy> securityContextHolderStrategy,
OAuth2AuthorizedClientManagerRegistrar authorizedClientManagerRegistrar) {
this.authorizedClientManager = authorizedClientManager.getIfUnique();
this.securityContextHolderStrategy = securityContextHolderStrategy;
this.authorizedClientManagerRegistrar = authorizedClientManagerRegistrar;
}

@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
OAuth2AuthorizedClientManager authorizedClientManager = getAuthorizedClientManager();
if (authorizedClientManager != null) {
OAuth2AuthorizedClientArgumentResolver resolver = new OAuth2AuthorizedClientArgumentResolver(
authorizedClientManager);
if (this.securityContextHolderStrategy != null) {
resolver.setSecurityContextHolderStrategy(this.securityContextHolderStrategy);
}
this.securityContextHolderStrategy.ifAvailable(resolver::setSecurityContextHolderStrategy);
argumentResolvers.add(resolver);
}
}

@Autowired(required = false)
void setAuthorizedClientManager(List<OAuth2AuthorizedClientManager> authorizedClientManagers) {
if (authorizedClientManagers.size() == 1) {
this.authorizedClientManager = authorizedClientManagers.get(0);
}
}

@Autowired(required = false)
void setSecurityContextHolderStrategy(SecurityContextHolderStrategy strategy) {
this.securityContextHolderStrategy = strategy;
}

@Autowired
void setAuthorizedClientManagerRegistrar(
OAuth2AuthorizedClientManagerRegistrar authorizedClientManagerRegistrar) {
this.authorizedClientManagerRegistrar = authorizedClientManagerRegistrar;
}

private OAuth2AuthorizedClientManager getAuthorizedClientManager() {
if (this.authorizedClientManager != null) {
return this.authorizedClientManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@
import org.springframework.beans.factory.ListableBeanFactory;
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.config.BeanDefinition;
import org.springframework.beans.factory.support.BeanDefinitionBuilder;
import org.springframework.beans.factory.support.BeanDefinitionRegistry;
Expand Down Expand Up @@ -93,9 +92,16 @@ ReactiveOAuth2AuthorizedClientManagerRegistrar authorizedClientManagerRegistrar(
@Configuration(proxyBeanMethods = false)
static class OAuth2ClientWebFluxSecurityConfiguration implements WebFluxConfigurer {

private ReactiveOAuth2AuthorizedClientManager authorizedClientManager;
private final ReactiveOAuth2AuthorizedClientManager authorizedClientManager;

private ReactiveOAuth2AuthorizedClientManagerRegistrar authorizedClientManagerRegistrar;
private final ReactiveOAuth2AuthorizedClientManagerRegistrar authorizedClientManagerRegistrar;

OAuth2ClientWebFluxSecurityConfiguration(
ObjectProvider<ReactiveOAuth2AuthorizedClientManager> authorizedClientManager,
ReactiveOAuth2AuthorizedClientManagerRegistrar authorizedClientManagerRegistrar) {
this.authorizedClientManager = authorizedClientManager.getIfUnique();
this.authorizedClientManagerRegistrar = authorizedClientManagerRegistrar;
}

@Override
public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
Expand All @@ -105,19 +111,6 @@ public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
}
}

@Autowired(required = false)
void setAuthorizedClientManager(List<ReactiveOAuth2AuthorizedClientManager> authorizedClientManager) {
if (authorizedClientManager.size() == 1) {
this.authorizedClientManager = authorizedClientManager.get(0);
}
}

@Autowired
void setAuthorizedClientManagerRegistrar(
ReactiveOAuth2AuthorizedClientManagerRegistrar authorizedClientManagerRegistrar) {
this.authorizedClientManagerRegistrar = authorizedClientManagerRegistrar;
}

private ReactiveOAuth2AuthorizedClientManager getAuthorizedClientManager() {
if (this.authorizedClientManager != null) {
return this.authorizedClientManager;
Expand Down

0 comments on commit 8b97fdd

Please sign in to comment.