検索
ホームページJava&#&チュートリアルSpring Boot での OAuth 認証: Google と GitHub ログインを統合するためのガイド

OAuth 2.0 によるセキュリティの強化: Spring Boot でのソーシャル ログインの実装

現代の Web 開発の世界では、アプリケーションを保護し、ユーザーの認証を可能な限りスムーズにすることが最優先事項です。ここで OAuth 2.0 が登場します。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 などのプロバイダーとの統合が簡素化されます。ユーザーのプロバイダーのログイン ページへのリダイレクト、トークンの管理、API エンドポイントの保護など、OAuth ログイン フロー全体を処理します。この依存関係を追加すると、Spring Boot アプリケーションで安全でユーザーフレンドリーな認証を簡単に有効にすることができます。

Spring Boot の "Spring Web" 依存関係は、Web アプリケーションの開発にとって重要です。 RESTful API の作成、MVC アーキテクチャのサポート、HTML ビューを提供する機能などの重要な機能を提供します。 Spring Web を使用すると、HTTP リクエストと応答を簡単に処理し、ルーティングを管理し、他の Spring コンポーネントと統合できるため、堅牢な Web アプリケーションを構築するための基礎部分となります。

アプリケーション構成

Google および GitHub での OAuth 2.0 認証用に Spring Boot アプリケーションを設定するには、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 から取得した認証情報に置き換えます。

OAuth 2.0 認証のためにアプリケーションを Google および GitHub に登録するには、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

アプリケーション タイプWeb アプリケーション

に選択します

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

結論

Google と GitHub を認証プロバイダーとして使用して、Spring Boot で OAuth 2.0 を実装することは、アプリケーションのセキュリティと使いやすさを強化する強力な方法です。ユーザーが既存のアカウントでログインできるようにすることで、煩わしさが軽減され、よりスムーズなユーザー エクスペリエンスが提供されます。同時に、JWT で API エンドポイントを保護することで、認証されたユーザーのみが機密リソースにアクセスできるようになります。

このガイドでは、Google と GitHub での OAuth 資格情報の設定から、認証を処理してエンドポイントを保護するための Spring Boot アプリケーションの構成まで、すべてを説明しました。 OAuth 2.0 を初めて使用する場合でも、OAuth 2.0 をプロジェクトに統合しようとしている場合でも、これらの手順は安全でスケーラブルな認証システムを構築するのに役立ちます。

セキュリティは終わりのない旅ですが、適切なツールと実践を使用すれば、安全でユーザーフレンドリーなアプリケーションを構築できます。強固な基盤ができたので、プロバイダーを追加したり、ユーザー エクスペリエンスをカスタマイズしたり、JWT 構成をさらに詳しく調べたりすることができます。コーディングを楽しんでください!

以上がSpring Boot での OAuth 認証: Google と GitHub ログインを統合するためのガイドの詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

声明
この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
2025年のトップ4 JavaScriptフレームワーク:React、Angular、Vue、Svelte2025年のトップ4 JavaScriptフレームワーク:React、Angular、Vue、SvelteMar 07, 2025 pm 06:09 PM

この記事では、2025年の上位4つのJavaScriptフレームワーク(React、Angular、Vue、Svelte)を分析し、パフォーマンス、スケーラビリティ、将来の見通しを比較します。 強力なコミュニティと生態系のためにすべてが支配的なままですが、彼らの相対的なポップ

カフェインやグアバキャッシュなどのライブラリを使用して、Javaアプリケーションにマルチレベルキャッシュを実装するにはどうすればよいですか?カフェインやグアバキャッシュなどのライブラリを使用して、Javaアプリケーションにマルチレベルキャッシュを実装するにはどうすればよいですか?Mar 17, 2025 pm 05:44 PM

この記事では、カフェインとグアバキャッシュを使用してJavaでマルチレベルキャッシュを実装してアプリケーションのパフォーマンスを向上させています。セットアップ、統合、パフォーマンスの利点をカバーし、構成と立ち退きポリシー管理Best Pra

node.js 20:キーパフォーマンスが向上し、新機能node.js 20:キーパフォーマンスが向上し、新機能Mar 07, 2025 pm 06:12 PM

