最新Spring Security实战教程(十五)快速集成 GitHub 与 Gitee 的社交登录

最新Spring Security实战教程(十五)快速集成 GitHub 与 Gitee 的社交登录
在这里插入图片描述
🌷 古之立大事者,不惟有超世之才,亦必有坚忍不拔之志
🎐 个人CSND主页——Micro麦可乐的博客
🐥《Docker实操教程》专栏以最新的Centos版本为基础进行Docker实操教程,入门到实战
🌺《RabbitMQ》专栏19年编写主要介绍使用JAVA开发RabbitMQ的系列教程,从基础知识到项目实战
🌸《设计模式》专栏以实际的生活场景为案例进行讲解,让大家对设计模式有一个更清晰的理解
🌛《开源项目》本专栏主要介绍目前热门的开源项目,带大家快速了解并轻松上手使用
✨《开发技巧》本专栏包含了各种系统的设计原理以及注意事项,并分享一些日常开发的功能小技巧
💕《Jenkins实战》专栏主要介绍Jenkins+Docker的实战教程,让你快速掌握项目CI/CD,是2024年最新的实战教程
🌞《Spring Boot》专栏主要介绍我们日常工作项目中经常应用到的功能以及技巧,代码样例完整
🌞《Spring Security》专栏中我们将逐步深入Spring Security的各个技术细节,带你从入门到精通,全面掌握这一安全技术
如果文章能够给大家带来一定的帮助!欢迎关注、评论互动~

最新Spring Security实战教程(十五)快速集成 GitHub 与 Gitee 的社交登录

回顾链接:
最新Spring Security实战教程(一)初识Spring Security安全框架
最新Spring Security实战教程(二)表单登录定制到处理逻辑的深度改造
最新Spring Security实战教程(三)Spring Security 的底层原理解析
最新Spring Security实战教程(四)基于内存的用户认证
最新Spring Security实战教程(五)基于数据库的动态用户认证传统RBAC角色模型实战开发
最新Spring Security实战教程(六)最新Spring Security实战教程(六)基于数据库的ABAC属性权限模型实战开发
最新Spring Security实战教程(七)方法级安全控制@PreAuthorize注解的灵活运用
最新Spring Security实战教程(八)Remember-Me实现原理 - 持久化令牌与安全存储方案
最新Spring Security实战教程(九)前后端分离认证实战 - JWT+SpringSecurity无缝整合
最新Spring Security实战教程(十)权限表达式进阶 - 在SpEL在安全控制中的高阶魔法
最新Spring Security实战教程(十一)CSRF攻防实战 - 从原理到防护的最佳实践
最新Spring Security实战教程(十二)CORS安全配置 - 跨域请求的安全边界设定
最新Spring Security实战教程(十三)会话管理机制 - 并发控制与会话固定攻击防护
最新Spring Security实战教程(十四)OAuth2.0精讲 - 四种授权模式与资源服务器搭建

专栏更新完毕后,博主将会上传所有章节代码到ZEEKLOG资源免费给大家下载,如你不想等后续章节代码需提前获取,可以私信或留言!

1. 前言

在微服务与前后端分离架构中,第三方社交登录已成为提升用户体验的重要功能。社交登录可以有效降低用户注册成本,同时利用第三方平台的账号体系,实现快速认证与信息获取。Spring Security 6 作为 Java 生态中的安全框架,通过 OAuth2 协议简化了第三方认证的集成流程。

本章节博主将通过完整代码案例,讲解如何基于 Spring Security 6 实现 GitHubGitee 的社交登录功能。


2. 原理分析

回顾一下我们上一个章节,OAuth 2.0 授权码流程 【最新Spring Security实战教程(十四)OAuth2.0精讲 - 四种授权模式与资源服务器搭建】

在这里插入图片描述

其主要包括:

  • 跳转授权:客户端(我们的网站)将用户重定向至第三方授权服务器的授权端点,携带 client_idredirect_uriscope 等参数
  • 用户登录并同意:用户在第三方平台完成登录后,同意授权给客户端指定权限
  • 回调获取授权码:授权服务器重定向回客户端注册的 redirect_uri,并在查询参数中附带 code
  • 交换令牌:客户端后端使用 codeclient_secret 等向授权服务器的令牌端点发起 POST 请求,获取 access_token
  • 获取用户信息:客户端携带 access_token 调用用户信息端点,解析并登录或注册用户

3. 开发准备

3.1 初始化项目

还是继续基于我们之前的项目构建子项目 social-login-spring-security, pom文件引入项目依赖

<dependencies><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-oauth2-client</artifactId></dependency></dependencies>

GitHub/Gitee 开发者账号:用于注册 OAuth 应用。

3.2 注册 GitHub OAuth 应用

访问 GitHub 设置 → Developer settingsOAuth AppsNew OAuth App,填写应用名、主页 URL、授权回调 URL

在这里插入图片描述


填写应用信息:默认的重定向URI模板为{baseUrl}/login/oauth2/code/{registrationId}。registrationId是ClientRegistration的唯一标识符。

如Github固定写法则为:{baseUrl}//login/oauth2/code/github

