简体   繁体   English

Spring需要一个'AuthenticationManager'类型的bean

[英]Spring required a bean of type 'AuthenticationManager'

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. 我一直在尝试按照这里找到的教程设置演示,以帮助我在我的本地机器上理解SSO,然后再在另一个项目中实现。 I have run into a problem that has left me stuck. 我遇到了一个让我陷入困境的问题。 I receive and error telling me to add a bean. 我收到并告诉我添加一个bean的错误。 Please let me know what code I am missing. 请告诉我我错过的代码。 I cannot get the program to run. 我无法让程序运行。

Tree of file system 文件系统树

在此输入图像描述

AuthApplication.java 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 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 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.

您必须将AuthenticationManager公开为此处描述的spring bean。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 需要一个 org.springframework.security.authentication.AuthenticationManager 类型的 bean - required a bean of type org.springframework.security.authentication.AuthenticationManager *** 中的字段 authenticationManager 需要一个找不到的 'org.springframework.security.authentication.AuthenticationManager' 类型的 bean - Field authenticationManager in *** required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found service.SecurityServiceImpl 中的字段 authenticationManager 需要类型为“org.springframework.security.authentication.AuthenticationManager”的 bean - Field authenticationManager in service.SecurityServiceImpl required a bean of type 'org.springframework.security.authentication.AuthenticationManager' 带有xml config的Spring安全性NoSuchBeanDefenitionException否AuthenticationManager类型的合格Bean - Spring security with xml config NoSuchBeanDefenitionException No Qualifying bean of type AuthenticationManager 春季启动OAuth2实现:NoSuchBeanDefinitionException:没有类型为AuthenticationManager的合格Bean - Spring-boot OAuth2 implementation: NoSuchBeanDefinitionException: No qualifying bean of type AuthenticationManager Spring安全性:期望只为类型接口org.springframework.security.authentication.AuthenticationManager找到一个bean。 - Spring security : Expecting to only find a single bean for type interface org.springframework.security.authentication.AuthenticationManager 需要一个无法通过Spring Boot找到的类型的bean - Required a bean of type that could not be found Spring Boot spring 现场服务需要一个 bean 类型 - spring field service required a bean of type 字段 authenticationManager LoginController 需要一个无法找到的 typeAuthenticationManager' bean - Field authenticationManager LoginController required a bean of typeAuthenticationManager' that could not be found Spring Field需要一个找不到Spring JPA类型的bean - Spring Field required a bean of type that could not be found Spring JPA
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM