Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration

Jeff Cook

I followed few suggestions mentioned here, but it didn't work for me. Hence, putting the question here

  1. How To Inject AuthenticationManager using Java Configuration in a Custom Filter
  2. Spring required a bean of type 'AuthenticationManager'

Could anyone please guide me what's the issue and how to fixed that ?

Error:

***************************
APPLICATION FAILED TO START
***************************

Description:

Field authenticationManager in com.techprimers.security.springsecurityauthserver.config.AuthorizationServerConfig required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.


Action:

Consider defining a bean of type 'org.springframework.security.authentication.AuthenticationManager' in your configuration.

AuthorizationServerConfig.java

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

ResourceServerConfig.java

@EnableResourceServer
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {


    @Autowired
    @Qualifier("authenticationManagerBean")
    private AuthenticationManager authenticationManager;
    @Autowired
    private UserDetailsService customUserDetailsService;

    @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)
                .userDetailsService(customUserDetailsService);
    }
}

The code reference taken from https://github.com/TechPrimers/spring-security-oauth-mysql-example, only updated Spring Boot Parent Version to 2.0.4.RELEASE, things started breaking.

Poger

It seems like it's one of the "breaking changes" Spring Boot 2.0 introduced. I believe that your case is described in Spring Boot 2.0 Migration Guide.

In your WebSecurityConfigurerAdapter class you need to override authenticationManagerBean method and annotate it with @Bean, i.e.:

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

Moreover, in your WebSecurityConfigurerAdapter instead of injecting the AuthenticationManager instance with @Autowired you can just use the authenticationManagerBean() method, i.e.:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Consider revisiting the entries above or defining a bean of type 'org.springframework.data.redis.core.RedisTemplate' in your configuration

Consider defining a bean of type 'package' in your configuration [Spring-Boot]

Consider defining a bean of type 'service' in your configuration [Spring boot]

Consider defining a bean of type in your configuration

Could not autowire field: private org.springframework.security.authentication.AuthenticationManager

Consider defining a bean of type 'org.springframework.data.mongodb.core.MongoTemplate' in your configuration

java.lang.NoClassDefFoundError: org/springframework/security/authentication/AuthenticationManager

Spring Boot - Injecting Repository into Controller throws Consider defining a bean of type 'Type' in your configuration

Consider defining a bean of type 'UserConverter' in your configuration

only after refactoring packages names, Consider defining a bean of type * in your configuration

required a bean of type org.springframework.security.authentication.AuthenticationManager

Consider defining a bean of type 'org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder' in your configuration

Consider defining a bean of type * in your configuration

Consider defining a bean of type 'com.gisapp.gisapp.dao.IUserDAO' in your configuration

Field in required a bean of type that could not be found consider defining a bean of type in your configuration

Redis Issue Consider defining a bean of type 'org.springframework.data.redis.core.HashOperations' in your configuration

The type org.springframework.security.authentication.AuthenticationManager cannot be resolved. It is indirectly referenced from required .class files

Consider defining a bean of type 'com.ensat.services.ProductService' in your configuration

SpringBootTest - Consider defining a bean of type 'java.lang.String' in your configuration

How do i resolve Consider defining a bean of type 'java.lang.String' in your configuration?

Consider defining a bean of type 'javax.servlet.ServletContext' in your configuration

Field authenticationManager in service.SecurityServiceImpl required a bean of type 'org.springframework.security.authentication.AuthenticationManager'

APPLICATION FAILED TO START - Consider defining a bean of type in your configuration [SpringBoot]

Consider defining a bean of type 'io.ipl.amaresh.data.JobCompletionNotificationListener' in your configuration. The bean could not be found in that

How to fix "Consider defining a bean of type 'org.jooq.DSLContext' in your configuration." after update to jOOQ 3.15.0

Consider defining a bean of type 'int' in your configuration[SpringBoot]

Consider defining a bean of type '[3rd party dependency]' in your configuration

Consider defining a bean of type 'org.modelmapper.ModelMapper' in your configuration

Consider defining bean of type authentication manager