3

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);
    }
}
solujic
  • 924
  • 1
  • 18
  • 43

2 Answers2

4

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.

shazin
  • 21,379
  • 3
  • 54
  • 71
  • Hi that did fix this exception but I get another exception: Factory method 'springSecurityFilterChain' threw exception; nested exception is java.lang.NoClassDefFoundError: javax/xml/bind/JAXBException... is this related to this or is this another issue? p.s. Thanks for replying... – solujic Aug 29 '18 at 17:44
  • 1
    Seems like you are using Java 9 or higher. Add the following dependency in your maven or gradle file javax.xml.bind jaxb-api 2.3.0 – shazin Aug 29 '18 at 17:49
  • now i Get this: nested exception is java.lang.RuntimeException: javax.xml.bind.JAXBException: Implementation of JAXB-API has not been found on module path or classpath... ok found the answer to this one here: https://stackoverflow.com/questions/43574426/how-to-resolve-java-lang-noclassdeffounderror-javax-xml-bind-jaxbexception-in-j/48136912#48136912 – solujic Aug 29 '18 at 20:44
1

I think you are missing definition for authenticationManager bean. I am adding the lines below, check it out once:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfig extends WebSecurityConfigurerAdapter {

   // Other Details

   @Bean
   @Override
   protected AuthenticationManager authenticationManager() throws Exception {
      return super.authenticationManager();
   }

   @Override
   protected void configure(AuthenticationManagerBuilder auth) throws Exception {
      auth.userDetailsService(userDetailsService)
              .passwordEncoder(new ShaPasswordEncoder(encodingStrength));
   }

   @Override
   protected void configure(HttpSecurity http) throws Exception {
      http
              .sessionManagement()
              .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
              .and()
              .httpBasic()
              .realmName(securityRealm)
              .and()
              .csrf()
              .disable();

   }

 // Other Details

}

You can go through reference link below.

Reference: Spring Boot with JWT and OAuth2.0

Hope this helps you :)

ramkishorbajpai
  • 199
  • 1
  • 3
  • 15