[英]circular dependency in web application using spring boot security
循環依賴錯誤。 由於我是這項技術的新手,所以我無法解決這個問題。 我需要一些來解決這個問題。 此代碼用於保護現有項目中的幾個頁面。 我必須確保只有 3 個頁面才能由管理員訪問。 任何可以避免循環依賴或可以完成我的任務的解決方案都會有所幫助。 我的任務是完成安全的幾頁訪問用戶。 此代碼取自 stackoverflow 片段。
@Configuration
@EnableWebSecurity
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {
@Autowired
private UserDetailsService userDetailsService;
@Bean
public BCryptPasswordEncoder bCryptPasswordEncoder() {
return new BCryptPasswordEncoder();
}
@Override
public void configure(HttpSecurity http) throws Exception {
http.authorizeRequests()
.antMatchers("/editincident","editaccident","editreqeust").authenticated()
.anyRequest().permitAll()
.and()
.csrf().disable();
}
@Autowired
public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
auth.userDetailsService(userDetailsService).passwordEncoder(bCryptPasswordEncoder());
}
}
在 spring.io 文檔中有“如果您主要使用構造函數注入,則可能會創建無法解析的循環依賴場景。
例如:Class A通過構造函數注入需要一個class B的實例,class B通過構造函數注入需要一個class A的實例。 如果將類 A 和 B 的 bean 配置為相互注入,Spring IoC 容器會在運行時檢測到此循環引用,並拋出 BeanCurrentlyInCreationException。
一種可能的解決方案是編輯某些類的源代碼,使其由設置器而不是構造器配置。 或者,避免構造函數注入並僅使用 setter 注入。 換句話說,雖然不推薦,但你可以使用 setter 注入來配置循環依賴。”
因此,您應該使用構造函數方法更改創建其中一個 bean 的方式
您可以在application.properties
文件中寫入一行來消除此錯誤。
spring.main.allow-circular-references= true
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.