node.js 20は、V8エンジンの改善、特により速いガベージコレクションとI/Oを介してパフォーマンスを大幅に向上させます。 新機能には、より良いWebセンブリのサポートと洗練されたデバッグツール、開発者の生産性とアプリケーション速度の向上が含まれます。

Javaのクラスロードメカニズムは、さまざまなクラスローダーやその委任モデルを含むどのように機能しますか?Javaのクラスロードメカニズムは、さまざまなクラスローダーやその委任モデルを含むどのように機能しますか?Mar 17, 2025 pm 05:35 PM

Javaのクラスロードには、ブートストラップ、拡張機能、およびアプリケーションクラスローダーを備えた階層システムを使用して、クラスの読み込み、リンク、および初期化が含まれます。親の委任モデルは、コアクラスが最初にロードされ、カスタムクラスのLOAに影響を与えることを保証します

Iceberg:データレイクテーブルの未来Iceberg:データレイクテーブルの未来Mar 07, 2025 pm 06:31 PM

大規模な分析データセットのオープンテーブル形式であるIcebergは、データの湖のパフォーマンスとスケーラビリティを向上させます。 内部メタデータ管理を通じて、寄木細工/ORCの制限に対処し、効率的なスキーマの進化、タイムトラベル、同時wを可能にします

Spring Boot Snakeyaml 2.0 CVE-2022-1471問題修正Spring Boot Snakeyaml 2.0 CVE-2022-1471問題修正Mar 07, 2025 pm 05:52 PM

この記事では、リモートコードの実行を可能にする重大な欠陥であるSnakeyamlのCVE-2022-1471の脆弱性について説明します。 Snakeyaml 1.33以降のSpring Bootアプリケーションをアップグレードする方法は、このリスクを軽減する方法を詳述し、その依存関係のアップデートを強調しています

Javaで機能的なプログラミング技術を実装するにはどうすればよいですか?Javaで機能的なプログラミング技術を実装するにはどうすればよいですか?Mar 11, 2025 pm 05:51 PM

この記事では、Lambda式、Streams API、メソッド参照、およびオプションを使用して、機能プログラミングをJavaに統合することを調べます。 それは、簡潔さと不変性を通じてコードの読みやすさと保守性の改善などの利点を強調しています

高度なJavaプロジェクト管理、自動化の構築、依存関係の解像度にMavenまたはGradleを使用するにはどうすればよいですか?高度なJavaプロジェクト管理、自動化の構築、依存関係の解像度にMavenまたはGradleを使用するにはどうすればよいですか?Mar 17, 2025 pm 05:46 PM

この記事では、Javaプロジェクト管理、自動化の構築、依存関係の解像度にMavenとGradleを使用して、アプローチと最適化戦略を比較して説明します。

See all articles

ホットAIツール

Undresser.AI Undress

Undresser.AI Undress

リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover

AI Clothes Remover

写真から衣服を削除するオンライン AI ツール。

Undress AI Tool

Undress AI Tool

脱衣画像を無料で

Clothoff.io

Clothoff.io

AI衣類リムーバー

AI Hentai Generator

AI Hentai Generator

AIヘンタイを無料で生成します。

ホットツール

ドリームウィーバー CS6

ドリームウィーバー CS6

ビジュアル Web 開発ツール

ゼンドスタジオ 13.0.1

ゼンドスタジオ 13.0.1

強力な PHP 統合開発環境

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Eclipse を SAP NetWeaver アプリケーション サーバーと統合します。

mPDF

mPDF

mPDF は、UTF-8 でエンコードされた HTML から PDF ファイルを生成できる PHP ライブラリです。オリジナルの作者である Ian Back は、Web サイトから「オンザフライ」で PDF ファイルを出力し、さまざまな言語を処理するために mPDF を作成しました。 HTML2FPDF などのオリジナルのスクリプトよりも遅く、Unicode フォントを使用すると生成されるファイルが大きくなりますが、CSS スタイルなどをサポートし、多くの機能強化が施されています。 RTL (アラビア語とヘブライ語) や CJK (中国語、日本語、韓国語) を含むほぼすべての言語をサポートします。ネストされたブロックレベル要素 (P、DIV など) をサポートします。

AtomエディタMac版ダウンロード

AtomエディタMac版ダウンロード

最も人気のあるオープンソースエディター