【发布时间】:2019-02-04 13:16:48
【问题描述】:
org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名为“authorizationServerConfig”的 bean 时出错:不满意 通过字段“authenticationManager”表示的依赖关系;嵌套的 例外是 org.springframework.beans.factory.NoSuchBeanDefinitionException: 否 符合条件的 bean 类型 'org.springframework.security.authentication.AuthenticationManager' 可用:预计至少有 1 个符合 autowire 条件的 bean 候选人。依赖注解: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
您好,我有 spring-boot web 应用程序,我正在尝试按照以下示例使用 Spring Security 和 OAuth2 实现登录/授权-身份验证系统:https://www.youtube.com/watch?v=dTAgI_UsqMg&t=1307s
一切都很好,但是当我运行我的应用程序时,我得到一个异常,说它无法为 AuthenticationManager 找到 bean,甚至认为它在那里并且自动装配。
在互联网上查看这似乎是 Oauth2 的一个已知或常见问题,但我找不到正确的解决方法
有些人建议“公开” AuthenticationManager bean,我不确定在这种情况下这意味着什么
这是我在 github 上当前项目的链接:https://github.com/chenchi13/spring-boot-cms
谁能帮我解决这个问题?
抛出异常的类:
@EnableResourceServer
@Configuration
public class ResourceServerConfig extends WebSecurityConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Autowired
private UserDetailsService customUserDetailService;
@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.parentAuthenticationManager(authenticationManager)
// .inMemoryAuthentication()
// .withUser("Peter")
// .password("peter")
// .roles("USER");
auth.parentAuthenticationManager(authenticationManager)
.userDetailsService(customUserDetailService);
}
}
授权服务器配置:
@Configuration
@EnableAuthorizationServer
public class AuthorizationServerConfig extends AuthorizationServerConfigurerAdapter {
@Autowired
private AuthenticationManager authenticationManager;
@Override
public void configure(AuthorizationServerSecurityConfigurer security) throws Exception {
security.tokenKeyAccess("permitAll()")
.checkTokenAccess("isAuthenticated()");
}
@Override
public void configure(ClientDetailsServiceConfigurer clients) throws Exception {
clients
.inMemory()
.withClient("ClientId")
.secret("secret")
.authorizedGrantTypes("authorization_code")
.scopes("user_info")
.autoApprove(true);
}
@Override
public void configure(AuthorizationServerEndpointsConfigurer endpoints) throws Exception {
endpoints.authenticationManager(authenticationManager);
}
}
【问题讨论】:
标签: java spring spring-mvc spring-boot oauth-2.0