简体   繁体   English

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

[英]circular dependency in web application using spring boot security

Circular dependency error.循环依赖错误。 As I'm new to this technology i couldn't solve this.由于我是这项技术的新手,所以我无法解决这个问题。 i need some here to clear this issue.我需要一些来解决这个问题。 This code is to secure few pages in the existing project.此代码用于保护现有项目中的几个页面。 I have to secure only 3 pages to access only by admin.我必须确保只有 3 个页面才能由管理员访问。 Any solution which can escape circular dependency or which can fulfil my task will help.任何可以避免循环依赖或可以完成我的任务的解决方案都会有所帮助。 My task is to complete secure few pages from accessing the user.我的任务是完成安全的几页访问用户。 This code is taken from a stackoverflow snippet.此代码取自 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());
    }
}

错误信息

In spring.io docs there is "If you use predominantly constructor injection, it is possible to create an unresolvable circular dependency scenario.在 spring.io 文档中有“如果您主要使用构造函数注入,则可能会创建无法解析的循环依赖场景。

For example: Class A requires an instance of class B through constructor injection, and class B requires an instance of class A through constructor injection.例如:Class A通过构造函数注入需要一个class B的实例,class B通过构造函数注入需要一个class A的实例。 If you configure beans for classes A and B to be injected into each other, the Spring IoC container detects this circular reference at runtime, and throws a BeanCurrentlyInCreationException.如果将类 A 和 B 的 bean 配置为相互注入,Spring IoC 容器会在运行时检测到此循环引用,并抛出 BeanCurrentlyInCreationException。

One possible solution is to edit the source code of some classes to be configured by setters rather than constructors.一种可能的解决方案是编辑某些类的源代码,使其由设置器而不是构造器配置。 Alternatively, avoid constructor injection and use setter injection only.或者,避免构造函数注入并仅使用 setter 注入。 In other words, although it is not recommended, you can configure circular dependencies with setter injection."换句话说,虽然不推荐,但你可以使用 setter 注入来配置循环依赖。”

So you should change the way you create one of your beans with using constructor method因此,您应该使用构造函数方法更改创建其中一个 bean 的方式

you can write one line in your application.properties file to remove this error.您可以在application.properties文件中写入一行来消除此错误。
spring.main.allow-circular-references= true

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM