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

使用 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
如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?如何使用咖啡因或Guava Cache等库在Java应用程序中实现多层缓存?Mar 17, 2025 pm 05:44 PM

本文讨论了使用咖啡因和Guava缓存在Java中实施多层缓存以提高应用程序性能。它涵盖设置,集成和绩效优势,以及配置和驱逐政策管理最佳PRA

Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Java的类负载机制如何起作用,包括不同的类载荷及其委托模型?Mar 17, 2025 pm 05:35 PM

Java的类上载涉及使用带有引导,扩展程序和应用程序类负载器的分层系统加载,链接和初始化类。父代授权模型确保首先加载核心类别,从而影响自定义类LOA

如何在Java中实施功能编程技术?如何在Java中实施功能编程技术?Mar 11, 2025 pm 05:51 PM

本文使用lambda表达式,流API,方法参考和可选探索将功能编程集成到Java中。 它突出显示了通过简洁性和不变性改善代码可读性和可维护性等好处

如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?如何将JPA(Java持久性API)用于具有高级功能(例如缓存和懒惰加载)的对象相关映射?Mar 17, 2025 pm 05:43 PM

本文讨论了使用JPA进行对象相关映射,并具有高级功能,例如缓存和懒惰加载。它涵盖了设置,实体映射和优化性能的最佳实践,同时突出潜在的陷阱。[159个字符]

如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?如何将Maven或Gradle用于高级Java项目管理,构建自动化和依赖性解决方案?Mar 17, 2025 pm 05:46 PM

本文讨论了使用Maven和Gradle进行Java项目管理,构建自动化和依赖性解决方案,以比较其方法和优化策略。

如何将Java的Nio(新输入/输出)API用于非阻滞I/O?如何将Java的Nio(新输入/输出)API用于非阻滞I/O?Mar 11, 2025 pm 05:51 PM

本文使用选择器和频道使用单个线程有效地处理多个连接的Java的NIO API,用于非阻滞I/O。 它详细介绍了过程,好处(可伸缩性,性能)和潜在的陷阱(复杂性,

如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?如何使用适当的版本控制和依赖项管理创建和使用自定义Java库(JAR文件)?Mar 17, 2025 pm 05:45 PM

本文使用Maven和Gradle之类的工具讨论了具有适当的版本控制和依赖关系管理的自定义Java库(JAR文件)的创建和使用。

如何使用Java的插座API进行网络通信?如何使用Java的插座API进行网络通信?Mar 11, 2025 pm 05:53 PM

本文详细介绍了用于网络通信的Java的套接字API,涵盖了客户服务器设置,数据处理和关键考虑因素,例如资源管理,错误处理和安全性。 它还探索了性能优化技术,我

See all articles

热AI工具

Undresser.AI Undress

Undresser.AI Undress

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

AI Clothes Remover

AI Clothes Remover

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

Undress AI Tool

Undress AI Tool

免费脱衣服图片

Clothoff.io

Clothoff.io

AI脱衣机

AI Hentai Generator

AI Hentai Generator

免费生成ai无尽的。

热门文章

R.E.P.O.能量晶体解释及其做什么(黄色晶体)
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.最佳图形设置
3 周前By尊渡假赌尊渡假赌尊渡假赌
R.E.P.O.如果您听不到任何人,如何修复音频
3 周前By尊渡假赌尊渡假赌尊渡假赌

热工具

安全考试浏览器

安全考试浏览器

Safe Exam Browser是一个安全的浏览器环境,用于安全地进行在线考试。该软件将任何计算机变成一个安全的工作站。它控制对任何实用工具的访问,并防止学生使用未经授权的资源。

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

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

MinGW - 适用于 Windows 的极简 GNU

MinGW - 适用于 Windows 的极简 GNU

这个项目正在迁移到osdn.net/projects/mingw的过程中,你可以继续在那里关注我们。MinGW:GNU编译器集合(GCC)的本地Windows移植版本,可自由分发的导入库和用于构建本地Windows应用程序的头文件;包括对MSVC运行时的扩展,以支持C99功能。MinGW的所有软件都可以在64位Windows平台上运行。

SublimeText3汉化版

SublimeText3汉化版

中文版,非常好用

EditPlus 中文破解版

EditPlus 中文破解版

体积小,语法高亮,不支持代码提示功能