如何解决为什么配置类会与自身形成循环依赖?
我不明白这个类是怎么产生循环依赖注入的。 我试过添加 @Lazy 注释或从构造函数中删除 @Autowired 但它没有帮助。房间里可能有一头我没看到的大象。
***************************
APPLICATION Failed TO START
***************************
Description:
The dependencies of some of the beans in the application context form a cycle:
┌─────┐
| securityConfiguration defined in file [E:\Users\Adelin\eclipse-workspace2\AuthenticationspringJWTAngular\target\classes\src\main\configuration\SecurityConfiguration.class]
这是配置类:
package src.main.configuration;
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
import static src.main.constant.SecurityConstant.PUBLIC_URLS;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter;
import src.main.filter.JWTAccessDeniedHandler;
import src.main.filter.JWTAuthenticationEntryPoint;
import src.main.filter.JWTAuthorizationFilter;
import src.main.service.IUserDetailsService;
@Configuration
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
private JWTAuthorizationFilter jwtAuthorizationFilter;
private JWTAccessDeniedHandler jwtAccessDeniedHandler;
private JWTAuthenticationEntryPoint jwtAuthenticationEntryPoint;
private IUserDetailsService userDetailsService;
private BCryptPasswordEncoder bCryptPasswordEncoder;
@Autowired
public SecurityConfiguration(JWTAuthorizationFilter jwtAuthorizationFilter,JWTAccessDeniedHandler jwtAccessDeniedHandler,JWTAuthenticationEntryPoint jwtAuthenticationEntryPoint,IUserDetailsService userDetailsService,BCryptPasswordEncoder bCryptPasswordEncoder) {
this.jwtAuthorizationFilter = jwtAuthorizationFilter;
this.jwtAccessDeniedHandler = jwtAccessDeniedHandler;
this.jwtAuthenticationEntryPoint = jwtAuthenticationEntryPoint;
this.userDetailsService = userDetailsService;
this.bCryptPasswordEncoder = bCryptPasswordEncoder;
}
@Override
protected void configure(AuthenticationManagerBuilder amb) throws Exception {
amb.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder);
}
@Override
protected void configure(HttpSecurity http) throws Exception {
http.csrf().disable()//
.cors().and() //
// we're not using sessions,not keeping track of currently logging in users
// users prove by using a token -> stateless
.sessionManagement().sessionCreationPolicy(STATELESS).and() //
.authorizeRequests().antMatchers(PUBLIC_URLS).permitAll() // no authentication
// needed
.anyRequest().authenticated().and() // anything else - reqs. authentication
.exceptionHandling().accessDeniedHandler(jwtAccessDeniedHandler)
.authenticationEntryPoint(jwtAuthenticationEntryPoint).and()
.addFilterBefore(jwtAuthorizationFilter,UsernamePasswordAuthenticationFilter.class);
}
@Bean
@Override
public AuthenticationManager authenticationManager() throws Exception {
return super.authenticationManager();
}
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
}
解决方法
我怀疑是因为这个
SecurityConfiguration
的构造函数用 @Autowired
类型的参数标记为 BCryptPasswordEncoder
所以基本上为了实例化 SecurityConfiguration
它需要一个 BCryptPasswordEncoder
类型的 bean .
但话说回来,同一个类有一个实例方法,它正在创建类型为 BCryptPasswordEncoder
的 bean。
所以基本上,构造函数不能被调用,因为它依赖于方法返回的值,而方法不能被调用,因为还没有调用构造函数来实例化它。
如果您将方法更改为以下(我不是建议将其作为解决方案,而只是为了验证我的理解),那么它可能会起作用。
@Bean
public static BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。