SpringBoot集成OAuth2.0有新方案了

本文概述了SpringBoot 2.0弃用SpringSecurityOAuth后的OAuth2.0迁移策略,包括Client、ResourceServer和AuthorizationServer的迁移步骤,以及如何在新版本中使用相关依赖。还介绍了兼容旧版方案的方法和不同Java版本的依赖要求。

背景说明

SpringBoot 2.X已经抛弃了对Spring Security OAuth模块,而是通过Spring Security 5 支持了OAuth 2.0、客户端、资源服务器、授权服务器。

由此产生的重大变动,官方也针对常见问题进行了解答。原文链接:https://github.com/spring-projects/spring-security/wiki/OAuth-2.0-Features-Matrix#frequently-asked-questions

OAuth 2.0 三大主要模块

  • 客户端 Client
  • 资源服务 Resource Server
  • 授权服务 Authorization Server
    在这里插入图片描述

Client与 Resource Server 迁移

如何将Client与Resource Server 迁移至Spring Security 5.2.X也提供了官方文档https://github.com/spring-projects/spring-security/wiki/OAuth-2.0-Migration-Guide

Authorization Server迁移

迁移Authorization Server可以参考如下依赖

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <version>2.5.4</version>
</dependency>
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <version>2.5.4</version>
</dependency>
<dependency>
    <groupId>org.springframework.security</groupId>
    <artifactId>spring-security-oauth2-authorization-server</artifactId>
    <version>0.2.0</version>
</dependency>

兼容历史方案

如仍需在Spring Boot 2.X项目中继续使用Spring Security OAuth可以通过如下依赖加入:

<dependency>
	<groupId>org.springframework.security.oauth.boot</groupId>
	<artifactId>spring-security-oauth2-autoconfigure</artifactId>
	<!-- <version>2.1.3.RELEASE</version> -->
</dependency>

Java版本依赖说明

0.3.0 之前版本依赖Java8
0.3.0 最低依赖java11
0.3.1 版本将降低Java版本至Java8

参考文档

集成 Spring Security OAuth Authorization Server
历史方案集成参考 spring-security-oauth2-jws-jwk
SpringBoot 2.4.6版本尚未支持说明 Springboot-features-security-authorization-server

Spring Boot集成OAuth 2.0实现第三方登录,可利用Spring Security的OAuth2.0模块来实现授权验证,以下是具体步骤: 1. **搭建授权服务器和资源服务器**:完成springboot + spring security + Oauth2.0的授权服务器和资源服务器的搭建工作,后续还需对Oauth2.0的重要结构文件进行详细配置,并处理授权错误,以实现系统登录的闭环测试[^2]。 2. **选择授权方式**:OAuth 2.0有四种授权方式,可根据需求选择合适的方式。例如授权码模式,源码在master分支(https://github.com/heartsuit/demo-springboot-oauth2/tree/master/in - memory );简化模式,源码在implicit分支(https://github.com/heartsuit/demo-springboot-oauth2/tree/implicit/in - memory );密码模式,源码在password分支(https://github.com/heartsuit/demo-springboot-oauth2/tree/password/in - memory );客户端模式,源码在client分支(https://github.com/heartsuit/demo-springboot-oauth2/tree/client/in - memory )。不同模式对于是否支持refresh_token和是否需要结合浏览器测试有不同要求,如授权码模式支持refresh_token且需结合浏览器测试,简化模式不支持refresh_token但需结合浏览器测试,密码模式支持refresh_token但无需结合浏览器测试,客户端模式不支持refresh_token且无需结合浏览器测试[^3]。 3. **数据库存储配置**:可以将OAuth 2.0客户端信息存储在MySQL数据库,access_token可存储在内存、MySQL、Redis或JWT中。相关源码路径分别为:客户端信息存储在MySQL数据库,access_token存储在内存、MySQL、Redis(https://github.com/heartsuit/demo-springboot-oauth2/tree/master/in - db );客户端信息存储在MySQL数据库,access_token存储在JWT(https://github.com/heartsuit/demo-springboot-oauth2/tree/master/in - db - jwt/springboot-oauth2-authorization-server )[^3]。 以下为一个简单的示例代码框架(仅作示意,实际使用需根据具体情况调整): ```java // 配置Spring Security和OAuth 2.0 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.config.annotation.web.configuration.EnableWebSecurity; import org.springframework.security.web.SecurityFilterChain; @Configuration @EnableWebSecurity public class SecurityConfig { @Bean public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception { http .authorizeRequests() .anyRequest().authenticated() .and() .oauth2Login(); return http.build(); } } ```
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值