首页 > Java > java教程 > 正文

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

王林
发布: 2024-08-31 18:31:17
原创
763 人浏览过

使用 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中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责声明 Sitemap
PHP中文网:公益在线PHP培训,帮助PHP学习者快速成长!