项目练习:SpringSecurity+OAuth2接入gitee的第三方登陆(授权码模式)

ops/2025/2/7 13:29:59/

文章目录

  • 一、知识准备
    • 1、OAuth2的角色
    • 2、使用场景
    • 3、四种授权模式
  • 二、案例实现
    • 1、gitee上注册应用
    • 2、直接通过手动发送http请求方式
    • 3、项目代码方式
    • 4、测试方法

一、知识准备

1、OAuth2的角色

1、资源所有者(Resource 0wner):即用户,资源的拥有人,想要通过客户应用访问资源服务器上的资源。
2、客户应用(client):通常是一个web或者无线应用,它需要访问用户的受保护资源。
3、资源服务器(Resource Server):存储受保护资源的服务器或定义了可以访问到资源的API,接收并验证客户端的访问令牌,以决定是否授权访问资源。
4、授权服务器(Authorization Server):负责验证资源所有者的身份并向客户端颁发放访问令牌

2、使用场景

使用第三方账号登陆当前系统

3、四种授权模式

1、授权码(最常用)
2、隐藏式
3、密码式
4、客户端凭证(机器对接机器)

四种模式的选择
在这里插入图片描述

二、案例实现

1、gitee上注册应用

点击设置
在这里插入图片描述
左侧下滑,选择第三方应用
在这里插入图片描述
创建应用
在这里插入图片描述
填写应用信息
http://localhost:8088/login/oauth2/code/gitee
在这里插入图片描述
创建成功后,我们可以得到ClientId和ClientSecret
在这里插入图片描述

2、直接通过手动发送http请求方式

大致分为两步
1、获取token

此次请求,gitee会通过配置的应用回调地址,返回一个code给我们
需要再浏览器中发起请求
https://gitee.com/oauth/authorize?client_id={client_id}&redirect_uri={redirect_uri}&response_type=code带上第一次返回的code及其他相关参数,这次请求会返回token给我们
需要再postman中发请求
https://gitee.com/oauth/token?grant_type=authorization_code&code={code}&client_id={client_id}&redirect_uri={redirect_uri}&client_secret={client_secret}

2、通过token获取用户信息

带上第一步中获取的token作为参数
https://gitee.com/api/v5/user?access_token=672bdec6268716a4011e5f633979b444

3、项目代码方式

  • common模块
        <!-- spring security 安全认证 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-security</artifactId></dependency><!-- oauth2 客户端 --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-oauth2-client</artifactId></dependency><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-thymeleaf</artifactId></dependency>

security.oauth2.client中自带的几个重要的原生类
OAuth2ClientProperties、CommonOAuth2Provider

  • admin模块的application.yml配置gitee信息
# Spring配置
spring:security:oauth2:client:registration:gitee:provider: giteeclient-id: 填写你自己的clientIdclient-secret: 填写自己的ClientSecret# 重定向的url地址,这个地址为默认的,改成自己项目的IP和PORT即可。redirect-uri: http://localhost:8088/login/oauth2/code/giteeauthorization-grant-type: "authorization_code"client-name: gitee#不配置这个会报 gitee找不到provider:gitee:authorization-uri: https://gitee.com/oauth/authorizetoken-uri: https://gitee.com/oauth/tokenuser-info-uri: https://gitee.com/api/v5/useruser-name-attribute: "name"
  • framework的SecurityConfig
http.authorizeHttpRequests(auth -> auth.anyRequest().authenticated());
http.oauth2Client(Customizer.withDefaults());
  • admin模块
    新增controller
