首页 Java java教程 Spring Boot 中的 OAuth 身份验证:Google 和 GitHub 登录集成指南

Spring Boot 中的 OAuth 身份验证:Google 和 GitHub 登录集成指南

Aug 31, 2024 pm 06:31 PM

使用 OAuth 2.0 增强安全性:在 Spring Boot 中实现社交登录

在现代 Web 开发的世界中,保护您的应用程序并使用户的身份验证尽可能顺利是首要任务。这就是 OAuth 2.0 的用武之地——它是一个强大的工具,不仅可以帮助保护您的 API,还可以让用户使用现有帐户从 Google 和 GitHub 等平台登录。这使每个人的事情变得更容易:用户不需要记住另一个密码,开发人员可以获得可靠的方法来管理身份验证。

在本博客中,我将逐步引导您了解如何在 Spring Boot 应用程序中设置 OAuth 2.0。我们将集成 Google 和 GitHub 进行身份验证,以便您的用户可以选择他们想要使用哪个服务来登录。我还将向您展示如何使用 JWT(JSON Web 令牌)保护您的 API 端点,确保仅经过身份验证的用户可以访问他们应该访问的资源。

无论您是构建新应用程序还是为现有应用程序添加安全性,本指南都将为您提供保持 Spring Boot 应用程序安全和用户友好所需的工具。

访问 https://start.spring.io/

创建项目

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

下载 zip 并解压,然后将项目加载到您的 IDE。

Spring Boot 中的“OAuth2 Client”依赖项简化了 OAuth 2.0 身份验证与 Google 和 GitHub 等提供商的集成。它处理整个 OAuth 登录流程,包括将用户重定向到提供商的登录页面、管理令牌和保护 API 端点。通过添加此依赖项,您可以轻松地在 Spring Boot 应用程序中启用安全且用户友好的身份验证。

Spring Boot 中的“Spring Web”依赖对于开发 Web 应用程序至关重要。它提供了一些基本功能,例如 RESTful API 创建、MVC 架构支持以及提供 HTML 视图的能力。使用 Spring Web,您可以轻松处理 HTTP 请求和响应、管理路由以及与其他 Spring 组件集成,使其成为构建健壮的 Web 应用程序的基础部分。

应用程序配置

要设置 Spring Boot 应用程序以通过 Google 和 GitHub 进行 OAuth 2.0 身份验证,您需要配置 application.properties 文件。此文件包含应用程序的基本设置,包括 OAuth 客户端凭据、日志记录级别和 JWT 配置。

spring.application.name=oauth2-authentication-service
server.port=8000

#for google
spring.security.oauth2.client.registration.google.client-id=YOUR_GOOGLE_CLIENT_ID
spring.security.oauth2.client.registration.google.client-secret=YOUR_GOOGLE_CLIENT_SECRET

#for github
spring.security.oauth2.client.registration.github.client-id=YOUR_GITHUB_CLIENT_ID
spring.security.oauth2.client.registration.github.client-secret= YOUR_GITHUB_CLIENT_SECRET

登录后复制

OAuth 客户端配置: 将 YOUR_GOOGLE_CLIENT_ID、YOUR_GOOGLE_CLIENT_SECRET、YOUR_GITHUB_CLIENT_ID 和 YOUR_GITHUB_CLIENT_SECRET 替换为您在注册应用程序时从 Google 和 GitHub 获取的凭据。

要向 Google 和 GitHub 注册您的应用程序以进行 OAuth 2.0 身份验证,我们需要访问 https://console.cloud.google.com

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

点击 API 服务

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

凭证 ->创建凭证 -> OAuth 客户端 ID

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

OAuth 客户端 ID ->创建 OAuth 客户端 ID

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

选择应用程序类型网络应用程序

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

给出应用程序名称

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

使用此 URL 设置 授权重定向 URI,这里我们的应用程序在 8000 端口上运行,因此应用程序端口为 8000。然后单击创建

