启动Spring Boot应用时遇Spring Security AbstractMethodError问题求助
问题分析与解决
错误原因
- API规范冲突:Spring Boot 3.x全面采用Jakarta EE规范(包前缀为
jakarta.servlet),但你的pom.xml中引入了Java EE时代的javax.servlet-api:4.0.1,两者的ServletContext接口分属不同包,导致类加载时找不到对应实现方法。 - Spring Security版本不一致:你手动指定了
spring-security-core:6.0.3和spring-security-config:5.7.1,版本不匹配且破坏了Spring Boot父依赖的版本管理机制。Spring Security 5.x依赖Java EE的javax.servlet,与Boot 3的Jakarta规范不兼容,这是触发AbstractMethodError的核心原因。
修正后的pom.xml
删除冲突和多余依赖,保留Spring Boot Starter的自动版本管理:
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.0.6</version> <relativePath/> </parent> <groupId>bc.inv</groupId> <artifactId>inv-service</artifactId> <version>0.0.1-SNAPSHOT</version> <name>inv-service</name> <description>inv-service</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-jpa</artifactId> </dependency> <dependency> <groupId>org.postgresql</groupId> <artifactId>postgresql</artifactId> <version>42.6.0</version> </dependency> <dependency> <groupId>org.json</groupId> <artifactId>json</artifactId> <version>20230227</version> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <dependency> <groupId>org.projectlombok</groupId> <artifactId>lombok</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-test</artifactId> <scope>test</scope> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project>
适配Spring Security 6.x的安全配置类
Spring Security 6.x废弃了authorizeRequests()和antMatchers(),改用新的API;同时@EnableGlobalMethodSecurity可替换为更简洁的@MethodSecurity(Spring Boot 3.x默认支持):
@Configuration @EnableWebSecurity @MethodSecurity(prePostEnabled = true, securedEnabled = true, jsr250Enabled = true) public class WebSecurityConfig { @Bean public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { http.csrf(csrf -> csrf.disable()) .authorizeHttpRequests(auth -> auth .requestMatchers("/login", "/inv").authenticated() .anyRequest().permitAll()) // 按需调整其他请求的权限规则 .httpBasic(Customizer.withDefaults()) .sessionManagement(session -> session .sessionCreationPolicy(SessionCreationPolicy.STATELESS)); return http.build(); } }
验证步骤
- 执行
mvn clean install清理并重新构建项目 - 启动Spring Boot应用,确认错误消失
内容的提问来源于stack exchange,提问作者Programmer2B
相关产品推荐
相关产品推荐

