org.zodiac.autoconfigure.loadbalancer.security.OAuth2AppLoadBalancerClientAutoConfiguration Maven / Gradle / Ivy
package org.zodiac.autoconfigure.loadbalancer.security;
import java.util.ArrayList;
import java.util.List;
import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.security.oauth2.OAuth2AutoConfiguration;
import org.springframework.boot.autoconfigure.security.oauth2.resource.UserInfoRestTemplateCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.http.client.ClientHttpRequestInterceptor;
import org.zodiac.commons.constants.SystemPropertiesConstants;
import org.zodiac.core.bootstrap.loadbalancer.AppLoadBalancerInterceptor;
import org.zodiac.core.bootstrap.loadbalancer.RetryAppLoadBalancerInterceptor;
@SpringBootConfiguration
@ConditionalOnClass(value = {org.springframework.security.oauth2.client.OAuth2RestTemplate.class})
@ConditionalOnProperty(value = SystemPropertiesConstants.Zodiac.SPRING_BOOTSTRAP_OAUTH2_LOAD_BALANCED_ENABLED, havingValue = "true")
@AutoConfigureAfter(value = {OAuth2AutoConfiguration.class})
public class OAuth2AppLoadBalancerClientAutoConfiguration {
@SpringBootConfiguration
@ConditionalOnBean(value = {AppLoadBalancerInterceptor.class})
protected static class UserInfoLoadBalancerConfig {
@Bean
protected UserInfoRestTemplateCustomizer
loadBalancedUserInfoRestTemplateCustomizer(final AppLoadBalancerInterceptor loadBalancerInterceptor) {
return restTemplate -> {
List interceptors = new ArrayList<>(restTemplate.getInterceptors());
interceptors.add(loadBalancerInterceptor);
restTemplate.setInterceptors(interceptors);
};
}
}
@SpringBootConfiguration
@ConditionalOnBean(value = {RetryAppLoadBalancerInterceptor.class})
protected static class UserInfoRetryLoadBalancerConfig {
@Bean
protected UserInfoRestTemplateCustomizer retryLoadBalancedUserInfoRestTemplateCustomizer(
final RetryAppLoadBalancerInterceptor loadBalancerInterceptor) {
return new UserInfoRestTemplateCustomizer() {
@Override
public void customize(org.springframework.security.oauth2.client.OAuth2RestTemplate restTemplate) {
List interceptors = new ArrayList<>(restTemplate.getInterceptors());
interceptors.add(loadBalancerInterceptor);
restTemplate.setInterceptors(interceptors);
}
};
}
}
}