Spring Authenticationmanager that could not be found

Asked

Viewed 748 times

0

I’m trying to set up the Oauth2 in my study application only it is returning me an error that I am not able to solve

required a 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);
    }
}
  • See if that help. Apply in your class ResourceServerConfig. At the moment, you want to inject a bean that does not yet exist. This linked solution helps to expose this bean.

1 answer

0

I managed to solve the problem as follows: in the Authorizationserverconfig class I added the Qualifier follows the new form it has become

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

}

and in the Resourceserverconfig class I made a extends of Websecurityconfigureradapter, I only wrote 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);
// }

}

Browser other questions tagged

You are not signed in. Login or sign up in order to post.