【问题标题】:Spring boot : delegateBuilder cannot be null on autowiring authenticationManager in custom UserDetailsServiceSpring boot:delegateBuilder 在自定义 UserDetailsS​​ervice 中的自动装配 authenticationManager 上不能为空
【发布时间】:2019-05-19 16:46:03
【问题描述】:

您好,我是 Spring Boot 新手,正在尝试为我的 rest api 实现安全性。

我正在使用 spring boot 2.0.7.release

我已将我的 WebSecurityConfig 配置如下

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Resource(name = "userService")
    private UserDetailsService userDetailsService;

    @Bean
    @Override
    public AuthenticationManager authenticationManagerBean() throws Exception {
       return super.authenticationManagerBean();
    }

    @Autowired
    public void globalUserDetails(AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService(userDetailsService)
            .passwordEncoder(encoder());
    }

    @Bean
    public JwtAuthenticationFilter authenticationTokenFilterBean() throws Exception {
        return new JwtAuthenticationFilter();
    }
    @Bean
    public PasswordEncoder encoder(){
         PasswordEncoder encoder = new CustomPasswordEncoder(); 
         return encoder;
    }
    ....
   }

我已添加资源名称,以便我可以将其指向自定义 userDetailsS​​ervice。

我尝试通过来配置 authenticationManager Bean,并通过 Qualifier authenticationManager bean 指向 bean,但错误仍然相同。

我的 pom.xml 看起来像是为了安全

......
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>
......

而我实现的 UserServiceImpl 是

@Service(value = "userService")
public class UserServiceImpl implements UserService, UserDetailsService {

    @Autowired
    private UserDAOService userDao;

    @Autowired
    private AuthenticationManager authenticationManager;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
         User user = userDao.findByUsername(username);
         if(user == null){
             throw new UsernameNotFoundException("Invalid username or password.");
          }
         return new org.springframework.security.core.userdetails.User(user.getEmail(), user.getPassword(), getAuthority());
    }

    @Override
    public String login(LoginUser user) {
           // valid user if it exits then do the following

            authenticationManager.authenticate(
                new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()));

           //generate the token and do other process.
    }

以下是错误日志。我只提供了邮件错误

  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'webSecurityConfig': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager' defined in class path resource [com/saikrishna/security/config/WebSecurityConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.authentication.AuthenticationManager]: Circular reference involving containing bean 'webSecurityConfig' - consider declaring the factory method as static for independence from its containing instance. Factory method 'authenticationManagerBean' threw exception; nested exception is java.lang.IllegalArgumentException: delegateBuilder cannot be null
  Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'userService': Unsatisfied dependency expressed through field 'authenticationManager'; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager' defined in class path resource [com/saikrishna/security/config/WebSecurityConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.authentication.AuthenticationManager]: Circular reference involving containing bean 'webSecurityConfig' - consider declaring the factory method as static for independence from its containing instance. Factory method 'authenticationManagerBean' threw exception; nested exception is java.lang.IllegalArgumentException: delegateBuilder cannot be null
  Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.security.authenticationManager' defined in class path resource [com/saikrishna/security/config/WebSecurityConfig.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.authentication.AuthenticationManager]: Circular reference involving containing bean 'webSecurityConfig' - consider declaring the factory method as static for independence from its containing instance. Factory method 'authenticationManagerBean' threw exception; nested exception is java.lang.IllegalArgumentException: delegateBuilder cannot be null
  Caused by: org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.springframework.security.authentication.AuthenticationManager]: Circular reference involving containing bean 'webSecurityConfig' - consider declaring the factory method as static for independence from its containing instance. Factory method 'authenticationManagerBean' threw exception; nested exception is java.lang.IllegalArgumentException: delegateBuilder cannot be null
  Caused by: java.lang.IllegalArgumentException: delegateBuilder cannot be null
  at org.springframework.util.Assert.notNull(Assert.java:193) ~[spring-core-5.0.11.RELEASE.jar:5.0.11.RELEASE]

【问题讨论】:

  • 你可能有一个循环 bean 依赖
  • 只需删除“authenticationManagerBean”方法并重试。我认为这个 bean 不需要显式创建(我做了类似的事情而没有显式声明它)。
  • @Spara 我已经检查了我的整个项目没有循环 bean 依赖
  • @sampathpremarathna 我尝试在发布之前删除 authenticationManagerBean 的 bean 配置,因为我在 userservice 中自动连接 - 类说 bean 未找到的实现类
  • @Saikrishna 哦,我还没有为 JWT 做过这个,但对我来说,WebSecurityConfig 上的 configureGlobal 实现确实有效,对我来说,不需要“UserServiceImpl”上的登录方法。这对我有帮助:o7planning.org/en/11543/…

标签: java spring spring-boot spring-security spring-java-config


【解决方案1】:

为了更好地帮助您,最好指明您遵循哪个参考来实现 JWT 机制。

从概念上讲,这部分源代码是错误的:

@Override
public String login(LoginUser user) {
       // valid user if it exits then do the following

        authenticationManager.authenticate(
            new UsernamePasswordAuthenticationToken(user.getUsername(), user.getPassword()));

       //generate the token and do other process.
}

看看下面的修改是否对你有帮助

1) 考虑使用 Java Config 在单独的配置类中声明您的 bean

@Configuration
public class ServiceConfig{   
   @Bean
   protected UserDAOService daoService()
   {
      return new UserDAOServiceImpl();
   }
   @Bean
   protected UserDetailsService userDetailService( UserDAOService dao )
   {
      return new UserServiceImpl( dao );
   }
   @Bean
   public PasswordEncoder encoder(){
     PasswordEncoder encoder = new CustomPasswordEncoder(); 
     return encoder;
   }
   @Bean
   public JwtAuthenticationFilter authenticationTokenFilterBean() throws Exception{                           {
    return new JwtAuthenticationFilter();
   }
}

2) 修改您的 WebSecurityConfig

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @Override
    protected void configure( AuthenticationManagerBuilder auth) throws Exception {
        auth.userDetailsService( userDetailsService ).passwordEncoder( passwordEncoder );
    }

}

【讨论】:

  • 我会试试这个
  • 感谢这项工作,但我在 web 安全配置中配置了一个更改 AuthenticationManager,因为它扩展了 WebSecurityConfigurerAdapter,我们正在覆盖
猜你喜欢
  • 1970-01-01
  • 2013-10-11
  • 2016-03-29
  • 1970-01-01
  • 1970-01-01
  • 2013-02-11
  • 2013-05-12
  • 2015-09-14
  • 1970-01-01
相关资源
最近更新 更多