Spring Boot 4 Complete Guide · Lekcja

Integracja OAuth2 i logowania społecznościowego

Przekażą Państwo uwierzytelnianie zaufanym dostawcom, takim jak Google i GitHub, korzystając z obsługi klienta OAuth2 w Spring Security.

Lekcja 4 z 413 kroki

Integracja OAuth2 i logowania społecznościowego to bezpłatna lekcja Spring Boot 4 Complete Guide na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Spring Boot 4 Complete Guide, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Spring Boot 4 Complete Guide zawiera 4 lekcji w sumie.

Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.

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.

Bezpłatny start

Ucz się Java dzięki korepetycjom AI — za darmo

Pisz i uruchamiaj kod w przeglądarce, otrzymuj natychmiastową pomoc od korepetytora AI dostępnego 24/7 i kontynuuj naukę w sieci lub w aplikacji.

Kursy
21
Lekcje
84

Często zadawane pytania

Czy lekcja „Integracja OAuth2 i logowania społecznościowego” jest bezpłatna?

Tak — pełny tekst „Integracja OAuth2 i logowania społecznościowego” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Spring Boot 4 Complete Guide, przejdź na CoddyKit PRO. Kurs Spring Boot 4 Complete Guide zawiera 4 lekcji w sumie.

Co nauczysz się w „Integracja OAuth2 i logowania społecznościowego”?

Przekażą Państwo uwierzytelnianie zaufanym dostawcom, takim jak Google i GitHub, korzystając z obsługi klienta OAuth2 w Spring Security. Ćwiczysz Spring Boot 4 Complete Guide z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.

Czy potrzebuję doświadczenia, aby zacząć Spring Boot 4 Complete Guide?

Nie wymagamy żadnego doświadczenia. Spring Boot 4 Complete Guide w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.

Ile czasu zajmuje lekcja „Integracja OAuth2 i logowania społecznościowego”?

Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.

Czy mogę pisać i uruchamiać kod w tej lekcji Spring Boot 4 Complete Guide?

Tak. Każda lekcja Spring Boot 4 Complete Guide zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.

Wszystkie lekcje w tym kursie

  1. Podstawy Spring Security
  2. Uwierzytelnianie i autoryzacja
  3. Bezpieczeństwo oparte na JWT
  4. Integracja OAuth2 i logowania społecznościowego
← Powrót do Spring Boot 4 Complete Guide