microsoft / azure-spring-boot

Spring Boot Starters for Azure services
MIT License
374 stars 460 forks source link

Can't autowire AADB2COidcLoginConfigurer #830

Closed neexs closed 4 years ago

neexs commented 4 years ago

Environment

Summary

I'm getting an error when running app, on following code (Autowiring is not working for the AADB2COidcLoginConfigurer object)

@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    private AADB2COidcLoginConfigurer configurer;

    public WebSecurityConfig(AADB2COidcLoginConfigurer **configurer**) {
        this.configurer = configurer;
    }

    @Override
    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .apply(configurer)
        ;
    }
}

The error : NoSuchBeanDefinitionException: No qualifying bean of type 'com.microsoft.azure.spring.autoconfigure.b2c.AADB2COidcLoginConfigurer' available

Reproduce steps

Following this sample and packaging it with maven/running it : https://github.com/microsoft/azure-spring-boot/tree/master/azure-spring-boot-samples/azure-active-directory-b2c-oidc-spring-boot-sample

Expected Results

No error.

Actual Results

java.lang.IllegalStateException: Failed to load ApplicationContext
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'webSecurityConfig' defined in file [C:\Users\[...]\WebSecurityConfig.class]: Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.microsoft.azure.spring.autoconfigure.b2c.AADB2COidcLoginConfigurer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.microsoft.azure.spring.autoconfigure.b2c.AADB2COidcLoginConfigurer' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {}
saragluna commented 4 years ago

Thanks for reporting this. We'll look into it.

saragluna commented 4 years ago

Hi @neexs, I've run the sample locally and did not run into the same exception. Could you please provide more information, like which version of Spring Boot you're using, or you pom.xml?

geoarchitect commented 4 years ago

@saragluna - We are facing the same issue with this using release 2.2.3 and java 11.

`

com.microsoft.azure azure-active-directory-b2c-spring-boot-starter 2.2.3

`

It appears if we look at the files they do not have spring annotations defining them as beans for autowiring. This should have something like @Component or @Configuration if I am not mistaken.

https://github.com/microsoft/azure-spring-boot/blob/master/azure-spring-boot/src/main/java/com/microsoft/azure/spring/autoconfigure/b2c/AADB2COidcLoginConfigurer.java

You can see any of the other classes for other modules have these annotations like this one for cosmos db.

https://github.com/microsoft/azure-spring-boot/blob/master/azure-spring-boot/src/main/java/com/microsoft/azure/spring/autoconfigure/cosmosdb/CosmosDbRepositoriesAutoConfiguration.java

geoarchitect commented 4 years ago

@saragluna - I worked on this some more and determined there must have been some issue causing the AADB2CAutoConfiguration.java not to do its job.

If I have time ill try and follow my initial steps and see what was missed causing this.

This is working as expected for me now.

saragluna commented 4 years ago

Glad to hear that and thanks for your investigation.

fdr42 commented 4 years ago

same problem with AADAppRoleAuthenticationFilterAutoConfiguration

saragluna commented 4 years ago

@fdr42 Sorry for the late response. Could you help provide more details?

fdr42 commented 4 years ago

I solved the problem which was my bad, sorry

neexs commented 4 years ago

Hi, sorry for the late response, I've got some urgent issues to solve...

I came to the same conclusion as @geoarchitect, but fortunately everything is fine now.

Thank you for your help, and sorry again.

droidamar007 commented 4 years ago

@fdr42 Could you please guide how you resolve your issue.

I'm facing same issue as of now.

I'm using spring boot 2.3.3.RELEASE, java 8, azure-active-directory-spring-boot-starter 2.2.3.

I followed https://docs.microsoft.com/en-us/azure/developer/java/spring-framework/configure-spring-boot-starter-java-app-with-azure-active-directory-b2c-oidc.

Thanks in advance.

gbsmith commented 4 years ago

Having this same problem in Dec 2020. @neexs , @fd42, what were you doing wrong and how did you fix it?

sergei-pavlov commented 3 years ago

@gbsmith in my case the cause of the problem was invalid "application.properties", I assume that these beans are conditional and you have to add some properties keys to create them. For example: "AADAppRoleStatelessAuthenticationFilter" requires "azure.activedirectory.client-id" property