navikt / mock-oauth2-server

A scriptable/customizable web server for testing HTTP clients using OAuth2/OpenID Connect or applications with a dependency to a running OAuth2 server (i.e. APIs requiring signed JWTs from a known issuer)
MIT License
248 stars 59 forks source link

No qualifying bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' available #735

Open wimdeblauwe opened 3 months ago

wimdeblauwe commented 3 months ago

I am using Spring Boot 3.3.2 and try to integrate this library to avoid having to integrate with Okta for our integration tests.

I created this context initializer:

public class MockOAuth2ServerInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext> {

    @Override
    public void initialize(ConfigurableApplicationContext applicationContext) {
        var server = registerMockOAuth2Server(applicationContext);
        var baseUrl = server.baseUrl().toString().replaceAll("/$", "");

        String issuer = baseUrl + "/issuer1";
        TestPropertyValues
                .of(Map.of("okta.oauth2.issuer", issuer))
                .applyTo(applicationContext);
    }

    private MockOAuth2Server registerMockOAuth2Server(ConfigurableApplicationContext applicationContext) {
        var ssl = new Ssl();
        OAuth2Config config = new OAuth2Config(false, null, null, false, new OAuth2TokenProvider(), Collections.emptySet(), new MockWebServerWrapper(ssl));
        var server = new MockOAuth2Server(config);
        server.start();
        ((GenericApplicationContext) applicationContext).registerBean(MockOAuth2Server.class, () -> server);
        return server;
    }
}

And using it from my @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT). However, I get this error at startup:

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.oauth2.client.registration.ClientRegistrationRepository' available
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:343)
    at org.springframework.beans.factory.support.DefaultListableBeanFactory.getBean(DefaultListableBeanFactory.java:334)
    at org.springframework.context.support.AbstractApplicationContext.getBean(AbstractApplicationContext.java:1261)
    at org.springframework.security.config.annotation.web.configurers.oauth2.client.OAuth2ClientConfigurerUtils.getClientRegistrationRepositoryBean(OAuth2ClientConfigurerUtils.java:58)
    at org.springframework.security.config.annotation.web.configurers.oauth2.client.OAuth2ClientConfigurerUtils.getClientRegistrationRepository(OAuth2ClientConfigurerUtils.java:50)
    at org.springframework.security.config.annotation.web.configurers.oauth2.client.OAuth2LoginConfigurer.init(OAuth2LoginConfigurer.java:342)
    at org.springframework.security.config.annotation.web.configurers.oauth2.client.OAuth2LoginConfigurer.init(OAuth2LoginConfigurer.java:160)
    at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.init(AbstractConfiguredSecurityBuilder.java:365)
    at org.springframework.security.config.annotation.AbstractConfiguredSecurityBuilder.doBuild(AbstractConfiguredSecurityBuilder.java:327)
    at org.springframework.security.config.annotation.AbstractSecurityBuilder.build(AbstractSecurityBuilder.java:38)
...

Any idea how to fix this? Anybody else using this for integration testing when they use Okta?