将UserService注入到jhipster中的SecurityConfiguration会导致spring bean循环依赖

时间:2017-03-13 22:45:04

标签: java spring jhipster auth0

我正在尝试配置jhipster 4应用程序以使用auth0进行身份验证。当我最初生成项目时,我选择了JWT选项,这似乎工作正常并提供JWT支持。我现在要做的是正确处理与auth0的集成,通过阅读他们提供的JWT令牌并在第一次看到新的auth0用户名时创建用户。

为此,我计划将UserService注入JWTFilter提供的jhipster,并在第一次看到用户名时创建一个新帐户:

private UserService userService;

public JWTFilter(TokenProvider tokenProvider, UserRepository userRepository, UserService userService) {
    this.tokenProvider = tokenProvider;
    this.userRepository = userRepository;
    this.userService = userService;
}

为了做到这一点,我必须将UserService添加到JWTConfigurer的构造函数中,并将JWTFilter配置为:

package com.proj.security.jwt;

import com.proj.repository.UserRepository;
import com.proj.service.UserService;
import org.springframework.security.config.annotation.SecurityConfigurerAdapter;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.web.DefaultSecurityFilterChain;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;

public class JWTConfigurer extends SecurityConfigurerAdapter<DefaultSecurityFilterChain, HttpSecurity> {

    public static final String AUTHORIZATION_HEADER = "Authorization";

    public static final String AUTHORIZATION_TOKEN = "access_token";

    private TokenProvider tokenProvider;

    private UserRepository userRepository;

    private UserService userService;

    public JWTConfigurer(TokenProvider tokenProvider, UserRepository userRepository, UserService userService) {
        this.tokenProvider = tokenProvider;
        this.userRepository = userRepository;
        this.userService = userService;
    }

    @Override
    public void configure(HttpSecurity http) throws Exception {
        JWTFilter customFilter = new JWTFilter(tokenProvider, userRepository, userService);
        http.addFilterBefore(customFilter, UsernamePasswordAuthenticationFilter.class);
    }
}

最后,必须将其注入SecurityConfiguration

的构造函数中
public SecurityConfiguration(AuthenticationManagerBuilder authenticationManagerBuilder, UserDetailsService userDetailsService,
        TokenProvider tokenProvider, CorsFilter corsFilter, UserRepository userRepository, UserService userService) {

    this.authenticationManagerBuilder = authenticationManagerBuilder;
    this.userDetailsService = userDetailsService;
    this.tokenProvider = tokenProvider;
    this.corsFilter = corsFilter;
    this.userRepository = userRepository;
    this.userService = userService;
}

并添加到JWTConfigurer bean

private JWTConfigurer securityConfigurerAdapter() {
    return new JWTConfigurer(tokenProvider, userRepository, userService);
}

但是,当我用maven启动应用程序时,我遇到以下错误:

2017-03-13 22:41:00.816  WARN 45420 --- [  restartedMain] o.s.boot.SpringApplication               : Error handling failed (Error creating bean with name 'delegatingApplicationListener' defined in class path resource [org/springframework/security/config/annotation/web/configuration/WebSecurityConfiguration.class]: BeanPostProcessor before instantiation of bean failed; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.springframework.cache.annotation.ProxyCachingConfiguration': Initialization of bean failed; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'org.springframework.context.annotation.ConfigurationClassPostProcessor.importRegistry' available)
2017-03-13 22:41:00.823 ERROR 45420 --- [  restartedMain] o.s.b.d.LoggingFailureAnalysisReporter   : 

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

┌─────┐
|  securityConfiguration defined in file [/Users/user/work/proj/target/classes/com/proj/config/SecurityConfiguration.class]
↑     ↓
|  userService defined in file [/Users/user/work/proj/target/classes/com/proj/service/UserService.class]
└─────┘

UserService没有注入SecurityConfiguration,所以我不确定如何解决这个问题。还有另外一种解决这个问题的方法吗?

1 个答案:

答案 0 :(得分:0)

最后,我通过将SecurityConfiguration类混合constructor and setter injection来解决这个问题:

@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true, securedEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {

    private final AuthenticationManagerBuilder authenticationManagerBuilder;

    private final UserDetailsService userDetailsService;

    private UserRepository userRepository;

    private final TokenProvider tokenProvider;

    private final CorsFilter corsFilter;

    private UserService userService;

    public SecurityConfiguration(AuthenticationManagerBuilder authenticationManagerBuilder, UserDetailsService userDetailsService,
            TokenProvider tokenProvider, CorsFilter corsFilter, UserRepository userRepository) {

        this.authenticationManagerBuilder = authenticationManagerBuilder;
        this.userDetailsService = userDetailsService;
        this.tokenProvider = tokenProvider;
        this.corsFilter = corsFilter;
        this.userRepository = userRepository;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}

一旦我这样做,我看到使用正确的参数调用构造函数,然后构造UserService,然后按照我希望的那样注入SecurityConfiguration的setter。