java HttpSecurity与Sping Boot 3.0不兼容

oalqel3c  于 2023-03-16  发布在  Java
关注(0)|答案(1)|浏览(221)

我把Sping Boot 版本从2.5.2换到了3.0.2,并且做了一些重构。但是这个问题一直困扰着我。HttpSecurity addFilter()方法需要javax.servlet.filter,但是Spring Boot 3.0使用jakarta。如何解决这个问题?代码来自Youtube教程,但是是针对Spring Boot 2.5.2的
Maven依赖项:

<dependencies>
        <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>org.yaml</groupId>
            <artifactId>snakeyaml</artifactId>
            <version>2.0</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.mysql</groupId>
            <artifactId>mysql-connector-j</artifactId>
            <version>8.0.31</version>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>com.auth0</groupId>
            <artifactId>java-jwt</artifactId>
            <version>4.2.1</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
            <version>5.7.5</version>
        </dependency>
    </dependencies>

由于存在传递依赖漏洞,snakeyaml版本已更改为2.0
添加了屏幕截图

nom7f22z

nom7f22z1#

如果您打算使用Spring Boot3.x,那么您可以使用最新的spring-cloud bom来管理依赖项,如下面的pom.xml,它将修复javax.xxx之间的冲突。

<parent>
        <groupId>org.springframework.cloud</groupId>
        <artifactId>spring-cloud-starter-parent</artifactId>
        <!-- latest spring cloud version which supports spring boot3.x-->
        <version>2022.0.1</version>
    </parent>
    <groupId>your-groupId</groupId>
    <artifactId>your-artifactId</artifactId>
    <version>0.0.1-SNAPSHOT</version>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.security</groupId>
            <artifactId>spring-security-config</artifactId>
        </dependency>

        <!-- other dependencies go below -->
    </dependencies>

但是现在你可能要处理spring-security的代码兼容性问题,自从你把spring-security版本升级到spring-security 6.x,参见spring-security6.x migration doc,有很多变化。
如果您不想处理spring安全代码兼容性问题,那么您应该将spring Boot 降级回2.x。

相关问题