SpringBoot集成OAuth2.0的四种授权方式

2023-11-17

背景

OAuth(开放授权)是一个开放标准,允许用户授权第三方应用访问他们存储在另外的服务提供者上的信息,而不需要将用户名和密码提供给第三方应用或分享他们数据的所有内容。 OAuth2.0 是OAuth协议的延续版本,但不向后兼容 OAuth 1.0 ,即完全废止了 OAuth1.0 。很多大公司,国外的如Google,Netflix,Microsoft等,国内的像ByteDance,Alibaba,Tencent等都提供了OAuth认证服务(开放平台),这些都足以说明OAuth标准逐渐成为开放资源授权的标准。

该代码仓库主要结合 SpringBootSpringSecurityOAuth2.0 等技术实现了 OAuth2.0 的四种授权方式的Demo,内容如下:

四种授权方式

模式 名称 是否支持refresh_token 是否需要结合浏览器测试
authorization_code 授权码模式 支持
implicit 简化模式 不支持
password 密码模式 支持
client_credentials 客户端模式 不支持

authorization_code 授权码模式 需结合浏览器测试

GET http://localhost:9000/oauth/authorize?client_id=client&response_type=code

1.获取授权码,会回调至目标地址

http://localhost:9000/oauth/authorize?client_id=client&response_type=code

2.认证之后,点击允许,获取授到权码3.通过授权码向授权服务获取令牌:access_tokenhttp://client:secret@localhost:9000/oauth/token

将上一步获取的授权码作为x-www-form-urlencoded的一个参数:

grant_type:authorization_code code:wgs8XF

获取到access_token

{"access_token": "d21a37e3-d423-4419-bb41-5712e23aee6b","token_type": "bearer","expires_in": 43200,"scope": "app"
} 

4.后续对资源服务的请求,附带access_token即可

http://localhost:8000/private/hi?access_token=d69bc334-3d5c-4c86-972c-11826f44c4af

源码在master分支:github.com/heartsuit/d…

implicit 简化模式 不支持refresh token 需结合浏览器测试

GET http://localhost:9000/oauth/authorize?grant_type=implicit&response_type=token&scope=read&client_id=client0&client_secret=secret0

源码在implicit分支:github.com/heartsuit/d…

password 密码模式

POST http://localhost:9000/oauth/token?username=admin&password=123456&grant_type=password&scope=read&client_id=client1&client_secret=secret1

源码在password分支:github.com/heartsuit/d…

client_credentials 客户端模式 不支持refresh token

POST http://localhost:9000/oauth/token?grant_type=client_credentials&scope=read&client_id=client2&client_secret=secret2

源码在client分支:github.com/heartsuit/d…

四种方式的授权流程

以下流程来自:tools.ietf.org/html/rfc674…

authorization_code 授权码模式

 +----------+
 | Resource |
 | Owner|
 ||
 +----------+^| (B)
 +----|-----+Client Identifier+---------------+
 | -+----(A)-- & Redirection URI ---->| |
 |User- | | Authorization |
 |Agent-+----(B)-- User authenticates --->| Server|
 || | |
 | -+----(C)-- Authorization Code ---<| |
 +-|----|---+ +---------------+ || ^v(A)(C)|| || || ^v ||
 +---------+||
 | |>---(D)-- Authorization Code ---------'|
 |Client |& Redirection URI|
 | | |
 | |<---(E)----- Access Token -------------------'
 +---------+ (w/ Optional Refresh Token) 

Note: The lines illustrating steps (A), (B), and © are broken into two parts as they pass through the user-agent.

 Figure 3: Authorization Code Flow 

The flow illustrated in Figure 3 includes the following steps:

(A) The client initiates the flow by directing the resource owner’s user-agent to the authorization endpoint. The client includes its client identifier, requested scope, local state, and a redirection URI to which the authorization server will send the user-agent back once access is granted (or denied).

(B) The authorization server authenticates the resource owner (via the user-agent) and establishes whether the resource owner grants or denies the client’s access request.

© Assuming the resource owner grants access, the authorization server redirects the user-agent back to the client using the redirection URI provided earlier (in the request or during client registration). The redirection URI includes an authorization code and any local state provided by the client earlier.