在这里插入图片描述


获取应用程序id,生成应用程序密钥:保存好生成的 Client ID 与 Client Secret

在这里插入图片描述

3.3 注册 Gitee OAuth 应用

登陆Gitee 访问 Gitee应用管理,创建新应用

在这里插入图片描述


提交创建应用会得到 Client IDClient Secret


4. 实战案例

完成了上述的准备,我们开始来对我们的项目进行配置

4.1 配置 application.yml

#最新Spring Security实战教程(十五)快速集成 GitHub 与 Gitee 的社交登录spring:application:name: social-login-spring-security security:oauth2:client:registration:github:client-id: YOUR_CLIENT_ID client-secret: YOUR_CLIENT_SECRET scope: user:email redirect-uri:"{baseUrl}/login/oauth2/code/{registrationId}"authorization-grant-type: authorization_code client-name: github gitee:client-id: YOUR_CLIENT_ID client-secret: YOUR_CLIENT_SECRET scope: user_info client-name: Gitee authorization-grant-type: authorization_code redirect-uri:"{baseUrl}/login/oauth2/code/{registrationId}"provider: gitee provider:gitee:authorization-uri: https://gitee.com/oauth/authorize token-uri: https://gitee.com/oauth/token user-info-uri: https://gitee.com/api/v5/user user-name-attribute: name server:port:8089

4.2 自定义用户信息处理

GitHubGitee 返回的用户数据结构不同,需统一处理:

