@EnableOAuth2Sso custom config detection doesn't detect @Imported configs
The WebSecurityEnhancerCondition will only detect beans that have been directly component scanned. context.getBeanFactory().getBeanNamesForAnnotation(EnableOAuth2Sso.class) doesn't seem to traverse beans that have been @Imported. This prevents me from being able to create security configs that use @EnableOAuth2Sso that can be @Imported via my own @EnableWhatever meta-annotations in some library project.
Eg. Given FooConfig in package foo:
package foo;
import org.springframework.boot.autoconfigure.security.oauth2.client.EnableOAuth2Sso;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
@EnableOAuth2Sso
@Configuration
public class FooConfig extends WebSecurityConfigurerAdapter {
@Override
protected void configure(HttpSecurity http) throws Exception {
//
}
}
This works:
package bar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import foo.FooConfig;
@SpringBootApplication
// FooConfig is directly component scanned
@ComponentScan(basePackageClasses= FooConfig.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
This doesn't work, the condition on OAuth2SsoCustomConfiguration won't pass.
package bar;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Import;
import foo.FooConfig;
@SpringBootApplication
// FooConfig is imported
@Import(FooConfig.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
getBeanNamesForAnnotation doesn't know anything about imports. It just looks at all of the beans in the bean factory and returns any with the required annotation. If you change your main method to look up any @EnableOAuth2Sso-annotated beans, it'll find FooConfig:
String[] enablers = SpringApplication.run(Application.class, args)
.getBeanNamesForAnnotation(EnableOAuth2Sso.class);
System.out.println(Arrays.toString(enablers));
[com.example.foo.FooConfig]
It looks like an ordering problem. The condition is being evaluated before the import has been processed.
ConfigurationClassParser registers a bean definition for each configuration class after it gets a complete picture of all of the configuration classes, i.e. after its processed imports, etc.
Here's what's happening:
Applicationis processed- It imports
FooConfig FooConfigis processed- It is annotated with
@EnableOAuth2Ssowhich importsOAuth2SsoCustomConfiguration OAuth2SsoCustomConfigurationis processedWebSecurityEnhancerConditionis evaluated and it finds no matches as the bean definition forFooConfighas not yet been created.
@jhoeller do you have any suggestions? The difference in behaviour between import and component scan surprised me.
Maybe the current behaviour is ok or intended? I made a workaround that disables the WebSecurityEnhancerCondition on OAuth2SsoCustomConfiguration, but I can't have multiple instances of OAuth2SsoCustomConfiguration anyways. If I were to use this mechanism in a library, that disallows the use of @EnableOAuth2Sso in the application code (or least, only one will win). So if the intent is for @EnableOAuth2Sso to only work when directly component scanned in an application, and in this case the developer would expect there to only be one such configuration, then the current behaviour is by design.
I think this needs to be documented clearly for the end user, I struggled with this issue for several days.
@snicoll Could you mention this issue to Jürgen please?
@wilkinsona I already did :)
I've not looked at this deeply, but I wonder if WebSecurityEnhancerCondition needs to implement ConfigurationCondition and use the REGISTER_BEAN phase.
Pretty nasty issue - tripped over it as well. Needed to @import config classes that activate OAuth using @EnableOAuth2Sso. Nothing useful in the logs. OAuth just "silently" stops working.
I'm also having problem with this conditional. We have class annotated with @EnableOAuth2Sso inside our custom auto-configure classes. Sadly these classes are processed after evaluating @WebSecurityEnhancerCondition
If it would be helpful I can prepare small project that fails to configure security correctly due to this issue.
I managed to do workaround this issue here https://github.com/dick-the-deployer/dick/pull/160, and everything seems to work just fine.
In my case the auto-configure is responsible for SSO enabling. Moving this feature from component scanning to auto-configuration classes implies some work thats needs to be done to make sure if we get configuration order just right. There are couple things that are important:
- I'm not using
@EnableOAuth2Ssoannotation. That's because I don't need theOAuth2SsoDefaultConfigurationwhich enables the default SSOWebSecurityConfigurerAdapteras well asOAuth2SsoCustomConfigurationwhich is responsible for enriching custom ones (that's the feature which only works if adapter is available via component scanning) - The
OAuth2ClientConfiguration, which by default is imported by@EnableOAuth2Clientabove@EnableOAuth2Ssomust be processed beforeResourceServerTokenServicesConfiguration, hence in https://github.com/dick-the-deployer/dick/pull/160/files#diff-f129b59353fc52f3f8231b9d9deeb9caR71 the import order. - With
@EnableOAuth2Ssoleft out@EnableConfigurationProperties(OAuth2SsoProperties.class)must be present. - I have to register
OAuth2ClientAuthenticationConfigurerand add authentication entry point manually: https://github.com/dick-the-deployer/dick/pull/160/files#diff-f129b59353fc52f3f8231b9d9deeb9caR84 - Finally, my auto-configuration class has to be analysed before
OAuth2AutoConfiguration, that's why is marked with@AutoConfigureBefore({OAuth2AutoConfiguration.class})annotation. This is pretty important, because if it's not in this orderOAuth2ClientContextFilterwill be not pushed beforeWebSecurityfilter (OAuth2RestOperationsConfigurationis responsible for this configuration), and as a result the exception that is triggering redirect to SSO server won't be processed correctly.
There is one gotcha - conditionals implemented in ResourceServerTokenServicesConfiguration wont work correctly if the properties are added using @PropertySource. Thats probably because the @PropertySources are registered after the evaluation of this conditionals, but I didn't had the chance to dig into this deeper.
During evaluation with the default OAuth2SsoDefaultConfiguration everything worked as expected.
When the class marked with @EnableOAuth2Sso extends WebSecurityConfigurerAdapter overrides of configure(HttpSecurity http) are being ignored as the OAuth2SsoCustomConfiguration is not taking effect.
This is a BIG problem as we import the security implementation depending on configuration.
The workaround for me was to temporarily add a @ComponentScan to my top level @Configuration class and restructure my packages. This was hard to find in an application with complex security requirements!
I just discussed the underlying framework problem with @jhoeller and I've raised SPR-15929
Spring Boot 2.0 has dropped support for this in favor of the OAuth support that's coming in Spring Security 5.x.