首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Spring boot :在自定义UserDetailsService中自动装配authenticationManager时,delegateBuilder不能为空

Spring boot :在自定义UserDetailsService中自动装配authenticationManager时,delegateBuilder不能为空
EN

Stack Overflow用户
提问于 2018-12-19 16:14:49
回答 1查看 2.3K关注 0票数 2

您好,我是spring boot新手,正在尝试将安全性实现到我的rest中。

我使用的是spring boot 2.0.7.release

我已经将我的WebSecurityConfig配置如下

代码语言:javascript
运行
复制
@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;
    }
    ....
   }

我已经添加了资源名称,这样我就可以指向自定义userDetailsService。

我已经尝试通过来配置authenticationManager Bean,并通过限定符authenticationManager bean指向bean,但它仍然错误不变。

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

代码语言:javascript
运行
复制
......
<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-security</artifactId>
</dependency>
......

我实现的UserServiceImpl是

代码语言:javascript
运行
复制
@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.
    }

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

代码语言:javascript
运行
复制
  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]
EN

Stack Overflow用户

回答已采纳

发布于 2018-12-19 16:36:16

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

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

代码语言:javascript
运行
复制
@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

代码语言:javascript
运行
复制
@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

代码语言:javascript
运行
复制
@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 );
    }

}
票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53847050

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档