0Pricing
Spring Security 6 & JWT Authentication · บทเรียน

การผสานรวมการเข้าสู่ระบบผ่านโซเชียล

ผสานรวมผู้ให้บริการเข้าสู่ระบบผ่านโซเชียลยอดนิยม (เช่น Google และ GitHub) เข้ากับแอปพลิเคชันของคุณโดยใช้ไคลเอ็นต์ OAuth2 ของ Spring Security

การผสานรวมการเข้าสู่ระบบผ่านโซเชียล เป็นบทเรียน Spring Security 6 & JWT Authentication ฟรีบน CoddyKit นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Security 6 & JWT Authentication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Social Login: Easy Access

Welcome to social login! Ever seen buttons like "Sign in with Google" or "Login with GitHub"? That's social login in action.

It lets users sign into your application using their existing accounts from popular identity providers. This means less friction for users and no new passwords to remember!

Spring Security Simplifies It

Integrating social login manually can be tricky, involving many redirects and token exchanges. But don't worry!

Spring Security provides powerful, out-of-the-box support for OAuth2 clients. It handles most of the complex flow for you, making it straightforward to add providers like Google, GitHub, or Facebook.

Your App as an OAuth2 Client

In the context of social login, your application acts as an OAuth2 Client. This client needs to be registered with the social identity provider (which is the Authorization Server).

This registration process gives your application unique credentials: a Client ID and a Client Secret.

Registering with Google (Part 1)

Let's use Google as a practical example. To enable Google Sign-In, you first need to register your application with the Google Cloud Console.

  • Create a new project.
  • Go to APIs & Services > OAuth consent screen to configure user-facing information.

This tells Google who your app is and what it wants to do.

Registering with Google (Part 2)

Next, you'll create OAuth client ID credentials. Choose 'Web application' as the application type.

The most crucial step is setting the 'Authorized redirect URIs'. This is where Google will send the user back after successful authentication.

  • For local development, this is often http://localhost:8080/login/oauth2/code/google.
  • You'll get a Client ID and Client Secret – keep these safe!

Spring Boot Configuration

With your Google Client ID and Secret, you can now configure your Spring Boot application's application.properties (or .yml) file.

Spring Security uses a convention for naming these properties. Just replace the placeholder values with your actual credentials.

spring.security.oauth2.client.registration.google.client-id=YOUR_GOOGLE_CLIENT_ID
spring.security.oauth2.client.registration.google.client-secret=YOUR_GOOGLE_CLIENT_SECRET

Minimal Security Configuration

To enable OAuth2 social login, you need a basic Spring Security configuration. Spring Boot's defaults are very helpful here.

By simply calling .oauth2Login() on your HttpSecurity object, Spring Security automatically configures the necessary filters and endpoints to handle the OAuth2 flow.

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.web.SecurityFilterChain;

@Configuration
@EnableWebSecurity
public class SecurityConfig {

  @Bean
  public SecurityFilterChain securityFilterChain(HttpSecurity http) throws Exception {
    http
      .authorizeHttpRequests(authorize -> authorize
        .anyRequest().authenticated() // Require authentication for all requests
      )
      .oauth2Login(oauth2 -> oauth2
        // Spring Security provides a default login page with social login links
      );
    return http.build();
  }
}

The Social Login Page

With the configuration in place, Spring Security will automatically generate a login page at /login (if no custom login page is specified).

This page will include links for each configured social provider. When a user clicks, they are redirected to the provider for authentication.

<p>Conceptual HTML for a login link:</p>
<code>&lt;a href="/oauth2/authorization/google"&gt;Login with Google&lt;/a&gt;</code>
<p>Spring Security handles the redirection to Google and processes the response.</p>

Accessing User Details

After a successful social login, Spring Security populates the SecurityContext with the authenticated user's details.

You can inject an OAuth2User object into your controllers to access information like the user's name, email, or other attributes provided by the social identity provider.

import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.security.oauth2.core.user.OAuth2User;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

// This is a snippet of a Spring Boot controller.
// It needs a full Spring Boot application context to run.
@RestController
public class UserController {

  @GetMapping("/user-info")
  public String getUserInfo(@AuthenticationPrincipal OAuth2User oauth2User) {
    if (oauth2User != null) {
      String name = oauth2User.getAttribute("name");
      String email = oauth2User.getAttribute("email");
      return "Hello, " + name + "! Your email is " + email + ".";
    }
    return "User not authenticated via social login.";
  }
}

Social Login Quiz

You've learned how to set up social login! Let's test your understanding of the core components.

Social Login Recap

Great job! You've learned how to integrate social login into your Spring Boot application using Spring Security's OAuth2 client support.

  • We understood the benefits of social login and Spring Security's role.
  • We walked through setting up your app as an OAuth2 Client and registering it with a provider like Google.
  • You saw how to configure application.properties and enable .oauth2Login() in your security chain.
  • Finally, you learned how to access authenticated user details using the OAuth2User object.

Social login significantly enhances user experience and simplifies authentication!

คำถามที่พบบ่อย

บทเรียน “การผสานรวมการเข้าสู่ระบบผ่านโซเชียล” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การผสานรวมการเข้าสู่ระบบผ่านโซเชียล” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Security 6 & JWT Authentication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การผสานรวมการเข้าสู่ระบบผ่านโซเชียล”

ผสานรวมผู้ให้บริการเข้าสู่ระบบผ่านโซเชียลยอดนิยม (เช่น Google และ GitHub) เข้ากับแอปพลิเคชันของคุณโดยใช้ไคลเอ็นต์ OAuth2 ของ Spring Security คุณปฏิบัติ Spring Security 6 & JWT Authentication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Security 6 & JWT Authentication หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Security 6 & JWT Authentication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 2 จากทั้งหมด 4 บทเรียน

บทเรียน “การผสานรวมการเข้าสู่ระบบผ่านโซเชียล” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Spring Security 6 & JWT Authentication นี้ได้ไหม

ได้ บทเรียน Spring Security 6 & JWT Authentication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การตั้งค่าไคลเอ็นต์ OAuth2
  2. การผสานรวมการเข้าสู่ระบบผ่านโซเชียล
  3. ตัวจัดการความสำเร็จ OAuth2 แบบกำหนดเอง
  4. การเข้าถึงผู้ใช้ OAuth2 ที่ผ่านการตรวจสอบสิทธิ์
← กลับไปที่ Spring Security 6 & JWT Authentication