I'm trying to make a custom spring boot starter that will be used by multiple projects to authenticate with Azure AD. All the Azure AD config has been set up and an individual project hardcoded with all the settings to work with Azure AD works fine too. Now I'm trying to move these settings into a custom Spring Boot starter so that multiple projects can use it. It works for the most part, except for one thing: moving the bean config for a custom AADAppRoleStatelessAuthenticationFilter
. If I leave my custom implementation (CustomAADAppRoleStatelessAuthFilter
) hardcoded in the actual implementing project, everything works and only CustomAADAppRoleStatelessAuthFilter
is created, but as soon as I move it into the custom starter, I only ever get AADAppRoleStatelessAuthenticationFilter
instead.
Note that my CustomAADAppRoleStatelessAuthFilter
extends the starter's
AADAppRoleStatelessAuthenticationFilter
.
The autoconfig for AADAppRoleStatelessAuthenticationFilter
in the azure-spring-boot project (https://github.com/microsoft/azure-spring-boot/blob/master/azure-spring-boot/src/main/java/com/microsoft/azure/spring/autoconfigure/aad/AADAuthenticationFilterAutoConfiguration.java) is:
@Bean
@ConditionalOnMissingBean(AADAppRoleStatelessAuthenticationFilter.class)
@ConditionalOnProperty(prefix = PROPERTY_PREFIX, value = PROPERTY_SESSION_STATELESS, havingValue = "true")
public AADAppRoleStatelessAuthenticationFilter azureADStatelessAuthFilter(ResourceRetriever resourceRetriever) {
//bean details omitted
}
My custom autoconfig that should replace the above is as follows:
@Bean
@ConditionalOnMissingBean(AADAppRoleStatelessAuthenticationFilter.class)
@ConditionalOnProperty(prefix = PROPERTY_PREFIX, value = PROPERTY_SESSION_STATELESS, havingValue = "true")
public AADAppRoleStatelessAuthenticationFilter customAADAppRoleStatelessAuthFilter(
ResourceRetriever resourceRetriever) {
return new CustomAADAppRoleStatelessAuthenticationFilter(/*details omitted*/);
}
No amount of @AutoConfigureBefore(AADAuthenticationFilterAutoConfiguration.class)
works.
Also, if I change the condition on my custom bean to be the subtype (@ConditionalOnMissingBean(CustomAADAppRoleStatelessAuthFilter.class)
), BOTH types get created, and I can autowire my CustomAwareAADAppRoleStatelessAuthFilter
and put it in my WebSecurityConfigurerAdapter
, but things STILL won't work. I debugged things and found that the CustomAADAppRoleStatelessAuthFilter
is the only bean of the ADAppRoleStatelessAuthenticationFilter
type in my spring security filter chain, but that once the 'end of the additional filter chain' has completed and the 'original chain proceeds', I find that the ADAppRoleStatelessAuthenticationFilter
has fired! And of course it throws an error because my CustomAADAppRoleStatelessAuthFilter
has already done things to customize the UserPrincipal
. I can't figure out where the ADAppRoleStatelessAuthenticationFilter
is getting added to any filter chain, and even if I mark my CustomAADAppRoleStatelessAuthFilter
bean with @Primary
, the starter ADAppRoleStatelessAuthenticationFilter
will still be used instead.
The only 'solutions' that have worked are to define the CustomAADAppRoleStatelessAuthFilter
in the actual implementing project instead of the custom starter project, or to exclude the AADAuthenticationFilterAutoConfiguration
in my actual implementing project's @SpringBootApplication
annotation (Not even excluding it the property-based way works).
Is there a way to make AADAuthenticationFilterAutoConfiguration
s ADAppRoleStatelessAuthenticationFilter
bean definition back off? 'Cause @AutoConfigureBefore(AADAuthenticationFilterAutoConfiguration.class)
on my custom auto configuration class that has my CustomAADAppRoleStatelessAuthFilter
definition doesn't work, and having all the implementing projects explicitly exclude AADAuthenticationFilterAutoConfiguration
isn't the most ideal solution (although at least with that solution they don't all need to declare their own bean definition for CustomAADAppRoleStatelessAuthFilter
).