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

การกำหนดสิทธิ์ตามบทบาทด้วย Granted Authorities

หลังจากตรวจสอบสิทธิ์ผู้ใช้จากฐานข้อมูลแล้ว เรียนรู้การกำหนดสิทธิ์ด้วยบทบาทและสิทธิ์ เพื่อรักษาความปลอดภัยให้ปลายทางและเมธอดใน Spring Security

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

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

Authentication vs Authorization

You can now load users from a database and verify passwords. That is authentication (who you are). The next question is authorization (what you may do), driven by roles and authorities.

Authorities and Roles

Spring represents permissions as GrantedAuthority objects. A role is just an authority with a ROLE_ prefix, e.g. ROLE_ADMIN.

Assigning Authorities to a User

When building your UserDetails, attach the authorities the user holds.

User.withUsername('alice')
    .password(encoded)
    .roles('ADMIN', 'USER')
    .build();

Securing URLs by Role

In the filter chain, restrict paths with hasRole. Spring adds the ROLE_ prefix for you here.

http.authorizeHttpRequests(a -> a
    .requestMatchers('/admin/**').hasRole('ADMIN')
    .anyRequest().authenticated());

Requiring Specific Authorities

For finer control use hasAuthority, which matches the authority string exactly with no prefix added.

http.authorizeHttpRequests(a -> a
    .requestMatchers('/reports/**').hasAuthority('REPORT_READ'));

Multiple Allowed Roles

hasAnyRole permits access if the user has at least one of several roles.

http.authorizeHttpRequests(a -> a
    .requestMatchers('/staff/**').hasAnyRole('ADMIN', 'MANAGER'));

Method-Level Security

Enable annotation-based security to protect service methods, not just URLs.

@EnableMethodSecurity
@Configuration
public class SecurityConfig { }

Using @PreAuthorize

@PreAuthorize runs a SpEL expression before the method executes, blocking unauthorized callers.

@PreAuthorize("hasRole('ADMIN')")
public void deleteUser(Long id) { }

Checking the Current User

SpEL can reference the authenticated principal, e.g. to allow users to edit only their own data.

@PreAuthorize("#username == authentication.name")
public void updateProfile(String username) { }

Mapping DB Roles to Authorities

In your UserDetailsService, convert role rows from the database into SimpleGrantedAuthority objects so authorization rules apply.

var auths = roles.stream()
    .map(r -> new SimpleGrantedAuthority('ROLE_' + r))
    .toList();

Putting It Together

The full picture: authenticate from the DB, map roles to authorities, secure URLs with hasRole/hasAuthority, and protect methods with @PreAuthorize.

Quick Check

What is the difference between hasRole('ADMIN') and hasAuthority('ADMIN')?

Recap

You can now control what authenticated users may do:

  • Roles are authorities with a ROLE_ prefix
  • hasRole/hasAnyRole vs exact hasAuthority
  • @EnableMethodSecurity + @PreAuthorize for method-level rules
  • Map DB roles to SimpleGrantedAuthority in your UserDetailsService

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

บทเรียน “การกำหนดสิทธิ์ตามบทบาทด้วย Granted Authorities” ฟรีหรือไม่

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

คุณจะเรียนรู้อะไรในบทเรียน “การกำหนดสิทธิ์ตามบทบาทด้วย Granted Authorities”

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

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

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

บทเรียน “การกำหนดสิทธิ์ตามบทบาทด้วย Granted Authorities” ใช้เวลานานแค่ไหน

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

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

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

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

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