publicclassCustomOAuth2UserServiceimplementsOAuth2UserService<OAuth2UserRequest,OAuth2User>{@OverridepublicOAuth2UserloadUser(OAuth2UserRequest userRequest)throwsOAuth2AuthenticationException{DefaultOAuth2UserService delegate =newDefaultOAuth2UserService();OAuth2User oAuth2User = delegate.loadUser(userRequest);String registrationId = userRequest.getClientRegistration().getRegistrationId();Map<String,Object> attributes = oAuth2User.getAttributes();// 根据平台解析用户信息if("github".equals(registrationId)){returnnewDefaultOAuth2User( oAuth2User.getAuthorities(), attributes,"login"// GitHub 的用户名字段);}elseif("gitee".equals(registrationId)){returnnewDefaultOAuth2User( oAuth2User.getAuthorities(), attributes,"name"// Gitee 的用户名字段);}thrownewOAuth2AuthenticationException("Unsupported platform");}}

4.3 SecurityConfig配置

@Configuration@EnableWebSecuritypublicclassSecurityConfig{@BeanpublicSecurityFilterChainsecurityFilterChain(HttpSecurity http)throwsException{ http .authorizeHttpRequests(authorize -> authorize .requestMatchers("/","/public/**").permitAll().anyRequest().authenticated()).oauth2Login(oauth2 -> oauth2 .userInfoEndpoint(userInfo -> userInfo .userService(customOAuth2UserService()))).csrf(AbstractHttpConfigurer::disable);return http.build();}@BeanpublicOAuth2UserService<OAuth2UserRequest,OAuth2User>customOAuth2UserService(){returnnewCustomOAuth2UserService();}}

4.4 控制器与页面

编写自定义的登陆页,授权登陆返回授权的用户信息

@ControllerpublicclassIndexController{@GetMapping("/")publicStringindex(Model model,@RegisteredOAuth2AuthorizedClientOAuth2AuthorizedClient authorizedClient,@AuthenticationPrincipalOAuth2User oauth2User){ model.addAttribute("userName", oauth2User.getName()); model.addAttribute("clientName", authorizedClient.getClientRegistration().getClientName()); model.addAttribute("userAttributes", oauth2User.getAttributes());//TODO 可以增加与项目用户注册、绑定等业务servicereturn"index";}@GetMapping("/user")@ResponseBodypublicMap<String,Object>userInfo(OAuth2AuthenticationToken authentication){OAuth2User user = authentication.getPrincipal();returnMap.of("username", user.getName(),"authorities", user.getAuthorities().stream().map(GrantedAuthority::getAuthority).collect(Collectors.toList()));}}

thymeleaf模版页面

<!DOCTYPEhtml><htmlxmlns="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><metacharset="utf-8"/></head><body><divstyle="float: right"th:fragment="logout"sec:authorize="isAuthenticated()"><divstyle="float:left"><spanstyle="font-weight:bold">User: </span><spansec:authentication="name"></span></div><divstyle="float:none">&nbsp;</div><divstyle="float:right"><formaction="#"th:action="@{/logout}"method="post"><inputtype="submit"value="Logout"/></form></div></div><h1>OAuth 2.0 Login with Spring Security</h1><div> You are successfully logged in <spanstyle="font-weight:bold"th:text="${userName}"></span> via the OAuth 2.0 Client <spanstyle="font-weight:bold"th:text="${clientName}"></span></div><div>&nbsp;</div><div><spanstyle="font-weight:bold">User Attributes:</span><ul><lith:each="userAttribute : ${userAttributes}"><spanstyle="font-weight:bold"th:text="${userAttribute.key}"></span>: <spanth:text="${userAttribute.value}"></span></li></ul></div></body></html>

5. 测试验证

  • 访问首页:http://localhost:8089,跳转至 Spring Security 默认登录页
注:应用回调地址如果天蝎的是 localhost,本地访问项目也保持 localhost 访问,如使用127.0.0.1 会导致回调地址不一致出现回调地址错误的异常
  • 选择登录方式:点击 GitHubGitee 按钮,跳转至对应授权页面

GitHub授权页

在这里插入图片描述

Gitee授权页

在这里插入图片描述
  • 授权登录:同意授权后,自动跳转回应用,显示用户信息
  • 访问 http://localhost:8089/user 显示经过处理的用户信息
{"authorities":["OAUTH2_USER","SCOPE_user:email"],"username":"授权返回的用户名"}

6. 常见问题与扩展

  • 回调地址不匹配:确保 redirect-uri 与注册应用时填写的一致
  • 跨域问题:前端分离项目需配置 CorsFilter
  • 用户信息字段缺失:检查 user-info-uriuser-name-attribute 配置

7. 总结

通过 Spring Security 6OAuth2 Client 模块,开发者可以快速集成主流社交登录功能。本章节案例以 GitHubGitee 为例,展示了从应用注册到用户信息处理的完整流程,适用于需要第三方登录的 Web 应用场景。
后续章节博主将继续扩展支持微信、支付宝、QQ、微博等更多平台,进一步提升用户体验。

如果你在实践过程中有任何疑问或更好的扩展思路,欢迎在评论区留言,最后希望大家一键三连给博主一点点鼓励!


Read more

Java 时间类(上):JDK7 及以前时间类 Date、SimpleDateFormat、Calendar 最全总结

Java 时间类(上):JDK7 及以前时间类 Date、SimpleDateFormat、Calendar 最全总结

🏠个人主页:黎雁 🎬作者简介:C/C++/JAVA后端开发学习者 ❄️个人专栏:C语言、数据结构(C语言)、EasyX、JAVA、游戏、规划、程序人生 ✨ 从来绝巘须孤往,万里同尘即玉京 文章目录 * Java 时间类(上):JDK7 及以前时间类 Date、SimpleDateFormat、Calendar 最全总结 🕒 * 📝 文章摘要 * 一、时间相关基础知识点 ⏱ * 1. 时间标准 * 2. 时间单位与换算 * 二、Date 时间类 📅 * 1. 概述 * 2. 构造方法 * 3. 成员方法 * 4. 代码示例 * 三、SimpleDateFormat 格式化与解析 ✍️ * 1. 作用

By Ne0inhk

Java版LeetCode热题100之「两两交换链表中的节点」详解

Java版LeetCode热题100之「两两交换链表中的节点」详解 本文约9200字,全面深入剖析 LeetCode 第24题《两两交换链表中的节点》。涵盖题目解析、递归与迭代两种解法、复杂度分析、面试高频问答、实际开发应用场景、相关题目推荐等,助你彻底掌握链表操作核心技巧。 一、原题回顾 题目描述: 给你一个链表,两两交换其中相邻的节点,并返回交换后链表的头节点。 你必须在不修改节点内部的值的情况下完成本题(即,只能进行节点交换)。 示例 1: 输入:head = [1,2,3,4] 输出:[2,1,4,3] 示例 2: 输入:head = [] 输出:[] 示例 3: 输入:head = [1] 输出:[1] 提示:

By Ne0inhk
AI协作天花板!CherryStudio让多模型协同像搭积木

AI协作天花板!CherryStudio让多模型协同像搭积木

文章目录 * 前言 * 【视频教程】 * 1. 本地安装 * 2. 配置模型服务 * 2.1 配置在线模型服务 * 2.2 配置本地模型服务 * 2.3 其他功能简单演示 * 2.3.1 创建智能体 * 2.3.2 AI文生图 * 3. 安装内网穿透工具 * 4. 配置公网地址 * 5. 配置固定公网地址 * 总结 前言 当单个AI还在"单打独斗"时,CherryStudio已经实现了多模型协作的突破!这款开源客户端支持同时接入OpenAI、Gemini、本地Ollama等10+模型,就像一个AI调度中心。最惊艳的是"智能体协作"功能——让GPT分析数据,让Claude写报告,

By Ne0inhk

CodeBuddy AI IDE :Skills 模式

国内首个支持Skills模式的编程助手,开启AI编程二阶段! 一、Skills模式核心概念与架构 🎯 核心概念:从"写提示词"到"设计流程"的范式转变 Skills模式是CodeBuddy AI IDE的核心创新机制,它通过模块化、可组合的AI能力封装,将专业知识与经验转化为AI可执行的"技能包"。与传统IDE的固定功能不同,Skills模式实现了从"工具使用"到"能力赋能"的根本转变。 基本定义:每个Skill是一个标准化的文件夹体系,包含: * SKILL.md文件(必需):包含YAML前置元数据(name、description)和Markdown指令 * 捆绑资源(可选):scripts/(可执行代码)、references/(参考文档)、assets/

By Ne0inhk