(D) The client requests an access token from the authorization server’s token endpoint by including the authorization code received in the previous step. When making the request, the client authenticates with the authorization server. The client includes the redirection URI used to obtain the authorization code for verification.

(E) The authorization server authenticates the client, validates the authorization code, and ensures that the redirection URI received matches the URI used to redirect the client in step ©. If valid, the authorization server responds back with an access token and, optionally, a refresh token.

implicit 简化模式

 +----------+
 | Resource |
 |Owner |
 ||
 +----------+^| (B)
 +----|-----+Client Identifier +---------------+
 | -+----(A)-- & Redirection URI --->| |
 |User- || Authorization |
 |Agent-|----(B)-- User authenticates -->| Server|
 ||| |
 ||<---(C)--- Redirection URI ----<| |
 ||with Access Token +---------------+
 ||in Fragment
 ||+---------------+
 ||----(D)--- Redirection URI ---->| Web-Hosted|
 ||without Fragment| Client|
 |||Resource |
 | (F)|<---(E)------- Script ---------<| |
 ||+---------------+
 +-|--------+ ||(A)(G) Access Token || ^v
 +---------+
 | |
 |Client |
 | |
 +---------+ 

Note: The lines illustrating steps (A) and (B) are broken into two parts as they pass through the user-agent.

 Figure 4: Implicit Grant Flow 

The flow illustrated in Figure 4 includes the following steps:

(A) The client initiates the flow by directing the resource owner’s user-agent to the authorization endpoint. The client includes its client identifier, requested scope, local state, and a redirection URI to which the authorization server will send the user-agent back once access is granted (or denied).

(B) The authorization server authenticates the resource owner (via the user-agent) and establishes whether the resource owner grants or denies the client’s access request.

© Assuming the resource owner grants access, the authorization server redirects the user-agent back to the client using the redirection URI provided earlier. The redirection URI includes the access token in the URI fragment.

(D) The user-agent follows the redirection instructions by making a request to the web-hosted client resource (which does not include the fragment per [RFC2616]). The user-agent retains the fragment information locally.

(E) The web-hosted client resource returns a web page (typically an HTML document with an embedded script) capable of accessing the full redirection URI including the fragment retained by the user-agent, and extracting the access token (and other parameters) contained in the fragment.

(F) The user-agent executes the script provided by the web-hosted client resource locally, which extracts the access token.

(G) The user-agent passes the access token to the client.

password 密码模式

 +----------+
 | Resource |
 |Owner |
 ||
 +----------+v|Resource Owner (A) Password Credentials|v
 +---------++---------------+
 | |>--(B)---- Resource Owner ------->| |
 | | Password Credentials | Authorization |
 | Client|| Server|
 | |<--(C)---- Access Token ---------<| |
 | |(w/ Optional Refresh Token) | |
 +---------++---------------+Figure 5: Resource Owner Password Credentials Flow 

The flow illustrated in Figure 5 includes the following steps:

(A) The resource owner provides the client with its username and password.

(B) The client requests an access token from the authorization server’s token endpoint by including the credentials received from the resource owner. When making the request, the client authenticates with the authorization server.

© The authorization server authenticates the client and validates the resource owner credentials, and if valid, issues an access token.

client_credentials 客户端模式

 +---------++---------------+
 | || |
 | |>--(A)- Client Authentication --->| Authorization |
 | Client|| Server|
 | |<--(B)---- Access Token ---------<| |
 | || |
 +---------++---------------+ Figure 6: Client Credentials Flow 

The flow illustrated in Figure 6 includes the following steps:

(A) The client authenticates with the authorization server and requests an access token from the token endpoint.

(B) The authorization server authenticates the client, and if valid, issues an access token.

Reference


If you have any questions or any bugs are found, please feel free to contact me.

Your comments and suggestions are welcome!

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

