Leo S
Leo S

Reputation: 339

autowired for authentication manager

I want to implement an authorization and a resource server for Oauth2 and here is my reference site but the private AuthenticationManager authenticationManager gives me an error on intellij like "could not autowire" so how can i fix it?

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;
import org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;

@Configuration
@EnableAuthorizationServer
public class AuthorizationServer extends AuthorizationServerConfigurerAdapter {

    @Autowired
    private AuthenticationManager authenticationManager;

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

    @Override
    public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
        clients.inMemory().withClient("javainuse-client").secret("javainuse-secret")
                .authorizedGrantTypes("client_credentials").scopes("resource-server-read", "resource-server-write");
    }
}

Upvotes: 0

Views: 237

Answers (1)

Gimby
Gimby

Reputation: 5283

If you must use an AuthenticationManager (which is not always true, it depends on how you manage users), one will need to be exposed as a bean. Like so:

@Configuration
public static class AuthenticationMananagerProvider extends WebSecurityConfigurerAdapter {

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

Source (which is an interesting read providing many insights):

https://github.com/spring-projects/spring-boot/issues/11136

Upvotes: 1

Related Questions