spring-boot icon indicating copy to clipboard operation
spring-boot copied to clipboard

@EnableOAuth2Sso custom config detection doesn't detect @Imported configs

Open william-tran opened this issue 10 years ago • 13 comments

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);
    }
}

william-tran avatar Feb 05 '16 14:02 william-tran

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.

wilkinsona avatar Feb 05 '16 15:02 wilkinsona

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:

  1. Application is processed
  2. It imports FooConfig
  3. FooConfig is processed
  4. It is annotated with @EnableOAuth2Sso which imports OAuth2SsoCustomConfiguration
  5. OAuth2SsoCustomConfiguration is processed
  6. WebSecurityEnhancerCondition is evaluated and it finds no matches as the bean definition for FooConfig has not yet been created.

@jhoeller do you have any suggestions? The difference in behaviour between import and component scan surprised me.

wilkinsona avatar Feb 05 '16 15:02 wilkinsona

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.

william-tran avatar Feb 05 '16 17:02 william-tran

I think this needs to be documented clearly for the end user, I struggled with this issue for several days.

ghost avatar Apr 20 '16 18:04 ghost

@snicoll Could you mention this issue to Jürgen please?

wilkinsona avatar Apr 21 '16 13:04 wilkinsona

@wilkinsona I already did :)

snicoll avatar Apr 21 '16 13:04 snicoll

I've not looked at this deeply, but I wonder if WebSecurityEnhancerCondition needs to implement ConfigurationCondition and use the REGISTER_BEAN phase.

philwebb avatar Apr 21 '16 22:04 philwebb

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.

ghillert avatar May 17 '16 16:05 ghillert

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.

mariuszluciow avatar Jun 02 '16 09:06 mariuszluciow

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 @EnableOAuth2Sso annotation. That's because I don't need the OAuth2SsoDefaultConfiguration which enables the default SSO WebSecurityConfigurerAdapter as well as OAuth2SsoCustomConfiguration which 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 @EnableOAuth2Client above @EnableOAuth2Sso must be processed before ResourceServerTokenServicesConfiguration, hence in https://github.com/dick-the-deployer/dick/pull/160/files#diff-f129b59353fc52f3f8231b9d9deeb9caR71 the import order.
  • With @EnableOAuth2Sso left out @EnableConfigurationProperties(OAuth2SsoProperties.class) must be present.
  • I have to register OAuth2ClientAuthenticationConfigurer and 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 order OAuth2ClientContextFilter will be not pushed before WebSecurity filter (OAuth2RestOperationsConfiguration is 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.

mariuszluciow avatar Jun 04 '16 12:06 mariuszluciow

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!

CobraFlow avatar Nov 03 '16 16:11 CobraFlow

I just discussed the underlying framework problem with @jhoeller and I've raised SPR-15929

snicoll avatar Sep 06 '17 15:09 snicoll

Spring Boot 2.0 has dropped support for this in favor of the OAuth support that's coming in Spring Security 5.x.

mbhave avatar Sep 27 '17 16:09 mbhave