Jeff Cook
Jeff Cook

Reputation: 8774

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

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.

Upvotes: 49

Views: 155938

Answers (7)

Youcef LAIDANI
Youcef LAIDANI

Reputation: 59950

Deprecated "WebSecurityConfigurerAdapter"

As WebSecurityConfigurerAdapter it has been deprecated, you can now use:

@Bean
public AuthenticationManager authenticationManager(HttpSecurity http) throws Exception {
    return http.getSharedObject(AuthenticationManagerBuilder.class)
            .build();
}

Upvotes: 18

Hilal Aissani
Hilal Aissani

Reputation: 735

Just add this to the AuthenticationManagerBuilder

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

And in your controller where you need to use it add this :

@Autowired
private AuthenticationManager authenticationManager;

Upvotes: 5

Sajandeep SIngh
Sajandeep SIngh

Reputation: 1

I forget to write "@Component" at the top of the class. For Example:

@Component
@AllArgsConstructor
public class CustomAuthenticationManager implements AuthenticationManager {

Upvotes: 0

JeanValjean
JeanValjean

Reputation: 17713

You may want to consider to register a GlobalAuthenticationConfigurerAdapter to configure the AuthenticationManager

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration {

    @Bean
    public AuthenticationManager authenticationManager(AuthenticationConfiguration authConfig) throws Exception {
        final List<GlobalAuthenticationConfigurerAdapter> configurers = new ArrayList<>();
        configurers.add(new GlobalAuthenticationConfigurerAdapter() {
                    @Override
                    public void configure(AuthenticationManagerBuilder auth) throws Exception {
                        // auth.doSomething()
                    }
                }
        );
        return authConfig.getAuthenticationManager();
    }

}

where I assumed for instance that you want to register a custom UserDetailsService (i.e. MyUserDetailsService) and a custom password encoder (MyPasswordEncoder).

Upvotes: 0

Poger
Poger

Reputation: 1937

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

Upvotes: 92

Harsh
Harsh

Reputation: 7

You may execute following command: "mvn clean package" and then restart the application

Upvotes: -2

Mayank Avasthi
Mayank Avasthi

Reputation: 9

Consider defining a bean of type 'org.springframework.security.core.userdetails.UserDetails' in your configuration.

Upvotes: -3

Related Questions