Spring required a bean of type 'AuthenticationManager'

Thomas Rokicki :

I have been trying to follow a tutorial found HERE for setting up a demo to help me understand SSO on my local machine before implementing in another project. I have run into a problem that has left me stuck. I receive and error telling me to add a bean. Please let me know what code I am missing. I cannot get the program to run.

Tree of file system

enter image description here

AuthApplication.java

package com.spud.auth;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
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.configuration.EnableResourceServer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;
import org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;

@SpringBootApplication
@EnableResourceServer
public class AuthApplication {

    public static void main(String[] args) {
        SpringApplication.run(AuthApplication.class, args);
    }

    @Configuration
    protected static class LoginConfig extends WebSecurityConfigurerAdapter {

        @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.inMemoryAuthentication().withUser("user").password("password").roles("USER");
        }
    }

    @Configuration
    @EnableAuthorizationServer
    protected static class OAuth2Config extends AuthorizationServerConfigurerAdapter {
        @Autowired
        private AuthenticationManager authenticationManager;

        @Override
        public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
            clients.inMemory().withClient("foo").secret("bar")
                    .authorizedGrantTypes("authorization_code", "refresh_token", "password").scopes("user_info")
                    .autoApprove(true);
        }

        @Override
        public void configure(AuthorizationServerSecurityConfigurer oauthServer) throws Exception {
            oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");
        }

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

UserController.java

package com.spud.controllers;

import java.security.Principal;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserController {

    @GetMapping("/user/me")
    public Principal user(Principal principal) {
        return principal;
    }
}

application.properties

server.context-path=/sso-server

Error Given (not full console output from run but this is the error)

***************************
APPLICATION FAILED TO START
***************************

Description:

Field authenticationManager in com.spud.auth.AuthApplication$OAuth2Config 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.
f1l2 :

You have to expose the AuthenticationManager as spring bean described here.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Spring boot Field required a bean of type that could not be found

'Field required a bean of type that could not be found.' error spring restful API using mongodb

Field personRepositary in () required a bean of type () that could not be found

How to fix “Field … required a bean of type … that could not be found” exception Spring Boot

No bean named authenticationManager

return spring bean with type

Spring boot basic application: field NotesRepository required a bean of type 'com.demo.NotesRepository' that could not be found

required a bean of type org.springframework.security.authentication.AuthenticationManager

Field xx in xx service required a bean of type

How to fix "Field ... required a bean of type ... that could not be found" exception Spring Boot

"A component required a bean of type", but which one?

Spring - Error Parameter 0 of constructor in Service required a bean of type Configuration that could not be found

Field required a bean of type ... that could not be found

Spring MVC AuthenticationManager expected single matching bean but found 4

Parameter 0 of constructor required a bean of type 'FileStoragePropertiesAedImages'

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

Spring-boot OAuth2 implementation: NoSuchBeanDefinitionException: No qualifying bean of type AuthenticationManager

The type org.springframework.security.authentication.AuthenticationManager cannot be resolved. It is indirectly referenced from required .class files

Spring Boot controller required a bean of type 'org.dozer.Mapper' that could not be found

Field authenticationManager in service.SecurityServiceImpl required a bean of type 'org.springframework.security.authentication.AuthenticationManager'

Spring & No unique bean of type

Error creating bean with name AuthenticationManager

Spring configuration - Autowired bean required?

Spring configuring bean with component scan - Field userRepository in service.UserService required a bean of type 'repository

Error required a bean of type 'XXX' that could not be found

SecurityConfiguration required bean of type BCryptPasswordEncoder

Parameter 0 of constructor in .. required a bean type of.. Spring Boot

Parameter 0 of constructor in 'ItemController' required a bean of type '.ItemRepository' that could not be found. (Spring Data Cloud Firestore)

Spring Boot APPLICATION FAILED TO START due to required a bean of type AuthenticationManager