ทำความเข้าใจสายโซ่ตัวกรองของ Spring Security
ดูเบื้องหลัง Spring Security 6 เพื่อทำความเข้าใจว่าสายโซ่ตัวกรองของเซิร์ฟเล็ตประมวลผลคำขอแต่ละรายการอย่างไร และการตรวจสอบสิทธิ์เข้ามาเกี่ยวข้องตรงจุดใด
ทำความเข้าใจสายโซ่ตัวกรองของ Spring Security เป็นบทเรียน Spring Security 6 & JWT Authentication ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Spring Security 6 & JWT Authentication และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
How Requests Get Secured
So how does every request actually get checked? The security filter chain — a series of servlet filters Spring slots in before your controllers.
What Is a Servlet Filter?
A servlet Filter intercepts HTTP requests and responses before they reach your code. Spring Security is built almost entirely from these filters.
The DelegatingFilterProxy
The real servlet filter, DelegatingFilterProxy, hands each request to a Spring-managed bean — bridging the servlet world and the Spring context.
The FilterChainProxy
Behind that proxy sits FilterChainProxy, which holds one or more SecurityFilterChain instances and routes each request to the one that matches.
Key Filters in Order
Filters run in a fixed order: SecurityContextHolderFilter loads context, the auth filter handles login, and AuthorizationFilter enforces access rules.
Defining a SecurityFilterChain Bean
In Spring Security 6 you configure everything by declaring a SecurityFilterChain bean — the modern replacement for WebSecurityConfigurerAdapter. See below.
@Bean
SecurityFilterChain chain(HttpSecurity http) throws Exception {
http.authorizeHttpRequests(a -> a.anyRequest().authenticated())
.formLogin(Customizer.withDefaults());
return http.build();
}Where the SecurityContext Lives
After login, the Authentication is stored in the SecurityContext and stays reachable via SecurityContextHolder for the rest of the request.
Authentication auth = SecurityContextHolder.getContext().getAuthentication();Permitting Some Paths
Let public paths through while securing the rest — all on the same chain. The code uses permitAll() for /public and authenticated() for everything else.
http.authorizeHttpRequests(a -> a
.requestMatchers('/public/**').permitAll()
.anyRequest().authenticated());Multiple Filter Chains
Register several SecurityFilterChain beans with securityMatcher so API and web paths get different rules. The first matching chain wins.
http.securityMatcher('/api/**');Adding a Custom Filter
Slot your own filter at a precise position with addFilterBefore — the foundation for the JWT processing you'll build later in this course.
http.addFilterBefore(myFilter, UsernamePasswordAuthenticationFilter.class);Why This Matters
Knowing the chain explains why ordering matters, where auth versus authz happens, and exactly where a custom JWT filter has to plug in.
Quick Check
In Spring Security 6, how do you define your security configuration?
Recap
Recap: requests flow DelegatingFilterProxy to FilterChainProxy to SecurityFilterChain; filters run in order, auth then authz, and addFilterBefore inserts custom ones.
คำถามที่พบบ่อย
บทเรียน “ทำความเข้าใจสายโซ่ตัวกรองของ Spring Security” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “ทำความเข้าใจสายโซ่ตัวกรองของ Spring Security” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Spring Security 6 & JWT Authentication ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Spring Security 6 & JWT Authentication มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “ทำความเข้าใจสายโซ่ตัวกรองของ Spring Security”
ดูเบื้องหลัง Spring Security 6 เพื่อทำความเข้าใจว่าสายโซ่ตัวกรองของเซิร์ฟเล็ตประมวลผลคำขอแต่ละรายการอย่างไร และการตรวจสอบสิทธิ์เข้ามาเกี่ยวข้องตรงจุดใด คุณปฏิบัติ Spring Security 6 & JWT Authentication ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Spring Security 6 & JWT Authentication หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Spring Security 6 & JWT Authentication บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน
บทเรียน “ทำความเข้าใจสายโซ่ตัวกรองของ Spring Security” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Spring Security 6 & JWT Authentication นี้ได้ไหม
ได้ บทเรียน Spring Security 6 & JWT Authentication ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- บทนำสู่ Spring Security 6
- การตั้งค่าโปรเจกต์และการพึ่งพา
- การยืนยันตัวตนผู้ใช้ในหน่วยความจำ
- ทำความเข้าใจสายโซ่ตัวกรองของ Spring Security