Analysis and Solutions for Bean Creation Errors in Spring Boot with Spring Security Integration

Dec 03, 2025 · Programming · 10 views · 7.8

Keywords: Spring Boot | Spring Security | Bean Creation Error

Abstract: This article provides an in-depth analysis of the common 'Error creating bean with name \'securityFilterChainRegistration\'' error encountered when integrating Spring Security into Spring Boot projects. Through a detailed case study, it explores the root causes, including improper dependency management, configuration conflicts, and proxy class access exceptions. Based on the best-practice answer, the article systematically proposes solutions such as using Spring Boot Starter dependencies, optimizing security configuration classes, removing redundant annotations, and adjusting bean definition order. With code examples and configuration adjustments, it explains how to avoid version incompatibilities and auto-configuration conflicts to ensure correct initialization of the security filter chain. Finally, it summarizes key points for maintaining Spring Security stability in microservices architecture, offering a comprehensive troubleshooting and repair guide for developers.

Problem Background and Error Analysis

When integrating Spring Security into Spring Boot applications, developers often encounter bean creation failures, specifically manifested as Error creating bean with name 'securityFilterChainRegistration'. This error typically stems from version mismatches or configuration conflicts. According to the provided case, the exception stack trace shows IllegalAccessError: class ...$Proxy49 cannot access its superinterface ...LazyBean, indicating an access permission issue during proxy class generation, potentially related to class loaders or dependency conflicts.

Root Cause Investigation

The core causes of the error can be summarized as follows:

From a technical perspective, IllegalAccessError often occurs when dynamic proxies (e.g., JDK Proxy) attempt to access restricted interfaces, possibly due to multiple versions of Spring Security libraries in the classpath breaking type consistency.

Solutions and Best Practices

Based on the best answer, resolving this error requires the following steps:

  1. Optimize Dependency Management: Remove explicit Spring and Spring Security dependencies and switch to Spring Boot Starters. This ensures version compatibility and avoids conflicts. For example, in pom.xml:
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>1.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <version>1.2.0.RELEASE</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-data-mongodb</artifactId>
    <version>1.2.0.RELEASE</version>
</dependency>

If using spring-boot-starter-parent as the parent project, version numbers can be omitted, and Tomcat version can be specified via <properties>, e.g., <tomcat.version>8.0.15</tomcat.version>.

<ol start="2">
  • Adjust Security Configuration Class: Remove @EnableWebMvcSecurity, as Spring Boot auto-handles this annotation. Add @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER) to ensure custom configuration takes precedence over defaults. Example code:
  • @Configuration
    @Order(SecurityProperties.ACCESS_OVERRIDE_ORDER)
    public class ApplicationSecurity extends WebSecurityConfigurerAdapter {
        @Override
        protected void configure(HttpSecurity http) throws Exception {
            // Configure HTTP security rules
        }
        
        @Override
        public void configure(AuthenticationManagerBuilder auth) throws Exception {
            auth.userDetailsService(new MyUserDetailsService(this.mongoTemplate))
                .passwordEncoder(new ShaPasswordEncoder(256));
        }
    }

    This simplifies authentication provider setup, avoiding manual creation of DaoAuthenticationProvider instances.

    <ol start="3">
  • Remove Custom FilterRegistrationBean: Spring Boot auto-registers the security filter chain, so the securityFilterChainRegistration bean definition in the case can be deleted to reduce conflict risks.
  • In-Depth Analysis and Extended Discussion

    From an architectural perspective, this error highlights the balance between Spring Boot auto-configuration and custom configurations. Spring Boot manages components via conditional bean registration (e.g., @ConditionalOnMissingBean), but version mismatches can disrupt this mechanism. Developers should:

    Additionally, the subsequent 404 error (Whitelabel Error Page) in the case might result from security rules intercepting unconfigured paths. It is recommended to add .antMatchers("/error").permitAll() in configure(HttpSecurity http) to allow access to error pages.

    Conclusion and Recommendations

    The key to resolving Spring Security integration errors lies in dependency consistency and configuration clarity. By using Spring Boot Starters, optimizing annotation order, and simplifying bean definitions, common pitfalls can be effectively avoided. In practice, it is advised to:

    Through these measures, developers can not only fix current errors but also enhance project maintainability and scalability, laying a foundation for building secure microservices applications.

    Copyright Notice: All rights in this article are reserved by the operators of DevGex. Reasonable sharing and citation are welcome; any reproduction, excerpting, or re-publication without prior permission is prohibited.