การผสานรวม OAuth2 และการเข้าสู่ระบบด้วยบัญชีโซเชียล
มอบหมายการยืนยันตัวตนให้ผู้ให้บริการที่เชื่อถือได้ เช่น Google และ GitHub โดยใช้การรองรับไคลเอ็นต์ OAuth2 ของ Spring Security
การผสานรวม OAuth2 และการเข้าสู่ระบบด้วยบัญชีโซเชียล เป็นบทเรียน Spring Boot 4 Complete Guide ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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_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.
เรียนรู้ Java ด้วย AI tutor — ฟรี
เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป
- คอร์ส
- 21
- บทเรียน
- 84
คำถามที่พบบ่อย
บทเรียน “การผสานรวม OAuth2 และการเข้าสู่ระบบด้วยบัญชีโซเชียล” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การผสานรวม OAuth2 และการเข้าสู่ระบบด้วยบัญชีโซเชียล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Boot 4 Complete Guide ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Boot 4 Complete Guide มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การผสานรวม OAuth2 และการเข้าสู่ระบบด้วยบัญชีโซเชียล”
มอบหมายการยืนยันตัวตนให้ผู้ให้บริการที่เชื่อถือได้ เช่น Google และ GitHub โดยใช้การรองรับไคลเอ็นต์ OAuth2 ของ Spring Security คุณปฏิบัติ Spring Boot 4 Complete Guide ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Boot 4 Complete Guide หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Boot 4 Complete Guide บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “การผสานรวม OAuth2 และการเข้าสู่ระบบด้วยบัญชีโซเชียล” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Boot 4 Complete Guide นี้ได้ไหม
ได้ บทเรียน Spring Boot 4 Complete Guide ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- พื้นฐาน Spring Security
- การยืนยันตัวตนและการอนุญาตสิทธิ์
- ความปลอดภัยโดยใช้ JWT
- การผสานรวม OAuth2 และการเข้าสู่ระบบด้วยบัญชีโซเชียล