Spring 需要一个类型为“AuthenticationManager"的 bean [英] Spring required a bean of type 'AuthenticationManager'
问题描述
我一直在尝试按照教程找到
AuthApplication.java
package com.spud.auth;导入 org.springframework.beans.factory.annotation.Autowired;导入 org.springframework.boot.SpringApplication;导入 org.springframework.boot.autoconfigure.SpringBootApplication;导入 org.springframework.context.annotation.Configuration;导入 org.springframework.security.authentication.AuthenticationManager;导入 org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;导入 org.springframework.security.config.annotation.web.builders.HttpSecurity;导入 org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;导入 org.springframework.security.oauth2.config.annotation.configurers.ClientDetailsServiceConfigurer;导入 org.springframework.security.oauth2.config.annotation.web.configuration.AuthorizationServerConfigurerAdapter;导入 org.springframework.security.oauth2.config.annotation.web.configuration.EnableAuthorizationServer;导入 org.springframework.security.oauth2.config.annotation.web.configuration.EnableResourceServer;导入 org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerEndpointsConfigurer;导入 org.springframework.security.oauth2.config.annotation.web.configurers.AuthorizationServerSecurityConfigurer;@SpringBootApplication@EnableResourceServer公共类 AuthApplication {公共静态无效主(字符串 [] args){SpringApplication.run(AuthApplication.class, args);}@配置受保护的静态类 LoginConfig 扩展了 WebSecurityConfigurerAdapter {@覆盖protected void configure(HttpSecurity http) 抛出异常 {http.requestMatchers().antMatchers("/login", "/oauth/authorize").and().authorizeRequests().anyRequest().authenticated().and().formLogin().permitAll();}@覆盖受保护的无效配置(AuthenticationManagerBuilder auth)抛出异常{auth.inMemoryAuthentication().withUser("user").password("password").roles("USER");}}@配置@EnableAuthorizationServer受保护的静态类 OAuth2Config 扩展了 AuthorizationServerConfigurerAdapter {@自动连线私有 AuthenticationManager authenticationManager;@覆盖公共无效配置(ClientDetailsServiceConfigurer 客户端)抛出异常 {clients.inMemory().withClient("foo").secret("bar").authorizedGrantTypes("authorization_code", "refresh_token", "password").scopes("user_info").autoApprove(true);}@覆盖公共无效配置(AuthorizationServerSecurityConfigurer oauthServer)抛出异常{oauthServer.tokenKeyAccess("permitAll()").checkTokenAccess("isAuthenticated()");}@覆盖public void configure(AuthorizationServerEndpointsConfigurer endpoints) 抛出异常 {endpoints.authenticationManager(authenticationManager);}}}
UserController.java
package com.spud.controllers;导入 java.security.Principal;导入 org.springframework.web.bind.annotation.GetMapping;导入 org.springframework.web.bind.annotation.RestController;@RestController公共类用户控制器{@GetMapping("/用户/我")公共主体用户(主体主体){返还本金;}}
application.properties
server.context-path=/sso-server
给出的错误(不是运行时的完整控制台输出,但这是错误)
***************************应用程序无法启动****************************描述:com.spud.auth.AuthApplication$OAuth2Config 中的字段 authenticationManager 需要一个无法找到的org.springframework.security.authentication.AuthenticationManager"类型的 bean.行动:考虑在您的配置中定义一个 'org.springframework.security.authentication.AuthenticationManager' 类型的 bean.
您必须将 AuthenticationManager
公开为 spring bean 描述 此处.
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
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.
You have to expose the AuthenticationManager
as spring bean described here.
这篇关于Spring 需要一个类型为“AuthenticationManager"的 bean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!