升级 SpringBoot3 全项目讲解 - 基于 SpringBoot3 的身份认证与 2 代有什么不同?

学会这款 🔥全新设计的 Java 脚手架 ,从此面试不再怕!

在这里插入图片描述
在这里插入图片描述

引言

随着 Spring Boot 3 的发布,许多开发者开始关注其新特性以及如何将其应用到现有项目中。特别是在身份认证这一块,Spring Boot 3 带来了不少变化和改进。本文将深入探讨 Spring Boot 3 在身份认证方面的变化,并与 Spring Boot 2 进行对比,帮助大家更好地理解如何升级和适配。

Spring Boot 3 身份认证的新特性

1. 支持 Jakarta EE 9+

Spring Boot 3 基于 Jakarta EE 9+,这意味着它不再支持 Java EE 8 及以下版本。Jakarta EE 9+ 的命名空间从 javax.* 变更为 jakarta.*,这一变化影响了 Spring Security 的许多核心类。

2. 更强大的 OAuth2 支持

Spring Boot 3 进一步加强了对 OAuth2 的支持,特别是在客户端和资源服务器的配置上。新的 OAuth2AuthorizedClientOAuth2Authorization 类提供了更细粒度的控制。

3. 更灵活的 SecurityFilterChain 配置

Spring Boot 3 引入了更灵活的 SecurityFilterChain 配置方式,允许开发者通过 @Bean 方法来定义多个安全过滤器链,从而更好地支持多租户应用。

Spring Boot 2 与 Spring Boot 3 身份认证的对比

1. 命名空间的变化

在 Spring Boot 2 中,Spring Security 的许多类都位于 javax.* 命名空间下。而在 Spring Boot 3 中,这些类已经迁移到了 jakarta.* 命名空间。例如:

// Spring Boot 2
import javax.servlet.Filter;

// Spring Boot 3
import jakarta.servlet.Filter;

2. OAuth2 配置的变化

在 Spring Boot 2 中,OAuth2 的配置通常通过 @EnableOAuth2Client@EnableOAuth2Sso 注解来实现。而在 Spring Boot 3 中,这些注解已经被弃用,取而代之的是更灵活的 OAuth2AuthorizedClientOAuth2Authorization 类。

// Spring Boot 2
@EnableOAuth2Client
public class OAuth2ClientConfig {
   
   
    // 配置代码
}

// Spring Boot 3
@Bean
public OAuth2AuthorizedClientManager authorizedClientManager(
        ClientRegistrationRepository clientRegistrationRepository,
        OAuth2AuthorizedClientRepository authorizedClientRepository) {
   
   
    OAuth2AuthorizedClientProvider authorizedClientProvider =
            OAuth2AuthorizedClientProviderBuilder.builder()
                    .authorizationCode()
                    .refreshToken()
                    .clientCredentials()
                    .password()
                    .build();

    DefaultOAuth2AuthorizedClientManager authorizedClientManager =
            new DefaultOAuth2AuthorizedClientManager(
                    clientRegistrationRepository, authorizedClientRepository);
    authorizedClientManager.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值