SpringBoot集成OAuth2.0的四种授权方式 的相关文章

  • 如何让 BlazeDS 忽略属性?

    我有一个 java 类 它有一个带有 getter 和 setter 的字段 以及第二对 getter 和 setter 它们以另一种方式访问 该字段 public class NullAbleId private static final
  • 不同帐户上的 Spring Boot、JmsListener 和 SQS 队列

    我正在尝试开发一个 Spring Boot 1 5 应用程序 该应用程序需要侦听来自两个不同 AWS 帐户的 SQS 队列 是否可以使用 JmsListener 注解创建监听器 我已检查权限是否正确 我可以使用 getQueueUrl 获取
  • Junit:如何测试从属性文件读取属性的方法

    嗨 我有课ReadProperty其中有一个方法ReadPropertyFile返回类型的Myclass从属性文件读取参数值并返回Myclass目的 我需要帮助来测试ReadPropertyFile方法与JUnit 如果可能的话使用模拟文件
  • 在内存中使用 byte[] 创建 zip 文件。 Zip 文件总是损坏

    我创建的 zip 文件有问题 我正在使用 Java 7 我尝试从字节数组创建一个 zip 文件 其中包含两个或多个 Excel 文件 应用程序始终完成 没有任何异常 所以 我以为一切都好 当我尝试打开 zip 文件后 Windows 7 出
  • 使用 LinkedList 实现下一个和上一个按钮

    这可能是一个愚蠢的问题 但我很难思考清楚 我编写了一个使用 LinkedList 来移动加载的 MIDI 乐器的方法 我想制作一个下一个和一个上一个按钮 以便每次单击该按钮时都会遍历 LinkedList 如果我硬编码itr next or
  • 动态选择端口号?

    在 Java 中 我需要获取端口号以在同一程序的多个实例之间进行通信 现在 我可以简单地选择一些固定的数字并使用它 但我想知道是否有一种方法可以动态选择端口号 这样我就不必打扰我的用户设置端口号 这是我的一个想法 其工作原理如下 有一个固定
  • 如何使用assertEquals 和 Epsilon 在 JUnit 中断言两个双精度数?

    不推荐使用双打的assertEquals 我发现应该使用带有Epsilon的形式 这是因为双打不可能100 严格 但无论如何我需要比较两个双打 预期结果和实际结果 但我不知道该怎么做 目前我的测试如下 Test public void te
  • HSQL - 识别打开连接的数量

    我正在使用嵌入式 HSQL 数据库服务器 有什么方法可以识别活动打开连接的数量吗 Yes SELECT COUNT FROM INFORMATION SCHEMA SYSTEM SESSIONS
  • 如何获取之前的URL?

    我需要调用我的网络应用程序的 URL 例如 如果有一个从 stackoverflow com 到我的网站 foo com 的链接 我需要 Web 应用程序 托管 bean 中的 stackoverflow 链接 感谢所有帮助 谢谢 并不总是
  • 在接口中使用默认方法是否违反接口隔离原则?

    我正在学习 SOLID 原则 ISP 指出 客户端不应被迫依赖于他们所使用的接口 不使用 在接口中使用默认方法是否违反了这个原则 我见过类似的问题 但我在这里发布了一个示例 以便更清楚地了解我的示例是否违反了 ISP 假设我有这个例子 pu
  • java.lang.IllegalStateException:提交响应后无法调用 sendRedirect()

    这两天我一直在尝试找出问题所在 我在这里读到我应该在代码中添加一个返回 我做到了 但我仍然得到 java lang IllegalStateException Cannot call sendRedirect after the respo
  • 帮助将图像从 Servlet 获取到 JSP 页面 [重复]

    这个问题在这里已经有答案了 我目前必须生成一个显示字符串文本的图像 我需要在 Servlet 上制作此图像 然后以某种方式将图像传递到 JSP 页面 以便它可以显示它 我试图避免保存图像 而是以某种方式将图像流式传输到 JSP 自从我开始寻
  • 像 Java 这样的静态类型语言中动态方法解析背后的原因是什么

    我对 Java 中引用变量的动态 静态类型和动态方法解析的概念有点困惑 考虑 public class Types Override public boolean equals Object obj System out println i
  • 内部类的构造函数引用在运行时失败并出现VerifyError

    我正在使用 lambda 为内部类构造函数创建供应商ctx gt new SpectatorSwitcher ctx IntelliJ建议我将其更改为SpectatorSwitcher new反而 SpectatorSwitcher 是我正
  • Java ResultSet 如何检查是否有结果

    结果集 http java sun com j2se 1 4 2 docs api java sql ResultSet html没有 hasNext 方法 我想检查 resultSet 是否有任何值 这是正确的方法吗 if resultS
  • 如何访问JAR文件中的Maven资源? [复制]

    这个问题在这里已经有答案了 我有一个使用 Maven 构建的 Java 应用程序 我有一个资源文件夹com pkg resources 我需要从中访问文件 例如directory txt 我一直在查看各种教程和其他答案 但似乎没有一个对我有
  • 如何使用 jUnit 将测试用例添加到套件中?

    我有 2 个测试类 都扩展了TestCase 每个类都包含一堆针对我的程序运行的单独测试 如何将这两个类 以及它们拥有的所有测试 作为同一套件的一部分执行 我正在使用 jUnit 4 8 在 jUnit4 中你有这样的东西 RunWith
  • 使用反射覆盖最终静态字段是否有限制?

    在我的一些单元测试中 我在最终静态字段上的反射中遇到了奇怪的行为 下面是说明我的问题的示例 我有一个基本的 Singleton 类 其中包含一个 Integer public class BasicHolder private static
  • 在java中为组合框分配键

    我想添加一个JComboBox在 Swing 中这很简单 但我想为组合中的每个项目分配值 我有以下代码 JComboBox jc1 new JComboBox jc1 addItem a jc1 addItem b jc1 addItem
  • 如果没有抽象成员,基类是否应该标记为抽象?

    如果一个类没有抽象成员 可以将其标记为抽象吗 即使没有实际理由直接实例化它 除了单元测试 是的 将不应该实例化的基类显式标记为抽象是合理且有益的 即使在没有抽象方法的情况下也是如此 它强制执行通用准则来使非叶类抽象 它阻止其他程序员创建该类

