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

การแมปข้อมูลกำกับ JWT ไปยังสิทธิ์ของ Spring

เรียนรู้ว่าเซิร์ฟเวอร์ทรัพยากรแปลงข้อมูลกำกับ JWT เป็น GrantedAuthorities ของ Spring Security เพื่อควบคุมการเข้าถึงอย่างละเอียดด้วย JwtAuthenticationConverter อย่างไร

บทเรียน 4 จาก 413 ขั้นตอน

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

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

From Claims to Authorities

A resource server validates a JWT, but to enforce access it needs Spring GrantedAuthority objects. The bridge between raw claims and authorities is the JwtAuthenticationConverter.

The Default Scope Mapping

By default Spring reads the scope or scp claim, splits it on spaces, and prefixes each value with SCOPE_. So a scope of read becomes the authority SCOPE_read.

// scope: 'read write'  ->  SCOPE_read, SCOPE_write

Checking Scope Authorities

You can require these authorities in your security config or with annotations.

http.authorizeHttpRequests(auth -> auth
    .requestMatchers('/api/data').hasAuthority('SCOPE_read'));

The Problem with Roles

Many identity providers put roles in a custom claim like roles or realm_access.roles, not in scope. The default converter ignores those, so you must customize it.

Building a Custom Converter

Create a JwtGrantedAuthoritiesConverter and point it at the claim that holds your roles.

JwtGrantedAuthoritiesConverter c = new JwtGrantedAuthoritiesConverter();
c.setAuthoritiesClaimName('roles');
c.setAuthorityPrefix('ROLE_');

Wrapping in JwtAuthenticationConverter

Wrap the authorities converter inside a JwtAuthenticationConverter, which produces the final authentication token.

JwtAuthenticationConverter conv = new JwtAuthenticationConverter();
conv.setJwtGrantedAuthoritiesConverter(c);

Registering the Converter

Tell the resource server to use your converter inside the JWT configuration.

http.oauth2ResourceServer(o -> o
    .jwt(j -> j.jwtAuthenticationConverter(conv)));

Nested Claims

Some providers nest roles, e.g. Keycloak uses realm_access.roles. The simple converter cannot read nested paths, so write a lambda converter that drills into the structure.

Converter<Jwt, Collection<GrantedAuthority>> conv = jwt -> {
    Map<String,Object> realm = jwt.getClaim('realm_access');
    List<String> roles = (List<String>) realm.get('roles');
    return roles.stream()
        .map(r -> new SimpleGrantedAuthority('ROLE_' + r))
        .collect(Collectors.toList());
};

Combining Scopes and Roles

You may want both scope-based and role-based authorities. Merge two converters' results so a single principal carries both SCOPE_ and ROLE_ authorities.

Customizing the Principal Name

By default the principal name is the sub claim. Override setPrincipalClaimName if you prefer to identify users by, say, preferred_username.

conv.setPrincipalClaimName('preferred_username');

Verifying the Mapping

Test with a mock JWT that carries the roles claim and assert the request succeeds only when the expected authority is present.

mockMvc.perform(get('/api/admin')
    .with(jwt().authorities(new SimpleGrantedAuthority('ROLE_admin'))))
    .andExpect(status().isOk());

Quick Check

Test your understanding of claim-to-authority mapping.

Recap

You learned to map JWT claims to Spring authorities:

  • Default mapping turns scope into SCOPE_ authorities
  • Use JwtGrantedAuthoritiesConverter to read custom role claims
  • Write a lambda converter for nested claims like realm_access.roles
  • Register it via jwtAuthenticationConverter

This gives your resource server precise, claim-driven access control.

เริ่มต้นได้ฟรี

เรียนรู้ Java ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

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

บทเรียน “การแมปข้อมูลกำกับ JWT ไปยังสิทธิ์ของ Spring” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การแมปข้อมูลกำกับ JWT ไปยังสิทธิ์ของ Spring”

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

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

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

บทเรียน “การแมปข้อมูลกำกับ JWT ไปยังสิทธิ์ของ Spring” ใช้เวลานานแค่ไหน

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

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

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

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

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