What is Chain of Responsibility? How do Servlet Filters and the Spring Security filter chain use it?
Short answer
Chain of Responsibility: a request goes through a chain of handlers, and each handler either handles it or passes it to the next. The sender doesn't know who will handle it; adding a step = adding a handler to the chain, without touching the others.
3 points to say:
- Each handler handles or passes on; stopping the chain is a deliberate decision (401, 429).
- Real examples: the Servlet Filter chain, Spring Security
SecurityFilterChain,HandlerInterceptor, validation / discount rule chains. - Traps: order matters, and forgetting
chain.doFiltersilently swallows the request.
Keep reading the deep dive
Create a free account to read the full explanation and follow-up questions, mark questions as studied and review them on a schedule.