首頁 > Java > java教程 > 主體

Spring Boot 中的 OAuth 驗證:Google 與 GitHub 登入整合指南

王林
發布: 2024-08-31 18:31:17
原創
696 人瀏覽過

使用 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 的憑證中從 Google 替換憑證。

要向 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學習者快速成長!