http://localhost:8000/login/oauth2/code/google
登录后复制

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

创建 OAuth 客户端后,我们会获取客户端 ID 和客户端密钥。

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

copy both and replace with the the properties of application.properties file

spring.security.oauth2.client.registration.google.client-id=YOUR_GOOGLE_CLIENT_ID
spring.security.oauth2.client.registration.google.client-secret=YOUR_GOOGLE_CLIENT_SECRET
登录后复制

The SecurityConfig class configures security for a Spring Boot application using OAuth2. It defines a SecurityFilterChain bean, which sets up security rules. The authorizeHttpRequests method ensures that all incoming requests require authentication. The .oauth2Login(Customizer.withDefaults()) line enables OAuth2 login functionality with default settings. Finally, the securityFilterChain method returns the configured security filter chain by calling http.build(). This setup ensures that the application is secure and supports OAuth2 authentication for users.

Accessing Your Application via Chrome

When developing and testing your Spring Boot application, it's crucial to know how to interact with it through Postman. If your application is running locally on port 8000, you can access it using the following base URL:

http://localhost:8000

登录后复制

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

we get the similar response like this

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

now we can access the end points.

GitHub Authentication

GitHub Authentication in Spring Boot allows users to log in using their GitHub accounts, streamlining the authentication process and enhancing security. By integrating GitHub as an OAuth 2.0 provider, your application can authenticate users through GitHub's trusted platform. This involves registering your application on GitHub to obtain a Client ID and Client Secret, which are then configured in your Spring Boot application. Users are redirected to GitHub for login, and upon successful authentication, they are redirected back to your application with an access token, allowing secure access to your protected resources. This integration is ideal for applications targeting developers and tech-savvy users.

create GitHub account and go to settings

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

in the left corner we get the developer settings

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

Navigate to OAuth Apps

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

click on create OAuth App

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

we get the interface like this

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

set ** Authorization callback URL ** according to your application port

http://localhost:8000/login/oauth2/code/github
登录后复制

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

and set Homepage URL

http://localhost:8000
登录后复制

after registering the Application we get the Client ID and Client Secret

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

now replace with the Application.properties file properties

spring.security.oauth2.client.registration.github.client-id=Ov23liBMLc5e1ItoONPx
spring.security.oauth2.client.registration.github.client-secret= 

登录后复制

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

Test the GitHub Login

Login with GitHub: When prompted, log in with your GitHub credentials.
Success Redirect: Upon successful authentication, you'll be redirected to the /home page of your application.

OAuth  Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login

您可以在我的 GitHub 存储库上探索用户身份验证服务的完整源代码。该项目展示了各种功能,例如用户注册、登录和使用 JWT 进行身份验证的安全访问。请随意查看、贡献或将其用作您自己的项目的参考!

GitHub 存储库:https://github.com/ishrivasayush/oauth2-authentication-service

结论

使用 Spring Boot 实现 OAuth 2.0,使用 Google 和 GitHub 作为身份验证提供程序,是增强应用程序安全性和可用性的有效方法。通过允许用户使用现有帐户登录,您可以减少摩擦并提供更流畅的用户体验。同时,使用 JWT 保护您的 API 端点可确保只有经过身份验证的用户才能访问敏感资源。

通过本指南,我们涵盖了从在 Google 和 GitHub 上设置 OAuth 凭据到配置 Spring Boot 应用程序以处理身份验证和保护端点的所有内容。无论您是 OAuth 2.0 的新手还是希望将其集成到您的项目中,这些步骤都将帮助您构建安全且可扩展的身份验证系统。

安全是一个永无止境的旅程,但通过正确的工具和实践,您可以构建既安全又用户友好的应用程序。现在您已经有了坚实的基础,您可以通过添加更多提供商、自定义用户体验或更深入地研究 JWT 配置来进一步探索。快乐编码!

