Home> Java> javaTutorial> body text

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

王林
Release: 2024-08-31 18:31:17
Original
607 people have browsed it

Enhance Security with OAuth 2.0: Implementing Social Logins in Spring Boot

In the world of modern web development, securing your applications and making authentication as smooth as possible for users is a top priority. That’s where OAuth 2.0 comes in—it’s a powerful tool that not only helps secure your APIs but also lets users log in with their existing accounts from platforms like Google and GitHub. This makes things easier for everyone: users don’t need to remember yet another password, and developers get a reliable way to manage authentication.

In this blog, I’ll take you step by step through how to set up OAuth 2.0 in a Spring Boot application. We’ll be integrating both Google and GitHub for authentication, so your users can choose which service they want to use to log in. I’ll also show you how to protect your API endpoints using JWT (JSON Web Tokens), ensuring that only authenticated users can access the resources they’re supposed to.

Whether you’re building a new app or adding security to an existing one, this guide will give you the tools you need to keep your Spring Boot application secure and user-friendly.

Visit https://start.spring.io/

create the project

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

Download the zip and extract it and load the project to your IDE.

The "OAuth2 Client" dependency in Spring Boot simplifies integrating OAuth 2.0 authentication with providers like Google and GitHub. It handles the entire OAuth login flow, including redirecting users to the provider's login page, managing tokens, and securing API endpoints. By adding this dependency, you can easily enable secure and user-friendly authentication in your Spring Boot application.

The "Spring Web" dependency in Spring Boot is crucial for developing web applications. It provides essential features like RESTful API creation, MVC architecture support, and the ability to serve HTML views. With Spring Web, you can easily handle HTTP requests and responses, manage routing, and integrate with other Spring components, making it a foundational part of building robust web applications.

Application Configuration

To set up your Spring Boot application for OAuth 2.0 authentication with Google and GitHub, you'll need to configure the application.properties file. This file contains essential settings for your application, including OAuth client credentials, logging levels, and JWT configurations.

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
Copy after login

OAuth Client Configurations:Replace YOUR_GOOGLE_CLIENT_ID, YOUR_GOOGLE_CLIENT_SECRET, YOUR_GITHUB_CLIENT_ID, and YOUR_GITHUB_CLIENT_SECRET with the credentials you obtain from Google and GitHub when you register your application.

To register your application with Google and GitHub for OAuth 2.0 authentication, we need to go https://console.cloud.google.com

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

click on API Services

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

Credentials -> create Credentials -> OAuth client ID

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

OAuth client ID -> Create OAuth client ID

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

selectApplication typetoweb Application

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

give application name

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

setAuthorized redirect URIswith this URL and here our application is running on 8000 port so application port is 8000. then click on create

http://localhost:8000/login/oauth2/code/google
Copy after login

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

after that OAuth client is created and we get the client ID and the Client secret.

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
Copy after login

TheSecurityConfigclass 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
Copy after login

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 thedeveloper 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 oncreate 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
Copy after login

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

and setHomepage URL

http://localhost:8000
Copy after login

after registering the Application we get theClient 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=
Copy after login

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 配置來進一步探索。快樂編碼!

The above is the detailed content of OAuth Authentication in Spring Boot: A Guide to Integrating Google and GitHub Login. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!