@Controller
public class OAuth2LoginController {@GetMapping("/")public String index(Model model, @RegisteredOAuth2AuthorizedClient OAuth2AuthorizedClient authorizedClient,@AuthenticationPrincipal OAuth2User oauth2User) {model.addAttribute("userName", oauth2User.getName());model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName());model.addAttribute("userAttributes", oauth2User.getAttributes());return "index";}@GetMapping("/test")public String test(){return "test";		//返回test.html页面}}

新增test.html页面

<!DOCTYPE html>
<html lang="en">
<head><meta charset="UTF-8"><title>测试页</title>
</head>
<body>
<h1>这是一个测试页面!</h1>
</body>
</html>

新增index.html页面

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="https://www.thymeleaf.org" xmlns:sec="https://www.thymeleaf.org/thymeleaf-extras-springsecurity5">
<head><title>Spring Security - OAuth 2.0 Login</title><meta charset="utf-8" />
</head>
<body>
<div style="float: right" th:fragment="logout" sec:authorize="isAuthenticated()"><div style="float:left"><span style="font-weight:bold">User: </span><span sec:authentication="name"></span></div><div style="float:none">&nbsp;</div><div style="float:right"><form action="#" th:action="@{/logout}" method="post"><input type="submit" value="Logout" /></form></div>
</div>
<h1>OAuth 2.0 Login with Spring Security</h1>
<div>You are successfully logged in <span style="font-weight:bold" th:text="${userName}"></span>via the OAuth 2.0 Client <span style="font-weight:bold" th:text="${clientName}"></span>
</div>
<div>&nbsp;</div>
<div><span style="font-weight:bold">User Attributes:</span><ul><li th:each="userAttribute : ${userAttributes}"><span style="font-weight:bold" th:text="${userAttribute.key}"></span>: <span th:text="${userAttribute.value}"></span></li></ul>
</div>
</body>
</html>

4、测试方法

浏览器访问:http://localhost:8088/login
在这里插入图片描述
成功拿到gitee的用户信息


http://www.ppmy.cn/ops/156442.html

相关文章

Spring Boot Actuator与JMX集成实战

在微服务架构中&#xff0c;监控和管理应用的运行状态是至关重要的。Spring Boot Actuator 提供了一种便捷的方式来监控和管理 Spring Boot 应用&#xff0c;而 JMX&#xff08;Java Management Extensions&#xff09;则是一种用于管理 Java 应用的标准技术。本文将通过一个实…

大语言模型极速部署:Ollama 、 One-API、OpenWebUi 完美搭建教程

大语言模型极速部署&#xff1a;Ollama 、 One-API、OpenWebUi 完美搭建教程 本文将介绍如何通过命令行工具部署 Ollama 和 One-API 以及 OpenWebUi&#xff0c;帮助你快速搭建私有化大模型。 Ollama 是一个容器化工具&#xff0c;简化了大语言模型的管理与运行&#xff0c;支持…

TongSearch3.0.4.0安装和使用指引(by lqw)

文章目录 安装准备手册说明支持的数据类型安装控制台安装单节点(如需集群请跳过这一节)解压和启动开启X-Pack Security和生成p12证书&#xff08;之后配置内置密码和ssl要用到&#xff09;配置内置用户密码配置ssl&#xff08;先配置内置用户密码再配ssl&#xff09;配置控制台…

linux环境自动化golang项目启动脚本解析

一.场景介绍 当在本地创建了golang项目,修改了代码功能,怎么在远程测试服务器上更新该功能呢,可以使用下面的步骤来解决该问题(这只是其中一种方法): (1).推送最新代码到远程仓库 (2).在测试服务器上创建该项目并拉取最新代码 (3).创建deploy.sh脚本 (4).运行deploy.sh脚本 二.…

deepseek接入pycharm 进行AI编程

要将DeepSeek接入PyCharm进行AI编程,可以按照以下步骤操作: ### 1. 获取DeepSeek API访问权限 DeepSeek通常以API的形式对外提供服务,你需要在其官方网站注册账号,申请API访问权限。在申请通过后,会获得API密钥(API Key),这是后续调用API的关键凭证。 ### 2. 安装必要…

游戏引擎 Unity - Unity 下载与安装

Unity Unity 首次发布于 2005 年&#xff0c;属于 Unity Technologies Unity 使用的开发技术有&#xff1a;C# Unity 的适用平台&#xff1a;PC、主机、移动设备、VR / AR、Web 等 Unity 的适用领域&#xff1a;开发中等画质中小型项目 Unity 适合初学者或需要快速上手的开…

visual studio安装

一、下载Visual Studio 访问Visual Studio官方网站。下载 Visual Studio Tools - 免费安装 Windows、Mac、Linux 在主页上找到并点击“下载 Visual Studio”按钮。 选择适合需求的版本&#xff0c;例如“Visual Studio Community”&#xff08;免费版本&#xff09;&#x…

通过docker安装部署deepseek以及python实现

前提条件 Docker 安装:确保你的系统已经安装并正确配置了 Docker。可以通过运行 docker --version 来验证 Docker 是否安装成功。 网络环境:保证设备有稳定的网络连接,以便拉取 Docker 镜像和模型文件。 步骤一:拉取 Ollama Docker 镜像 Ollama 可以帮助我们更方便地管理…