Spring Boot 4 Complete Guide · レッスン

OAuth2とソーシャルログインの統合

Spring SecurityのOAuth2クライアントサポートを使い、GoogleやGitHubなど信頼できるプロバイダーに認証を委任します。

レッスン 4/413 ステップ

「OAuth2とソーシャルログインの統合」はCoddyKit上の無料Spring Boot 4 Complete Guideレッスンです。 これはレッスン4/4です。 下記で完全なレッスンを無料で読むことができます。その後、ブラウザ内の組み込みコードエディタと24時間対応のAIチューターでハンズオン演習できます。 これはSpring Boot 4 Complete Guide学習パスの一部であり、ウェブとCoddyKitアプリ全体で進捗が同期されます。 Spring Boot 4 Complete Guideコースには全4レッスンが含まれています。

このレッスンの一部はまだ翻訳されておらず、英語で表示されています。

What Is OAuth2?

OAuth2 is a protocol that lets users grant your app limited access to their identity at another provider, without sharing their password. It powers Login with Google, GitHub, and more.

Roles in OAuth2

Three players matter: the user (resource owner), your app (the client), and the authorization server (the provider). Spring orchestrates the handshake between them.

The Authorization Code Flow

The most common flow redirects the user to the provider, who returns an authorization code. Your app exchanges that code for tokens behind the scenes.

  • Redirect to provider
  • User approves
  • Receive code, exchange for token

Adding the OAuth2 Client Starter

Spring Security ships an OAuth2 client starter that handles the entire flow for you. Add the dependency to get started.

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-oauth2-client</artifactId>
</dependency>

Registering a Provider

Configure your client ID and secret in properties. Spring already knows the endpoints for common providers like Google.

spring.security.oauth2.client.registration.google.client-id=YOUR_ID
spring.security.oauth2.client.registration.google.client-secret=YOUR_SECRET

Enabling Login

Call oauth2Login() in your security configuration to wire up the login page and callback handling automatically.

http.authorizeHttpRequests(a -> a.anyRequest().authenticated())
    .oauth2Login(Customizer.withDefaults());

Reading the Authenticated User

After login, inject the OAuth2User to access profile attributes like name and email returned by the provider.

@GetMapping("/me")
public String me(@AuthenticationPrincipal OAuth2User user) {
    return user.getAttribute("email");
}

Mapping Provider Roles

Providers return their own attributes. You often map them to your application's authorities so your existing access rules still apply.

OAuth2 vs OpenID Connect

OAuth2 is about authorization, while OpenID Connect adds an identity layer with an ID token. Most social logins use OIDC under the hood for authentication.

Persisting Users

On first login you typically create a local user record keyed by the provider's unique ID, linking the external identity to your own data model.

Security Considerations

Always validate redirect URIs, keep client secrets out of source control, and request only the scopes you actually need.

Quick Check

Test your understanding of OAuth2 login.

Recap

You added OAuth2 client support, registered a provider, enabled oauth2Login(), and read the authenticated user. Social login lets you offload password handling to trusted providers.

無料で開始

AI チューターと学ぶ Java — 無料

ブラウザでリアルコードを書いて実行し、24/7 の AI チューターから瞬時にサポートを受け、ウェブまたはアプリで続きから学習できます。

コース
21
レッスン
84

よくある質問

「OAuth2とソーシャルログインの統合」レッスンは無料ですか?

はい。「OAuth2とソーシャルログインの統合」の完全なテキストはこのウェブで無料で読めます。インタラクティブに演習し(組み込みコードエディタと24時間対応のAIチューター)、Spring Boot 4 Complete Guideコースの残りをアンロックするには、CoddyKit PROにアップグレードしてください。 Spring Boot 4 Complete Guideコースには全4レッスンが含まれています。

「OAuth2とソーシャルログインの統合」で何を学びますか?

Spring SecurityのOAuth2クライアントサポートを使い、GoogleやGitHubなど信頼できるプロバイダーに認証を委任します。 ブラウザで直接実行するハンズオンコードでSpring Boot 4 Complete Guideを演習し、24時間対応のAIチューターがレッスンを進める中での質問に答えます。

Spring Boot 4 Complete Guideを始めるのに経験は必要ですか?

事前経験は必要ありません。CoddyKitのSpring Boot 4 Complete Guideは初級者から上級者向けに構成されているため、ここから始めるか最初から始めて、自分のペースで進むことができます。 これはレッスン4/4です。

「OAuth2とソーシャルログインの統合」レッスンにはどのくらい時間がかかりますか?

ほとんどのCoddyKitレッスンは約5~10分かかります。各レッスンはコンパクトでインタラクティブなので、着実に進歩し、ウェブとアプリ全体で正確に前回の場所から再開できます。

このSpring Boot 4 Complete Guideレッスンでコードを書いて実行できますか?

はい。すべてのSpring Boot 4 Complete Guideレッスンに組み込みコードエディタが含まれているため、ブラウザでリアルコードを書いて実行し、即座のAIフィードバックを取得できます。ローカル設定は不要です。

このコースのすべてのレッスン

  1. Spring Securityの基礎
  2. 認証と認可
  3. JWTベースのセキュリティ
  4. OAuth2とソーシャルログインの統合
← Spring Boot 4 Complete Guideに戻る