以上是Spring Boot 中的 OAuth 身份验证:Google 和 GitHub 登录集成指南的详细内容。更多信息请关注PHP中文网其他相关文章!

本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智能驱动的应用程序,用于创建逼真的裸体照片

AI Clothes Remover

AI Clothes Remover

用于从照片中去除衣服的在线人工智能工具。

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

Video Face Swap

Video Face Swap

使用我们完全免费的人工智能换脸工具轻松在任何视频中换脸!

热门文章

<🎜>:泡泡胶模拟器无穷大 - 如何获取和使用皇家钥匙
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
北端:融合系统,解释
3 周前 By 尊渡假赌尊渡假赌尊渡假赌
Mandragora:巫婆树的耳语 - 如何解锁抓钩
3 周前 By 尊渡假赌尊渡假赌尊渡假赌

热工具

记事本++7.3.1

记事本++7.3.1

好用且免费的代码编辑器

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

禅工作室 13.0.1

禅工作室 13.0.1

功能强大的PHP集成开发环境

Dreamweaver CS6

Dreamweaver CS6

视觉化网页开发工具

SublimeText3 Mac版

SublimeText3 Mac版

神级代码编辑软件(SublimeText3)

热门话题

Java教程
1666
14
CakePHP 教程
1425
52
Laravel 教程
1327
25
PHP教程
1273
29
C# 教程
1252
24
公司安全软件导致应用无法运行?如何排查和解决? 公司安全软件导致应用无法运行?如何排查和解决? Apr 19, 2025 pm 04:51 PM

公司安全软件导致部分应用无法正常运行的排查与解决方法许多公司为了保障内部网络安全,会部署安全软件。...

如何将姓名转换为数字以实现排序并保持群组中的一致性? 如何将姓名转换为数字以实现排序并保持群组中的一致性? Apr 19, 2025 pm 11:30 PM

将姓名转换为数字以实现排序的解决方案在许多应用场景中,用户可能需要在群组中进行排序,尤其是在一个用...

如何使用MapStruct简化系统对接中的字段映射问题? 如何使用MapStruct简化系统对接中的字段映射问题? Apr 19, 2025 pm 06:21 PM

系统对接中的字段映射处理在进行系统对接时,常常会遇到一个棘手的问题:如何将A系统的接口字段有效地映�...

IntelliJ IDEA是如何在不输出日志的情况下识别Spring Boot项目的端口号的? IntelliJ IDEA是如何在不输出日志的情况下识别Spring Boot项目的端口号的? Apr 19, 2025 pm 11:45 PM

在使用IntelliJIDEAUltimate版本启动Spring...

如何优雅地获取实体类变量名构建数据库查询条件? 如何优雅地获取实体类变量名构建数据库查询条件? Apr 19, 2025 pm 11:42 PM

在使用MyBatis-Plus或其他ORM框架进行数据库操作时,经常需要根据实体类的属性名构造查询条件。如果每次都手动...

Java对象如何安全地转换为数组? Java对象如何安全地转换为数组? Apr 19, 2025 pm 11:33 PM

Java对象与数组的转换:深入探讨强制类型转换的风险与正确方法很多Java初学者会遇到将一个对象转换成数组的�...

如何利用Redis缓存方案高效实现产品排行榜列表的需求? 如何利用Redis缓存方案高效实现产品排行榜列表的需求? Apr 19, 2025 pm 11:36 PM

Redis缓存方案如何实现产品排行榜列表的需求?在开发过程中,我们常常需要处理排行榜的需求,例如展示一个�...

电商平台SKU和SPU数据库设计:如何兼顾用户自定义属性和无属性商品? 电商平台SKU和SPU数据库设计:如何兼顾用户自定义属性和无属性商品? Apr 19, 2025 pm 11:27 PM

电商平台SKU和SPU表设计详解本文将探讨电商平台中SKU和SPU的数据库设计问题,特别是如何处理用户自定义销售属...

See all articles