OAuth2- und Social-Login-Integration
Überlassen Sie die Authentifizierung vertrauenswürdigen Providern wie Google und GitHub und nutzen Sie dafür die OAuth2-Client-Unterstützung von Spring Security.
OAuth2- und Social-Login-Integration ist eine kostenlose Spring Boot 4 Complete Guide-Lektion auf CoddyKit. Dies ist Lektion 4 von 4. Du kannst die komplette Lektion unten kostenlos lesen – dann übst du sie direkt im Browser mit einem integrierten Code-Editor und einem KI-Tutor rund um die Uhr. Sie ist Teil des Spring Boot 4 Complete Guide-Lernpfads, und dein Fortschritt wird über Web und CoddyKit-App synchronisiert. Der Spring Boot 4 Complete Guide-Kurs umfasst insgesamt 4 Lektionen.
Teile dieser Lektion wurden noch nicht übersetzt und werden auf Englisch angezeigt.
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_SECRETEnabling 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.
Häufig gestellte Fragen
Ist die Lektion „OAuth2- und Social-Login-Integration“ kostenlos?
Ja — der vollständige Text von „OAuth2- und Social-Login-Integration“ ist hier im Web kostenlos zu lesen. Um sie interaktiv zu üben (integrierter Code-Editor und 24/7 KI-Tutor) und den Rest des Spring Boot 4 Complete Guide-Kurses freizuschalten, upgrade auf CoddyKit PRO. Der Spring Boot 4 Complete Guide-Kurs umfasst insgesamt 4 Lektionen.
Was lerne ich in „OAuth2- und Social-Login-Integration“?
Überlassen Sie die Authentifizierung vertrauenswürdigen Providern wie Google und GitHub und nutzen Sie dafür die OAuth2-Client-Unterstützung von Spring Security. Du übst Spring Boot 4 Complete Guide mit praktischem Code, den du direkt im Browser ausführst, und ein 24/7 KI-Tutor beantwortet deine Fragen während du die Lektion bearbeitest.
Brauche ich Erfahrung, um Spring Boot 4 Complete Guide zu starten?
Keine Vorkenntnisse erforderlich. Spring Boot 4 Complete Guide auf CoddyKit ist für Anfänger bis fortgeschrittene Lernende strukturiert, sodass du hier starten oder von Anfang an beginnen und in deinem eigenen Tempo voranschreiten kannst. Dies ist Lektion 4 von 4.
Wie lange dauert die Lektion „OAuth2- und Social-Login-Integration“?
Die meisten CoddyKit-Lektionen dauern etwa 5–10 Minuten. Jede ist kompakt und interaktiv, sodass du stetig Fortschritte machst und genau dort weitermachst, wo du aufgehört hast – im Web und in der App.
Kann ich in dieser Spring Boot 4 Complete Guide-Lektion Code schreiben und ausführen?
Ja. Jede Spring Boot 4 Complete Guide-Lektion enthält einen integrierten Code-Editor, sodass du echten Code direkt in deinem Browser schreibst und ausführst und sofort KI-Feedback erhältst — ohne lokale Einrichtung erforderlich.
Alle Lektionen in diesem Kurs
- Grundlagen von Spring Security
- Authentifizierung und Autorisierung
- Sicherheit auf JWT-Basis
- OAuth2- und Social-Login-Integration