Spring AuthenticationManager that could not be found

0

I'm trying to set up the oAuth2 in my study application but this is returning me an error that I can not solve

  

required to bean of type 'org.springframework.security.authentication.AuthenticationManager'   that could not be found

AuthorizationServerConfig.java

    @Configuration
    @EnableAuthorizationServer
    public class AuthorizationServerConfig extends 

    AuthorizationServerConfigurerAdapter {

        @Autowired
        private AuthenticationManager authenticationManager;

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.inMemory().withClient("client").secret("passwordClient").scopes("read", "write")
                    .authorizedGrantTypes("password").accessTokenValiditySeconds(1800);
        }

        @Override
        public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
            endpoints.tokenStore(tokenStore()).authenticationManager(this.authenticationManager);
        }

    @Bean
    public TokenStore tokenStore() {
        return new InMemoryTokenStore();
    }

}

ResourceServerConfig.java

@Configuration
@EnableWebSecurity
@EnableResourceServer
public class ResourceServerConfig extends ResourceServerConfigurerAdapter {

    @Autowired
    public void configure(AuthenticationManagerBuilder auth) throws Exception {
        auth.inMemoryAuthentication().withUser("admin").password("admin").roles("ROLE");
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        http.authorizeRequests().antMatchers("/categorias").permitAll().anyRequest().authenticated().and()
                .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().csrf().disable();
    }

    @Override
    public void configure(ResourceServerSecurityConfigurer resources) throws Exception {
        resources.stateless(true);
    }
}
    
asked by anonymous 12.08.2018 / 00:07

1 answer

0

I was able to solve the problem in the following way: in the AuthorizationServerConfig class I added the Qualifier follows the new way it was

@Configuration @EnableAuthorizationServer public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {

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



@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
    clients.inMemory().withClient("angular").secret("@ngul@r0").scopes("read", "write")
            .authorizedGrantTypes("password").accessTokenValiditySeconds(1800);
}

@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
    endpoints.tokenStore(tokenStore()).authenticationManager(authenticationManager);
}

@Bean
public TokenStore tokenStore() {
    return new InMemoryTokenStore();
}

}

In the ResourceServerConfig class I made an extends from WebSecurityConfigurerAdapter, I just noted with @Configuration and created the authenticationManagerBean method: follows the new form of the class

@Configuration public class ResourceServerConfig extends WebSecurityConfigurerAdapter {

@Autowired
public void configure(AuthenticationManagerBuilder auth) throws Exception {
    auth.inMemoryAuthentication().withUser("adm").password("admin").roles("ROLE");
}

@Override
public void configure(HttpSecurity http) throws Exception {
    http.authorizeRequests().antMatchers("/categorias").permitAll().anyRequest().authenticated().and()
            .sessionManagement().sessionCreationPolicy(SessionCreationPolicy.STATELESS).and().csrf().disable();
}

@Override
@Bean
public AuthenticationManager authenticationManagerBean() throws Exception {
    return super.authenticationManagerBean();
}
// @Override
// public void configure(ResourceServerSecurityConfigurer resources) throws
// Exception {
// resources.stateless(true);
// }

}

    
14.08.2018 / 05:49