为了账号安全,请及时绑定邮箱和手机立即绑定

Spring-boot OAuth2 实现:NoSuchBeanDefinition

Spring-boot OAuth2 实现:NoSuchBeanDefinition

慕虎7371278 2021-08-19 19:09:28
org.springframework.beans.factory.UnsatisfiedDependencyException:创建名为“authorizationServerConfig”的bean时出错:通过字段“authenticationManager”表达的不满意依赖;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“org.springframework.security.authentication.AuthenticationManager”类型的合格 bean:预计至少有 1 个 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谁能帮我解决这个问题?
查看完整描述

2 回答

?
Qyouu

TA贡献1786条经验 获得超11个赞

从 中删除以下内容ResourceServerConfig:


@Autowired

private AuthenticationManager authenticationManager;

并更改配置方法,如下所示:


@Override

protected void configure(AuthenticationManagerBuilder auth) throws Exception {

    auth.userDetailsService(customUserDetailService);

}

还覆盖以下方法ResourceServerConfig:


@Override

@Bean

public AuthenticationManager authenticationManagerBean() throws Exception {

    return super.authenticationManagerBean();

}

这应该可以解决您的问题。


查看完整回答
反对 回复 2021-08-19
?
叮当猫咪

TA贡献1776条经验 获得超12个赞

我认为您缺少authenticationManagerbean 的定义。我正在添加以下几行,请检查一次:


@Configuration

@EnableWebSecurity

@EnableGlobalMethodSecurity(prePostEnabled = true)

public class SecurityConfig extends WebSecurityConfigurerAdapter {


   // Other Details


   @Bean

   @Override

   protected AuthenticationManager authenticationManager() throws Exception {

      return super.authenticationManager();

   }


   @Override

   protected void configure(AuthenticationManagerBuilder auth) throws Exception {

      auth.userDetailsService(userDetailsService)

              .passwordEncoder(new ShaPasswordEncoder(encodingStrength));

   }


   @Override

   protected void configure(HttpSecurity http) throws Exception {

      http

              .sessionManagement()

              .sessionCreationPolicy(SessionCreationPolicy.STATELESS)

              .and()

              .httpBasic()

              .realmName(securityRealm)

              .and()

              .csrf()

              .disable();


   }


 // Other Details


}

您可以通过下面的参考链接。


查看完整回答
反对 回复 2021-08-19
  • 2 回答
  • 0 关注
  • 175 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信