如何全面升级spring-boot-2.x及Spring-security-oauth2

本文介绍了在将Spring Boot从1.5.x升级到2.5.14,Spring Security OAuth2从2.x升级到2.1.0.RELEASE后,遇到的登录回跳问题。问题在于UsernamePasswordAuthenticationFilter未拦截POST请求,导致登录失败。通过源码分析发现是NotOauthRequestMatcher匹配规则变化引起的。解决方案是调整NotOauthRequestMatcher的匹配规则,避免截胡自定义登录接口。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景介绍

老的项目基于spring-boot-1.5.x、spring-security-oauth2-2.x实现的sso,在近期的安全漏洞扫描中发现如下2个漏洞:

漏洞 涉及jar 修复方案
Spring Framework JDK >= 9 远程代码执行漏洞(CVE-2022-22965) spring-core-4.3.12.RELEASE.jar 升级官方安全版本 >= 5.3.18/5.2.20
Spring Security RegexRequestMatcher 认证绕过漏洞(CVE-2022-22978) spring-security-core-4.2.3.RELEASE.jar 5.5.x 版本使用者建议升级至5.5.7及其以上;
5.6.x 版本使用者建议升级至5.6.4及其以上

然后笔者的同事将spring-boot升级到了2.5.14;将spring-security-oauth2升级到了2.1.0、RELEASE,这样一来,spring-core的版本被间接的升级到了5.3.20,spring-security-core版本变成了5.5.8(主要是spring-boot决定的)。

因为spring-boot-1.x和2.x之间的差距,同步的对项目依赖和代码做了微调。这里不再列出。不是今天的重点。

项目终于能成功启动了,但是一个致命的问题出来了,登录成功后,不能正确的回跳到redirect_uri指定的地址。这里如果你对spring-security-oauth2实现登录认证不熟悉

### Spring Security OAuth2 配置指南 #### 1. 授权服务器 (Authorization Server) 为了配置授权服务器,可以使用 `spring-security-oauth2-authorization-server` 提供的功能。以下是具体的步骤: 引入必要的依赖项: ```xml <dependency> <groupId>org.springframework.security</groupId> <artifactId>spring-security-oauth2-authorization-server</artifactId> <version>1.0.0</version> </dependency> ``` 创建一个类来启用授权服务器功能并定义令牌端点和其他必要设置: ```java 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.core.userdetails.UserDetailsService; import org.springframework.security.crypto.password.PasswordEncoder; import org.springframework.security.oauth2.server.authorization.config.ProviderSettings; @Configuration(proxyBeanMethods = false) public class AuthorizationServerConfig { @Bean public ProviderSettings providerSettings() { return ProviderSettings.builder() .issuer("https://example.com") // 设置发行者URL .build(); } protected void configure(HttpSecurity http) throws Exception { http.authorizeRequests(authorize -> authorize.anyRequest().authenticated()) .oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt); } } ``` 上述代码片段展示了如何通过自定义 `ProviderSettings` 来指定授权服务器的相关参数[^1]。 --- #### 2. 客户端 (Client Configuration) 要使应用程序作为OAuth2客户端运行,需添加以下依赖项: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-client</artifactId> </dependency> ``` 在 `application.yml` 或 `application.properties` 文件中提供客户端的具体配置信息: ```yaml spring: security: oauth2: client: registration: google: clientId: your-google-client-id clientSecret: your-google-client-secret redirectUri: "{baseUrl}/login/oauth2/code/{registrationId}" scope: - email - profile provider: google: authorization-uri: https://accounts.google.com/o/oauth2/v2/auth token-uri: https://www.googleapis.com/oauth2/v4/token user-info-uri: https://www.googleapis.com/oauth2/v3/userinfo jwk-set-uri: https://www.googleapis.com/oauth2/v3/certs ``` 此部分描述了如何注册外部身份提供商(如Google),以及如何获取用户的电子邮件和个人资料数据[^2]。 --- #### 3. 资源服务器 (Resource Server) 资源服务器负责验证传入请求中的访问令牌,并保护受控API免遭未经授权的访问。为此,应加入如下依赖: ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-oauth2-resource-server</artifactId> </dependency> ``` 接着,在安全配置文件中声明资源服务器的行为模式: ```java 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.web.SecurityFilterChain; @Configuration public class ResourceServerConfig { @Bean public SecurityFilterChain resourceServerSecurityFilterChain(HttpSecurity http) throws Exception { http.authorizeHttpRequests(authz -> authz .anyRequest().authenticated()) .oauth2ResourceServer(oauth2 -> oauth2.jwt()); return http.build(); } } ``` 这里说明的是如何利用JWT解析器对进入系统的每一个HTTP请求执行认证操作[^3]。 --- #### 总结 以上分别介绍了基于Spring Boot框架下构建OAuth2架构所需的三个核心模块——授权服务器、客户端和资源服务器的基础搭建方法及其相互协作方式。每一步都紧密关联着实际项目开发过程中的需求场景和技术选型考量因素。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值