2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								[[jc-logout]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-31 02:52:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								= Handling Logouts
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[[logout-java-configuration]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-31 02:52:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								== Logout Java/Kotlin Configuration
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2022-02-08 23:12:10 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								When injecting the `{security-api-url}org/springframework/security/config/annotation/web/builders/HttpSecurity.html[HttpSecurity]` bean, logout capabilities are automatically applied.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								The default is that accessing the URL `/logout` will log the user out by:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- Invalidating the HTTP Session
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- Cleaning up any RememberMe authentication that was configured
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- Clearing the `SecurityContextHolder`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- Redirect to `/login?logout`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Similar to configuring login capabilities, however, you also have various options to further customize your logout requirements:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-06 18:45:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								.Logout Configuration
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								====
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								.Java
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[source,java,role="primary"]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
									
										
										
										
											2022-02-08 23:12:10 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								public SecurityFilterChain filterChain(HttpSecurity http) {
							 
						 
					
						
							
								
									
										
										
										
											2020-01-10 20:10:36 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    http
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        .logout(logout -> logout                                                // <1>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .logoutUrl("/my/logout")                                            // <2>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .logoutSuccessUrl("/my/index")                                      // <3>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .logoutSuccessHandler(logoutSuccessHandler)                         // <4>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .invalidateHttpSession(true)                                        // <5>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .addLogoutHandler(logoutHandler)                                    // <6>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            .deleteCookies(cookieNamesToClear)                                  // <7>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        )
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								        ...
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2020-07-06 18:45:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								.Kotlin
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[source,kotlin,role="secondary"]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-----
							 
						 
					
						
							
								
									
										
										
										
											2022-02-08 23:12:10 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								open fun filterChain(http: HttpSecurity): SecurityFilterChain {
							 
						 
					
						
							
								
									
										
										
										
											2020-07-06 18:45:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								    http {
							 
						 
					
						
							
								
									
										
										
										
											2022-08-26 01:26:32 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								        logout {                                                  // <1>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            logoutUrl = "/my/logout"                              // <2>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            logoutSuccessUrl = "/my/index"                        // <3>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            logoutSuccessHandler = customLogoutSuccessHandler     // <4>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            invalidateHttpSession = true                          // <5>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            addLogoutHandler(logoutHandler)                       // <6>
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								            deleteCookies(cookieNamesToClear)                     // <7>
							 
						 
					
						
							
								
									
										
										
										
											2020-07-06 18:45:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								        }
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								    }
							 
						 
					
						
							
								
									
										
										
										
											2022-02-08 23:12:10 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								    // ...
							 
						 
					
						
							
								
									
										
										
										
											2020-07-06 18:45:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								}
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								-----
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								====
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								<1> Provides logout support.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								<2> The URL that triggers log out to occur (default is `/logout`).
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If CSRF protection is enabled (default), then the request must also be a POST.
							 
						 
					
						
							
								
									
										
										
										
											2021-09-15 17:30:06 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								For more information, please consult the {security-api-url}org/springframework/security/config/annotation/web/configurers/LogoutConfigurer.html#logoutUrl-java.lang.String-[Javadoc].
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								<3> The URL to redirect to after logout has occurred.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The default is `/login?logout`.
							 
						 
					
						
							
								
									
										
										
										
											2021-09-15 17:30:06 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								For more information, please consult the {security-api-url}org/springframework/security/config/annotation/web/configurers/LogoutConfigurer.html#logoutSuccessUrl-java.lang.String-[Javadoc].
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								<4> Let's you specify a custom `LogoutSuccessHandler`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If this is specified, `logoutSuccessUrl()` is ignored.
							 
						 
					
						
							
								
									
										
										
										
											2021-09-15 17:30:06 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								For more information, please consult the {security-api-url}org/springframework/security/config/annotation/web/configurers/LogoutConfigurer.html#logoutSuccessHandler-org.springframework.security.web.authentication.logout.LogoutSuccessHandler-[Javadoc].
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								<5> Specify whether to invalidate the `HttpSession` at the time of logout.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This is *true* by default.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Configures the `SecurityContextLogoutHandler` under the covers.
							 
						 
					
						
							
								
									
										
										
										
											2021-09-15 17:30:06 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								For more information, please consult the {security-api-url}org/springframework/security/config/annotation/web/configurers/LogoutConfigurer.html#invalidateHttpSession-boolean-[Javadoc].
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								<6> Adds a `LogoutHandler`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`SecurityContextLogoutHandler` is added as the last `LogoutHandler` by default.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								<7> Allows specifying the names of cookies to be removed on logout success.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This is a shortcut for adding a `CookieClearingLogoutHandler` explicitly.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[NOTE]
							 
						 
					
						
							
								
									
										
										
										
											2020-02-29 02:14:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								====
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								Logouts can of course also be configured using the XML Namespace notation.
							 
						 
					
						
							
								
									
										
										
										
											2021-11-02 04:50:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Please see the documentation for the xref:servlet/appendix/namespace/http.adoc#nsa-logout[ logout element] in the Spring Security XML Namespace section for further details.
							 
						 
					
						
							
								
									
										
										
										
											2020-02-29 02:14:41 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								====
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Generally, in order to customize logout functionality, you can add
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`{security-api-url}org/springframework/security/web/authentication/logout/LogoutHandler.html[LogoutHandler]`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								and/or
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								`{security-api-url}org/springframework/security/web/authentication/logout/LogoutSuccessHandler.html[LogoutSuccessHandler]`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								implementations.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								For many common scenarios, these handlers are applied under the
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								covers when using the fluent API.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[[ns-logout]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-31 02:52:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								== Logout XML Configuration
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								The `logout` element adds support for logging out by navigating to a particular URL.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The default logout URL is `/logout`, but you can set it to something else using the `logout-url` attribute.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								More information on other available attributes may be found in the namespace appendix.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[[jc-logout-handler]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-31 02:52:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								== LogoutHandler
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Generally, `{security-api-url}org/springframework/security/web/authentication/logout/LogoutHandler.html[LogoutHandler]`
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								implementations indicate classes that are able to participate in logout handling.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								They are expected to be invoked to perform necessary clean-up.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								As such they should
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								not throw exceptions.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Various implementations are provided:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- {security-api-url}org/springframework/security/web/authentication/rememberme/PersistentTokenBasedRememberMeServices.html[PersistentTokenBasedRememberMeServices]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- {security-api-url}org/springframework/security/web/authentication/rememberme/TokenBasedRememberMeServices.html[TokenBasedRememberMeServices]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- {security-api-url}org/springframework/security/web/authentication/logout/CookieClearingLogoutHandler.html[CookieClearingLogoutHandler]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- {security-api-url}org/springframework/security/web/csrf/CsrfLogoutHandler.html[CsrfLogoutHandler]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- {security-api-url}org/springframework/security/web/authentication/logout/SecurityContextLogoutHandler.html[SecurityContextLogoutHandler]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- {security-api-url}org/springframework/security/web/authentication/logout/HeaderWriterLogoutHandler.html[HeaderWriterLogoutHandler]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2021-07-31 06:02:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								Please see xref:servlet/authentication/rememberme.adoc#remember-me-impls[Remember-Me Interfaces and Implementations] for details.
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Instead of providing `LogoutHandler` implementations directly, the fluent API also provides shortcuts that provide the respective `LogoutHandler` implementations under the covers.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								E.g. `deleteCookies()` allows specifying the names of one or more cookies to be removed on logout success.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This is a shortcut compared to adding a `CookieClearingLogoutHandler`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[[jc-logout-success-handler]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-31 02:52:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								== LogoutSuccessHandler
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `LogoutSuccessHandler` is called after a successful logout by the `LogoutFilter`, to handle e.g.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								redirection or forwarding to the appropriate destination.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Note that the interface is almost the same as the `LogoutHandler` but may raise an exception.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The following implementations are provided:
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- {security-api-url}org/springframework/security/web/authentication/logout/SimpleUrlLogoutSuccessHandler.html[SimpleUrlLogoutSuccessHandler]
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- HttpStatusReturningLogoutSuccessHandler
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								As mentioned above, you don't need to specify the `SimpleUrlLogoutSuccessHandler` directly.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Instead, the fluent API provides a shortcut by setting the `logoutSuccessUrl()`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								This will setup the `SimpleUrlLogoutSuccessHandler` under the covers.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The provided URL will be redirected to after a logout has occurred.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The default is `/login?logout`.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								The `HttpStatusReturningLogoutSuccessHandler` can be interesting in REST API type scenarios.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								Instead of redirecting to a URL upon the successful logout, this `LogoutSuccessHandler` allows you to provide a plain HTTP status code to be returned.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								If not configured a status code 200 will be returned by default.
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								[[jc-logout-references]]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-31 02:52:15 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								== Further Logout-Related References
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								
							 
						 
					
						
							
								
									
										
										
										
											2023-03-23 02:57:26 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								- xref:servlet/authentication/session-management.adoc#properly-clearing-authentication[Properly Clearing Authentication When Explicit Save Is Enabled]
							 
						 
					
						
							
								
									
										
										
										
											2019-09-22 14:56:30 +08:00 
										
									 
								 
							 
							
								
							 
							
								 
							
							
								- <<ns-logout, Logout Handling>>
							 
						 
					
						
							
								
									
										
										
										
											2021-10-30 02:34:29 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								- xref:servlet/test/mockmvc/logout.adoc#test-logout[ Testing Logout]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-31 05:56:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								- xref:servlet/integrations/servlet-api.adoc#servletapi-logout[ HttpServletRequest.logout()]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-31 06:02:44 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								- xref:servlet/authentication/rememberme.adoc#remember-me-impls[Remember-Me Interfaces and Implementations]
							 
						 
					
						
							
								
									
										
										
										
											2021-07-31 05:56:54 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								- xref:servlet/exploits/csrf.adoc#servlet-considerations-csrf-logout[ Logging Out] in section CSRF Caveats
							 
						 
					
						
							
								
							 
							
								
							 
							
								 
							
							
								- Section xref:servlet/authentication/cas.adoc#cas-singlelogout[ Single Logout] (CAS protocol)
							 
						 
					
						
							
								
									
										
										
										
											2021-11-02 04:50:25 +08:00 
										
									 
								 
							 
							
								
									
										 
								
							 
							
								 
							
							
								- Documentation for the xref:servlet/appendix/namespace/http.adoc#nsa-logout[ logout element] in the Spring Security XML Namespace section