SpringBoot整合SpringSecurity遇jakarta.servlet.ServletException缺失问题求助
整合SpringSecurity的SpringBoot项目遇jakarta.servlet.ServletException依赖缺失问题
问题详情
开发整合SpringSecurity的SpringBoot应用时,编译报错:
java: cannot access jakarta.servlet.ServletException
class file for jakarta.servlet.ServletException not found
已知SpringBoot会自动管理容器与Servlet依赖,手动调整SpringBoot、Spring版本后问题仍未解决,附上项目pom.xml:
<?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>2.6.1</version> <!-- <relativePath/> --> </parent> <groupId>com.example</groupId> <artifactId>SpringSecurityMaven</artifactId> <version>0.0.1-SNAPSHOT</version> <name>SpringSecurityMaven</name> <description>SpringSecurityMaven</description> <properties> <java.version>17</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-webmvc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>6.1.2</version> </dependency> <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-jdbc</artifactId> <version>6.1.0</version> </dependency> <!-- Spring Security --> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-config</artifactId> </dependency> <!-- H2 Database --> <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <version>2.2.224</version> </dependency> <!-- Thymeleaf Dependencies --> <dependency> <groupId>org.thymeleaf</groupId> <artifactId>thymeleaf-spring6</artifactId> <version>3.1.2.RELEASE</version> </dependency> <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-web</artifactId> <version>6.1.2</version> </dependency> <dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter-api</artifactId> <version>5.10.0-M1</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-core</artifactId> <version>6.0.11</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>6.0.11</version> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> <version>3.2.1</version> </plugin> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-dependency-plugin</artifactId> <version>3.2.0</version> <executions> <execution> <id>generate-dependency-tree</id> <phase>validate</phase> <goals> <goal>tree</goal> </goals> </execution> </executions> </plugin> </plugins> </build> </project>
问题原因
你的pom.xml存在严重版本不兼容:
- SpringBoot 2.6.1基于Spring 5.x,依赖
javax.servlet规范;但你手动引入了Spring 6.x、Spring Security 6.x组件,这类版本依赖jakarta.servlet规范,两者包名完全不同,导致找不到类。 - SpringBoot Maven插件版本3.2.1与父项目2.6.1不匹配,进一步引发依赖管理混乱。
修复方案
方案一:升级到SpringBoot 3.x(推荐)
SpringBoot 3.x原生支持Jakarta EE,适配Spring 6.x与Spring Security 6.x,操作如下:
- 统一父项目与插件版本:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.1</version> </parent>
- 替换Spring Security依赖为starter,自动管理config与web组件:
<!-- 替换原有spring-security-config和spring-security-web --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-security</artifactId> </dependency>
- 移除所有手动指定版本的Spring组件(spring-webmvc、spring-jdbc、spring-core、spring-context),由父项目统一管理版本。
- 替换Thymeleaf依赖为SpringBoot starter:
<!-- 替换原有thymeleaf-spring6 --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-thymeleaf</artifactId> </dependency>
- 移除手动添加的
junit-jupiter-api,spring-boot-starter-test已包含兼容版本的JUnit 5。
方案二:降级到Spring 5.x栈(不推荐)
若坚持使用SpringBoot 2.6.1,需将所有高版本依赖降级:
- spring-webmvc、spring-jdbc、spring-core、spring-context改为5.3.22
- spring-security-config、spring-security-web改为5.6.9
- thymeleaf-spring6改为thymeleaf-spring5,版本3.0.15.RELEASE
内容的提问来源于stack exchange,提问作者APY
相关产品推荐
相关产品推荐

