You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot+Vaadin中SecurityConfig的filterChain() Bean创建异常求助

Spring Boot 3.2.2 + Vaadin 24.6.6 持续出现 BeanCreationException 求助

我在Spring Boot 3.2.2搭配Vaadin 24.6.6的应用中反复遇到BeanCreationException,试过各种排查手段都没能解决。错误信息如下:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'VaadinSecurityFilterChainBean' defined in class path resource .../config/SecurityConfig.class]: No matching factory method found on class [... .config.SecurityConfig]: factory bean 'securityConfig'; factory method 'filterChain()'. Check that a method with the specified name exists and that it is non-static。

该错误提示Spring无法在SecurityConfig类中找到filterChain()方法,但我已经按要求定义了这个方法。

已尝试的排查步骤

  • 验证SecurityConfig.java配置
    • 文件位于正确包路径:com.clarkemiddle.skitripvaadin.config
    • 已添加@EnableWebSecurity和@Configuration注解
    • 继承自VaadinWebSecurity
    • filterChain()方法签名正确:@Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception
    • 方法为非静态
  • 编译与类文件检查
    • 多次执行mvn clean install
    • 确认target/classes/com/clarkemiddle/skitripvaadin/config目录下存在SecurityConfig.class
    • 通过反编译工具确认类文件中存在filterChain方法
  • 命令行启动测试:使用mvn spring-boot:run启动应用,问题未解决
  • 依赖检查
    • 执行mvn dependency:tree排查依赖冲突
    • 尝试在pom.xml中显式指定vaadin-spring-security版本
  • 最小化配置测试:改用仅包含http.authorizeHttpRequests(auth -> auth.anyRequest().permitAll());的极简SecurityConfig,错误依然存在
  • 新项目复现测试:创建全新的Spring Boot/Vaadin项目,复制SecurityConfig.java和LoginView.java后,错误仍出现
  • IDE缓存清理:清理IDE缓存并重启
  • Java版本确认:使用Java 17,符合框架版本要求
  • 应用类验证:确认主应用类路径正确且带有@SpringBootApplication注解

相关代码

SecurityConfig.java

import com.clarkemiddle.skitripvaadin.view.LoginView;
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.core.userdetails.User;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.security.provisioning.InMemoryUserDetailsManager;
import org.springframework.security.provisioning.UserDetailsManager;
import com.vaadin.flow.spring.security.VaadinWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@EnableWebSecurity
@Configuration
public class SecurityConfig extends VaadinWebSecurity {

    @Bean
    public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
        http.authorizeHttpRequests(auth -> auth
                .requestMatchers("/", "/register", "/login").permitAll()
                .anyRequest().authenticated()
        );

        setLoginView(http, LoginView.class);
        return http.build();
    }

    @Bean
    UserDetailsManager userDetailsManager() {
        // 生产环境请替换为数据库实现的UserDetailsService
        return new InMemoryUserDetailsManager(
                User.withUsername("username")
                        .password(passwordEncoder().encode("123456"))
                        .roles("USER").build()
        );
    }

    @Bean
    public PasswordEncoder passwordEncoder() {
        return new BCryptPasswordEncoder();
    }
}

pom.xml 依赖配置

<dependencies>
    <dependency>
        <groupId>com.vaadin</groupId>
        <artifactId>vaadin-spring-boot-starter</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-security</artifactId>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.33</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.security</groupId>
        <artifactId>spring-security-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
</dependencies>
<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>com.vaadin</groupId>
            <artifactId>vaadin-bom</artifactId>
            <version>${vaadin.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>

内容的提问来源于stack exchange,提问作者Aerialdad14

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.14 01:04:55