代码之家  ›  专栏  ›  技术社区  ›  Naveen Kumar

使用spring引导安全性的web应用程序中的循环依赖

  •  0
  • Naveen Kumar  · 技术社区  · 3 年前

    循环依赖错误。因为我对这项技术还不熟悉,所以我无法解决这个问题。我需要一些来澄清这个问题。 此代码用于保护现有项目中的几个页面。我必须确保只有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());
        }
    }
    

    error message

    3 回复  |  直到 3 年前
        1
  •  2
  •   Rahul Gupta    3 年前

    在其中一个项目中,我也面临着同样的问题。只需在列表中添加一行即可 application.properties 文件

    spring.main.allow-circular-references=true
    

    这可能会解决你的问题。

        2
  •  1
  •   Sayolima Banerjee    3 年前

    在你的申请中包括这一行。属性文件:

    spring.main.allow-circular-references=true

        3
  •  0
  •   Дима Шуть    3 年前

    春天。我知道了 “如果主要使用构造函数注入,则可能会创建无法解决的循环依赖场景。

    例如:类A通过构造函数注入需要类B的实例,类B通过构造函数注入需要类A的实例。如果为类A和类B配置bean以相互注入,Spring IoC容器会在运行时检测到这个循环引用,并抛出BeanCurrentlyIncremationException。

    一种可能的解决方案是编辑某些类的源代码,由setter而不是构造函数进行配置。或者,避免构造函数注入,只使用setter注入。换句话说,虽然不建议使用setter注入来配置循环依赖项。"

    因此,您应该改变使用构造函数方法创建一个bean的方式