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

การจัดการข้อผิดพลาดในการตรวจสอบสิทธิ์และจุดเริ่มต้น

ปรับแต่งวิธีที่แอป Spring ซึ่งรักษาความปลอดภัยด้วย JWT ตอบสนองต่อโทเค็นที่ไม่มีอยู่ ไม่ถูกต้อง หรือหมดอายุ โดยใช้ AuthenticationEntryPoint และ AccessDeniedHandler

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

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

Two Kinds of Security Failure

Spring Security distinguishes two failures:

  • Authentication failure (401): the user is not identified — missing or bad token
  • Authorization failure (403): the user is known but lacks permission

Each is handled by a different component.

The Default Behavior

Out of the box, a JWT app without a custom handler may redirect to a login page or return an HTML error. For a stateless API you usually want a clean JSON 401 instead.

AuthenticationEntryPoint

The AuthenticationEntryPoint is invoked when an unauthenticated user hits a protected endpoint. Implement commence to write your own response.

public interface AuthenticationEntryPoint {
    void commence(HttpServletRequest req,
                  HttpServletResponse res,
                  AuthenticationException ex);
}

Returning a JSON 401

Here the entry point sets a 401 status and writes a small JSON body, ideal for SPA and mobile clients.

res.setStatus(401);
res.setContentType('application/json');
res.getWriter().write("{\"error\":\"Unauthorized\"}");

AccessDeniedHandler

When an authenticated user lacks the required role, the AccessDeniedHandler runs. Implement handle to send a 403 response.

public interface AccessDeniedHandler {
    void handle(HttpServletRequest req,
                HttpServletResponse res,
                AccessDeniedException ex);
}

Returning a JSON 403

The denied handler mirrors the entry point but uses status 403 to signal a permission problem rather than a missing identity.

res.setStatus(403);
res.setContentType('application/json');
res.getWriter().write("{\"error\":\"Forbidden\"}");

Wiring Handlers into HttpSecurity

Register both handlers in your security configuration through exceptionHandling.

http.exceptionHandling(ex -> ex
    .authenticationEntryPoint(jwtEntryPoint)
    .accessDeniedHandler(jwtDeniedHandler));

Errors Inside the JWT Filter

If your JWT filter detects an expired or malformed token, do not throw a raw exception. Instead set a request attribute and let the entry point produce a consistent response.

catch (ExpiredJwtException e) {
    request.setAttribute('jwt_error', 'expired');
    filterChain.doFilter(request, response);
}

Including Helpful Details

A good error body helps clients react. Include a machine-readable code and a timestamp, but never leak internal stack traces or secrets.

res.getWriter().write(
  "{\"error\":\"token_expired\",\"status\":401}");

Consistent Error Shape

Keep every security error in the same JSON shape as your other API errors. Consistency lets the frontend handle 401, 403, and 500 with one error pipeline.

Testing the Handlers

Use MockMvc to confirm an unauthenticated request returns 401 and an under-privileged request returns 403 with the expected JSON.

mockMvc.perform(get('/api/secure'))
    .andExpect(status().isUnauthorized())
    .andExpect(jsonPath('$.error').value('Unauthorized'));

Quick Check

Test your understanding of security error handling.

Recap

You learned to customize JWT security errors:

  • AuthenticationEntryPoint handles 401 (unauthenticated)
  • AccessDeniedHandler handles 403 (forbidden)
  • Wire both via exceptionHandling
  • Return consistent JSON and never leak internals

Clear, predictable error responses make your secured API far easier to consume.

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

บทเรียน “การจัดการข้อผิดพลาดในการตรวจสอบสิทธิ์และจุดเริ่มต้น” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การจัดการข้อผิดพลาดในการตรวจสอบสิทธิ์และจุดเริ่มต้น”

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

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

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

บทเรียน “การจัดการข้อผิดพลาดในการตรวจสอบสิทธิ์และจุดเริ่มต้น” ใช้เวลานานแค่ไหน

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

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

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

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

  1. การออกแบบลำดับการยืนยันตัวตนด้วย JWT
  2. การสร้างตัวกรอง JWT แบบกำหนดเอง
  3. การผสานรวม AuthenticationManager และตัวให้บริการ
  4. การจัดการข้อผิดพลาดในการตรวจสอบสิทธิ์และจุดเริ่มต้น
← กลับไปที่ Spring Security 6 & JWT Authentication