LogoutFilter is the Spring Security filter that detects a logout request and runs the logout process. When a request matches the logout URL, it calls every configured LogoutHandler to clear the session, security context and cookies, and then calls a LogoutSuccessHandler to decide what the client sees, usually a redirect to the login page.
You rarely create it yourself. The logout() configuration in HttpSecurity builds it and places it in the security filter chain.
LogoutFilter is in the package org.springframework.security.web.authentication.logout. It extends GenericFilterBean, not OncePerRequestFilter. It runs after CsrfFilter and before the authentication filters such as UsernamePasswordAuthenticationFilter, so a logout request is handled before any login logic.
On each request the filter does the following:
RequestMatcher. If not, it passes the request on.Authentication from the SecurityContext.LogoutHandler in order.LogoutSuccessHandler, which writes the response. The filter chain stops there.With the default configuration in Spring Security 6:
/logout.POST request with a valid CSRF token. A plain link (GET) will not log the user out. If you disable CSRF, /logout accepts any HTTP method./login?logout.A logout button must therefore be a form.
Template engines such as Thymeleaf add the CSRF field to forms automatically.
Customize logout through http.logout(...). This is the lambda DSL used in Spring Security 6.
logoutSuccessUrl creates a SimpleUrlLogoutSuccessHandler. If you set a custom logoutSuccessHandler, it replaces that URL setting.
A LogoutHandler performs cleanup and has no control over the response. Use it for work such as removing server-side tokens or writing an audit record.
Register it with .logout(l -> l.addLogoutHandler(auditLogoutHandler)). Use a real logger instead of System.out in production. The handler may receive a null authentication when the session has already expired, so always check.
For a single-page app or REST API, a redirect is wrong. Return a status code instead with HttpStatusReturningLogoutSuccessHandler.
With OpenID Connect login, use OidcClientInitiatedLogoutSuccessHandler to end the session at the identity provider as well. Logging out only of your application leaves the user signed in at the provider.
You can also ask the browser to clear cached data on logout with a Clear-Site-Data header, using HeaderWriterLogoutHandler with ClearSiteDataHeaderWriter.
Since CSRF protection is on by default, Spring Security requires POST for logout. Submit a form with a CSRF token, or disable CSRF only if you understand the risk.
LogoutHandler instances do the cleanup and may be several. The LogoutSuccessHandler runs once at the end and produces the response.
No. A stateless JWT stays valid until it expires. Use short lifetimes, a deny list or token revocation at the identity provider.
Yes. Call logoutUrl("/signout"), or use logoutRequestMatcher(...) for a custom matching rule.
LogoutFilter intercepts logout requests, runs the LogoutHandler chain to invalidate the session and clear the security context, and then delegates to a LogoutSuccessHandler for the response. Configure it through HttpSecurity.logout(...), remember that logout needs a POST with CSRF by default, and use success handlers for redirects, status codes or OIDC logout.