启动服务器遇springSecurityFilterChain Bean创建异常,求技术协助
springSecurityFilterChain Bean Creation Error in Spring Cloud Config Server Hey there, let's sort out this BeanCreationException you're facing when launching your Spring Cloud Config Server—especially since you're following the tutorial without any custom tweaks. Here are the most common fixes to try:
1. Verify Spring Boot & Spring Cloud Version Compatibility
This is the #1 culprit for such bean creation errors. Spring Cloud has strict version mappings with Spring Boot, and mismatched versions will break auto-configurations like Spring Security.
For example:
- If you're using Spring Boot 3.x, pair it with Spring Cloud 2022.0.x (Gateway) or newer (like 2023.0.x for Boot 3.2+).
- If you're on Spring Boot 2.x, use Spring Cloud 2021.0.x (Jubilee) or older.
To fix this, update your pom.xml (or build.gradle) to align the versions:
Example pom.xml Snippet:
<!-- Spring Boot Parent --> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.0</version> <relativePath/> </parent> <!-- Spring Cloud Dependency Management --> <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.cloud</groupId> <artifactId>spring-cloud-dependencies</artifactId> <version>2023.0.0</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement>
2. Fix Security Configuration Class (If Using Modern Spring Boot)
If you're on Spring Boot 2.7+, the old WebSecurityConfigurerAdapter is deprecated. The tutorial might still use the old approach, which conflicts with auto-configured beans. Replace your security config with the modern SecurityFilterChain bean approach:
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.security.config.annotation.web.builders.HttpSecurity; import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.web.SecurityFilterChain; import static org.springframework.security.config.Customizer.withDefaults; @Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeHttpRequests(auth -> auth .anyRequest().authenticated() ) .httpBasic(withDefaults()); // Keeps the default HTTP Basic auth for Config Server return http.build(); } }
3. Clean Up Conflicting Dependencies
Occasionally, transitive dependencies can cause conflicts. Check your dependency tree to ensure you don't have duplicate or mismatched Spring Security jars.
For Maven, run this command to inspect dependencies:
mvn dependency:tree
Look for any spring-security-* artifacts with differing versions, and exclude the conflicting ones if needed.
4. Double-Check Default Security Properties
While you didn't modify anything, ensure your application.properties (or application.yml) doesn't have unintended security settings. For a basic Config Server, the default setup only requires setting a username and password if you want to override the auto-generated one:
spring.security.user.name=config-user spring.security.user.password=config-pass
内容的提问来源于stack exchange,提问作者Jeff Cook

