Spring 需要一个 'AuthenticationManager' 类型的 bean
Posted
技术标签:
【中文标题】Spring 需要一个 \'AuthenticationManager\' 类型的 bean【英文标题】:Spring required a bean of type 'AuthenticationManager'Spring 需要一个 'AuthenticationManager' 类型的 bean 【发布时间】:2018-09-03 13:05:41 【问题描述】:我一直在尝试按照找到的教程 HERE 设置演示,以帮助我了解本地计算机上的 SSO,然后再在另一个项目中实施。我遇到了一个让我陷入困境的问题。我收到并错误告诉我添加一个 bean。请让我知道我缺少什么代码。我无法让程序运行。
文件系统树
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(不是来自运行的完整控制台输出,但这是错误)
***************************
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.
【问题讨论】:
How To Inject AuthenticationManager using Java Configuration in a Custom Filter的可能重复 【参考方案1】:您必须将AuthenticationManager
公开为here 描述的spring bean。
【讨论】:
我应该提到我查看了其他一些相关的帖子,包括你链接我的帖子。然而,凌晨 4:00 开始了,我想我的阅读技巧出了问题。我取笑你,又读了一遍。这一次,我意识到“当然,将它放在 OAuth2Config 类中是行不通的”。我在正确的类中实现了它并且它有效。这是一个重复的问题。谢谢你,我很抱歉。 没问题。我将问题标记为重复。以上是关于Spring 需要一个 'AuthenticationManager' 类型的 bean的主要内容,如果未能解决你的问题,请参考以下文章