随机推荐

  • Python学习笔记综合

    一 安装和学习建议 1 使用的2 7 2 环境变量 python的根目录 3 cmd就可以运行 执行使用 python xxx py 4 编写代码可以直接python进入代码编辑 5 exit 退出编辑环境 安装与学习建议 pycharm专
  • 图像生成王者不是GAN?扩散模型最近有点火:靠加入类别条件,效果直达SOTA

    博雯 发自 凹非寺量子位 报道 公众号 QbitAI OpenAI刚刚推出的年末新作GLIDE 又让扩散模型小火了一把 这个基于扩散模型的文本图像生成大模型参数规模更小 但生成的图像质量却更高 于是 依旧是OpenAI出品 论文标题就直接号
  • 【RPA经验分享】远程桌面最小化或关闭状态运行 RPA

    了解RPA www i search com cn 学习RPA https support i search com cn 一 问题描述 当我们使用 window 自带的远程工具连接远程服务器并运行自动化流程 最小化远程窗口或关闭窗口后自动
  • 任意输入一长度不超过30的字符串,使用指针编写函数,实现如下功能:将一个字符串str1中的所有小写字母复制成为一个新的字符串str2。

    任意输入一长度不超过30的字符串 使用指针编写函数 实现如下功能 将一个字符串str1中的所有小写字母复制成为一个新的字符串str2 例如 若str1为 progRam Cczuoye 19 则str2应为 progamczuoye 要求
  • JMeter 批量接口测试

    一 背景 最近在进行某中台的接口测试准备 发现接口数量非常多 有6 70个 而且每个接口都有大量的参数并且需要进行各种参数验证来测试接口是否能够正确返回响应值 想了几种方案后 决定尝试使用JMeter的csv读取来实现批量的接口测试 接口测
  • 雷辉:让视频会议conferencing like TV

    伴随视频会议技术不断成熟 其功能已不局限于早期仅仅满足异地会议的需求 打破硬件的桎梏 提供白板 多媒体播放 文档协同等更多功能 如何为视频会议赋予更强大功能 实现更好体验 满足更多办公需求成为一个新的课题 LiveVideoStack邀请到
  • 测试人员必备:常用自动化测试工具

    Appium 官网 http appium io AppUI自动化测试 Appium 是一个移动端自动化测试开源工具 支持iOS 和Android 平台 支持Python Java 等语言 即同一套Java 或Python 脚本可以同时运行
  • 并发编程系列之CountDownLatch对战Cyclicbarrier

    前言 前面我们介绍了并发容器和队列 今天我们来介绍几个非常有用的并发工具类 今天主要讲CountDownLatch和Cyclicbarrier这两个工具类 通过讲解并对比两个类的区别 OK 让我们开始今天的并发之旅吧 什么是CountDow
  • Python爬虫学习-第四篇 Scrapy框架抓取唯品会数据

    上篇博文讲述了scrapy的框架和组件 对于scrapy有了基本的了解 那么我们进入今天的正题 使用Scrapy框架爬取数据 1 创建Scrapy项目 创建Scrapy工程文件的命令 scrapy startproject scrapyte
  • java实现飞机大战(简单版)

    import javafx animation AnimationTimer import javafx application Application import javafx scene Group import javafx sce
  • java压缩字符串并生成二维码

    针对特殊需求需要使用二维码传输数据 为了降低二维码的复杂度和提高数据传输量 需要先对数据进行压缩 然后生成二维码 压缩后的数据是byte 如果再转回字符串会严重影响压缩效果 因此考虑直接使用byte 生成和解析二维码 为了实现使用byte
  • 解决 “/lib64/libc.so.6: version `GLIBC_2.18‘ not found (required by /lib64/libstdc++.so.6)“

    https blog csdn net wiborgite article details 87707938
  • 基于RedHat 8.2源码编译升级Kernel 5.8.1

    1 基于RedHat 8 2源码编译升级Kernel 5 8 1 1 1 背景 只是单纯为了验证工作中遇到的一个bug 才诞生了此文 1 2 先从如下网址下载内核源码包 我这里下载的是 linux 5 8 1 tar gz 大家可根据自己需
  • 无监督深度估计、运动估计的深度学习方法(二)——SSIM损失函数

    在自监督深度估计中 一般输入2张图像 若为视频 则输入邻近的两帧图像 frame1和frame2 模型先估计相机拍摄这2张图像是的姿态变化pose 然后根据pose将frame1变换到frame2的视角下 得到合成图像synthetic f
  • 数据类型:C++中的基本数据类型

    数据类型 C 中的基本数据类型 在C 中 数据类型是用于存储不同种类数据的变量类型 C 提供了多种基本数据类型 包括字符型和数值类型 本篇博客将详细介绍C 中的基本数据类型 并提供相应的示例代码 目录 引言 字符型数据类型 2 1 char
  • Mysql数据备份-定时自动备份dump备份命令

    登录数据库服务器并打开命令行工具 如Windows系统中的cmd exe 或图形化管理工具 如phpMyAdmin Navicat等 根据数据库类型 执行相应的备份命令 例如 MySQL数据库可以使用以下命令导出备份 mysqldump u
  • 数仓分层理论

    数据仓库 在实际工作中 数仓分层 元数据管理 数据质量管理一直是一个持续优化的过程 我们公司业务也是在持续的做数仓的优化工作 在数据治理这方面还是欠缺很多的经验的 下面先简单整理了一下第一个理论部分的相关笔记 数据仓库理论 数据仓库四大特征
  • 锐捷ap设置为路由模式_路由器AP、路由、桥接模式有什么区别【详细介绍】

    现在的路由大多数已经开始支持多种网络连接模式 那么我们就挑一款模式最全的路由来了解各种模式的区别吧 下文将以TP Link迷你无线路由器为例 在TP Link迷你无线路由器上一般有AP 接入点 模式 Router 无线路由 模式 Repea
  • stm32——EXTI

    EXTI 外部中断 是stm32的众多外设之一 属于中断的一种 它最重要的就是通过检测外部引脚口的电平变化 比如说上升沿 下降沿 以及双边沿 来触发中断 让主程序放下当前的事情 去执行发生中断时应该执行的事情 设置好的函数 大概是如上所属
  • SpringBoot集成OAuth2.0的四种授权方式

    背景 OAuth 开放授权 是一个开放标准 允许用户授权第三方应用访问他们存储在另外的服务提供者上的信息 而不需要将用户名和密码提供给第三方应用或分享他们数据的所有内容 OAuth2 0 是OAuth协议的延续版本 但不向后兼容 OAuth