Spring Boot项目中实现OAuth2客户端模式(Client Credentials Grant Type)

embedded/2024/9/23 1:36:45/

背景

在项目中难免需要和外部系统进行对接,既然对接那就需要进行鉴权认证,一般外围系统的对接交互方式协议分两种:https和内网;如果是https,有些场景也需要进一步进行接口层面的鉴权认证,虽然通道已经进行了保障了

OAuth2基础知识

在学习本篇OAuth2的客户端模式认证之前,大家需要先了解OAuth2几种认证模式,分别使用场景
【OAuth2】详细讲解

OAuth2场景的认证模式:验证码和客户端
springboot整合支付宝第三方授权登录代码实现详细教程
20220419_SpringBoot-OAuth2-Gitee第三方登录
上面是博主认为比较有学习价值的学习视频,讲的比较清楚,可以深入了解验证码认证的过程
本篇主要讲客户端模式,其实客户端模式不属于OAuth2的范畴,因为没有了用户,但是有客户端id,所以也纳入了OAuth2范畴

OAuth2客户端认证

在Spring Boot项目中实现OAuth2客户端模式(Client Credentials Grant Type)对第三方接口进行认证,通常涉及以下几个步骤:

设置OAuth2服务提供商:你需要有一个支持OAuth2的服务提供商,该服务提供商能够颁发访问令牌(Access Token)。如果你自己控制服务提供商,那么你需要设置好OAuth2服务器端。
客户端应用程序配置:在客户端应用程序中配置OAuth2客户端凭证,包括客户端ID (client_id) 和客户端密钥 (client_secret)。
获取访问令牌:使用客户端凭证向OAuth2授权服务器请求访问令牌。
使用访问令牌访问资源:获取到访问令牌之后,将其添加到请求头中,用于访问受保护的资源。

客户端服务搭建

下面是一个简单的示例,展示如何在Spring Boot项目中实现OAuth2客户端模式认证,并调用受保护的资源API。

添加依赖

首先,确保你的Spring Boot项目中包含了Spring Security OAuth2相关依赖:

<!-- 在pom.xml中添加 -->
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>

配置application.properties或application.yml

在application.properties或application.yml中配置OAuth2客户端信息:

# application.properties
spring.security.oauth2.client.provider.custom_oauth2.token-uri=http://your-oauth2-server.com/oauth/token
spring.security.oauth2.client.provider.custom_oauth2.authorization-uri=http://your-oauth2-server.com/oauth/authorize
spring.security.oauth2.client.provider.custom_oauth2.user-name-attribute=idspring.security.oauth2.client.registration.custom_oauth2.client-id=your_client_id
spring.security.oauth2.client.registration.custom_oauth2.client-secret=your_client_secret
spring.security.oauth2.client.registration.custom_oauth2.scope=read,write
spring.security.oauth2.client.registration.custom_oauth2.authorization-grant-type=client_credentials
spring.security.oauth2.client.registration.custom_oauth2.redirect-uri-template=https://your-app.com/login/oauth2/code/custom_oauth2

创建OAuth2客户端配置

你可以创建一个配置类来指定OAuth2客户端的细节

import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.SecurityFilterChain;
import org.springframework.security.web.server.SecurityWebFilterChain;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.annotation.web.reactive.EnableWebFluxSecurity;
import org.springframework.security.config.web.server.ServerHttpSecurity;
import org.springframework.security.web.server.SecurityWebFilterChain;@EnableWebFluxSecurity
public class WebSecurityConfig {@Beanpublic SecurityWebFilterChain securityWebFilterChain(ServerHttpSecurity http) {http.authorizeExchange().anyExchange().authenticated().and().oauth2Login(); // 启用OAuth2登录return http.build();}
}

调用受保护资源

一旦获取到了访问令牌,你可以在HTTP请求头中包含Authorization字段,值为Bearer {access_token},来访问受保护的资源:

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.client.RestTemplate;public class ResourceFetcher {public void fetchResource(String accessToken) {HttpHeaders headers = new HttpHeaders();headers.setBearerAuth(accessToken); // 设置Bearer TokenHttpEntity<String> entity = new HttpEntity<>("body", headers);RestTemplate restTemplate = new RestTemplate();ResponseEntity<String> response = restTemplate.exchange("http://protected-resource.com/api/resource",HttpMethod.GET,entity,String.class);

http://www.ppmy.cn/embedded/111417.html

相关文章

表格多列情况下,loading不显示问题

问题描述&#xff1a; 用element plus 做得表格&#xff0c;如下图&#xff0c;列数较多&#xff0c;且部分表格内容显示比较复杂&#xff0c;数据量中等的情况下&#xff0c;有一个switch 按钮&#xff0c;切换部分列的显示和隐藏&#xff0c;会发现&#xff0c;切换为显示的时…

Visual Studio配置opencv环境

&#xff08;1&#xff09;打开属性页面&#xff08;鼠标放在解决方案上&#xff0c;点击右键会有一个属性选项弹出&#xff09; &#xff08;2&#xff09;配置opencv的include和opencv2路径&#xff0c;具体路径和版本根据自己电脑配置 &#xff08;3&#xff09;配置opencv…

Go开源日志库Logrus的使用

一、Logrus简介 Logrus 是一个流行的 Go 语言日志库&#xff0c;以其功能强大、性能高效和高度灵活性而闻名。有关更多介绍可查看 Logrus。 主要特点 丰富的日志级别&#xff1a;Logrus 支持多种日志级别&#xff0c;包括 Debug、Info、Warn、Error、Fatal 和 Panic&#xf…

图文讲解HarmonyOS应用发布流程

HarmonyOS应用的开发和发布过程可以分为以下几个步骤&#xff1a;证书生成、应用开发、应用签名和发布。 1. 证书生成&#xff1a; 在开始开发HarmonyOS应用之前&#xff0c;首先需要生成一个开发者证书。开发者证书用于标识应用的开发者身份并确保应用的安全性。可以通过Har…

如何在@GenericGenerator中显式指定schema

现在的情况是&#xff0c;在MySQL中有db1和db2两个数据库。项目使用Hibernate&#xff0c;可同时访问db1和db2&#xff0c;默认数据库为db1。表table2在db2中。且table2的主键名为ids&#xff0c;是自增长字段&#xff08;Auto Increment&#xff09;。 table2和ids的定义为&a…

Python习题 179:用 pathlib 模块列出指定目录下的所有子目录

(编码题)请写一个 Python 函数,使用 pathlib 模块列出指定目录下的所有子目录,并统计它们的总数。 参考答案Python 代码如下from pathlib import Pathdef list_files_and_directories(directory_path):"""列出指定目录下的所有子目录,并统计它们的总数。参…

Spring-bean的生命周期-前篇

Spring bean生命周期12个环节 1.阶段1&#xff1a;Bean元信息配置阶段 2.阶段2&#xff1a;Bean元信息解析阶段 3.阶段3&#xff1a;将Bean注册到容器中 4.阶段4&#xff1a;BeanDefinition合并阶段 阶段5&#xff1a;Bean Class加载阶段 6.阶段6&#xff1a;Bean实例化阶…

笔试强训day11

游游的水果大礼包 #include <iostream> #define int long longusing namespace std; int n, m, a, b;signed main() {cin>>n>>m>>a>>b;int ret 0;for(int x 0; x < min(n / 2, m); x) // 枚举 1 号礼包的个数{int y min(n - x * 2, (m - …