Spring-boot OAuth2 implementation: NoSuchBeanDefinitionException: No qualifying bean of type AuthenticationManager

solujic

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authorizationServerConfig': Unsatisfied dependency expressed through field 'authenticationManager'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.authentication.AuthenticationManager' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

Hi I have spring-boot web app and I'm trying to implement login/authorization -authentication system using Spring Security and OAuth2 by following this example: https://www.youtube.com/watch?v=dTAgI_UsqMg&t=1307s

Every thing was good but when I run my application I get exception saying it can+t find bean for AuthenticationManager even thought it is there and autowired.

Looking across internet this seems like a know or common issue with Oauth2 but I can't find right workaround

Some people suggested to "expose" the AuthenticationManager bean, I'm not sure what that means in this context

This is link to my current project on github: https://github.com/chenchi13/spring-boot-cms

Can anyone help me figure this out?

class that is throwing exception:

@EnableResourceServer
@Configuration                                                      
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsService customUserDetailService;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.requestMatchers()
                .antMatchers("/login", "/oauth/authorize")
                .and()
                .authorizeRequests()
                .anyRequest()
                .authenticated()
                .and()
                .formLogin()
                .permitAll();
    }


    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        //auth.parentAuthenticationManager(authenticationManager)
        //        .inMemoryAuthentication()
        //        .withUser("Peter")
        //        .password("peter")
        //        .roles("USER");

        auth.parentAuthenticationManager(authenticationManager)
                .userDetailsService(customUserDetailService);
    }
}

Authorization Server Config:

@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {

        security.tokenKeyAccess("permitAll()")
                .checkTokenAccess("isAuthenticated()");
    }


    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients
                .inMemory()
                .withClient("ClientId")
                .secret("secret")
                .authorizedGrantTypes("authorization_code")
                .scopes("user_info")
                .autoApprove(true);
    }


    @Override
    public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {

        endpoints.authenticationManager(authenticationManager);
    }
}
shazin

Remove the following from ResourceServerConfig:

@Autowired
private AuthenticationManager authenticationManager;

And change configure method like following:

@Override
protected void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.userDetailsService(customUserDetailService);
}

Also override the following method in ResourceServerConfig:

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}

This should fix your issue.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

NoSuchBeanDefinitionException: No qualifying bean of type "XInterceptor"

NoSuchBeanDefinitionException: No qualifying bean of type available

NoSuchBeanDefinitionException: No qualifying bean of type 'int'

NoSuchBeanDefinitionException: No qualifying bean of type found

Getting NoSuchBeanDefinitionException: No qualifying bean of type ServerRequest in Spring WebFlux

NoSuchBeanDefinitionException:No qualifying bean of type found for dependency with spring test and Junit

Spring boot test "No qualifying bean of type available"

"No qualifying bean of type" for JPA repository in Spring Boot

Spring Boot JPA - No qualifying bean of type

NoSuchBeanDefinitionException: No qualifying bean of type for inner class

JUnit Test: NoSuchBeanDefinitionException: No qualifying bean of type

How to cure 'NoSuchBeanDefinitionException: No qualifying bean of type' error?

NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.boot.web.reactive.error.ErrorAttributes' available:

spring security: NoSuchBeanDefinitionException: No qualifying bean of type [org.springframework.security.config.annotation.ObjectPostProcessor] found

Spring & Maven & JUnit - BeanCreationException: Could not autowire field NoSuchBeanDefinitionException: No qualifying bean of type

Spring unit test results in NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.mail.javamail.JavaMailSender'

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type in Spring 5

No qualifying bean - FxWeaver and Spring Boot

Spring Boot Multiple Databse : No qualifying bean of type EntityManagerFactoryBuilder

No qualifying bean of type found for dependency in Spring Boot single table

Spring Boot APPLICATION FAILED TO START due to required a bean of type AuthenticationManager

No qualifying bean of type 'javax.persistence.EntityManager' when using Entity Manager with spring boot 2

Spring required a bean of type 'AuthenticationManager'

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type

NoSuchBeanDefinitionException: No qualifying bean of type 'javax.servlet.Filter' available

NoSuchBeanDefinitionException: No qualifying bean of type [project.dao.ServiceUserDao] found for dependency

Spring Boot Testing - No Qualifying Bean Exception

Spring to Spring boot Migration - No qualifying bean of type 'java.lang.String' error

Spring boot test - No qualifying bean of type 'com.example.MyService' available