logout.adoc 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. [[jc-logout]]
  2. = Handling Logouts
  3. This section covers how to customize the handling of logouts.
  4. [[logout-java-configuration]]
  5. == Logout Java/Kotlin Configuration
  6. When using the `{security-api-url}org/springframework/security/config/annotation/web/builders/HttpSecurity.html[HttpSecurity]` bean, logout capabilities are automatically applied.
  7. The default is that accessing the URL `/logout` logs the user out by:
  8. - Invalidating the HTTP Session
  9. - Cleaning up any RememberMe authentication that was configured
  10. - Clearing the `SecurityContextHolder`
  11. - Clearing the `SecurityContextRepository`
  12. - Redirecting to `/login?logout`
  13. Similar to configuring login capabilities, however, you also have various options to further customize your logout requirements:
  14. .Logout Configuration
  15. [tabs]
  16. ======
  17. Java::
  18. +
  19. [source,java,role="primary"]
  20. ----
  21. public SecurityFilterChain filterChain(HttpSecurity http) {
  22. http
  23. .logout(logout -> logout // <1>
  24. .logoutUrl("/my/logout") // <2>
  25. .logoutSuccessUrl("/my/index") // <3>
  26. .logoutSuccessHandler(logoutSuccessHandler) // <4>
  27. .invalidateHttpSession(true) // <5>
  28. .addLogoutHandler(logoutHandler) // <6>
  29. .deleteCookies(cookieNamesToClear) // <7>
  30. )
  31. ...
  32. }
  33. ----
  34. Kotlin::
  35. +
  36. [source,kotlin,role="secondary"]
  37. -----
  38. open fun filterChain(http: HttpSecurity): SecurityFilterChain {
  39. http {
  40. logout { // <1>
  41. logoutUrl = "/my/logout" // <2>
  42. logoutSuccessUrl = "/my/index" // <3>
  43. logoutSuccessHandler = customLogoutSuccessHandler // <4>
  44. invalidateHttpSession = true // <5>
  45. addLogoutHandler(logoutHandler) // <6>
  46. deleteCookies(cookieNamesToClear) // <7>
  47. }
  48. }
  49. // ...
  50. }
  51. -----
  52. ======
  53. <1> Provides logout support.
  54. <2> The URL that triggers log out to occur (the default is `/logout`).
  55. If CSRF protection is enabled (the default), the request must also be a POST.
  56. For more information, see {security-api-url}org/springframework/security/config/annotation/web/configurers/LogoutConfigurer.html#logoutUrl-java.lang.String-[`logoutUrl(java.lang.String logoutUrl)`].
  57. <3> The URL to which to redirect after logout has occurred.
  58. The default is `/login?logout`.
  59. For more information, see {security-api-url}org/springframework/security/config/annotation/web/configurers/LogoutConfigurer.html#logoutSuccessUrl-java.lang.String-[`logoutSuccessUrl(java.lang.String logoutSuccessUrl)`].
  60. <4> Let's you specify a custom `LogoutSuccessHandler`.
  61. If this is specified, `logoutSuccessUrl()` is ignored.
  62. For more information, see {security-api-url}org/springframework/security/config/annotation/web/configurers/LogoutConfigurer.html#logoutSuccessHandler-org.springframework.security.web.authentication.logout.LogoutSuccessHandler-[`LogoutSuccessHandler`].
  63. <5> Specify whether to invalidate the `HttpSession` at the time of logout.
  64. This is *true* by default.
  65. Configures the `SecurityContextLogoutHandler` under the covers.
  66. For more information, see {security-api-url}org/springframework/security/config/annotation/web/configurers/LogoutConfigurer.html#invalidateHttpSession-boolean-[`invalidateHttpSession(boolean invalidateHttpSession)`].
  67. <6> Adds a `LogoutHandler`.
  68. By default, `SecurityContextLogoutHandler` is added as the last `LogoutHandler`.
  69. <7> Lets specifying the names of cookies be removed on logout success.
  70. This is a shortcut for adding a `CookieClearingLogoutHandler` explicitly.
  71. [NOTE]
  72. ====
  73. Logouts can also be configured by using the XML Namespace notation.
  74. 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.
  75. ====
  76. Generally, to customize logout functionality, you can add
  77. `{security-api-url}org/springframework/security/web/authentication/logout/LogoutHandler.html[LogoutHandler]`
  78. or
  79. `{security-api-url}org/springframework/security/web/authentication/logout/LogoutSuccessHandler.html[LogoutSuccessHandler]`
  80. implementations.
  81. For many common scenarios, these handlers are applied under the
  82. covers when using the fluent API.
  83. [[ns-logout]]
  84. == Logout XML Configuration
  85. The `logout` element adds support for logging out by navigating to a particular URL.
  86. The default logout URL is `/logout`, but you can set it to something else by setting the `logout-url` attribute.
  87. You can find more information on other available attributes in the namespace appendix.
  88. [[jc-logout-handler]]
  89. == LogoutHandler
  90. Generally, `{security-api-url}org/springframework/security/web/authentication/logout/LogoutHandler.html[LogoutHandler]`
  91. implementations indicate classes that are able to participate in logout handling.
  92. They are expected to be invoked to perform necessary clean-up.
  93. As a result, they should
  94. not throw exceptions.
  95. Spring Security provides various implementations:
  96. - {security-api-url}org/springframework/security/web/authentication/rememberme/PersistentTokenBasedRememberMeServices.html[PersistentTokenBasedRememberMeServices]
  97. - {security-api-url}org/springframework/security/web/authentication/rememberme/TokenBasedRememberMeServices.html[TokenBasedRememberMeServices]
  98. - {security-api-url}org/springframework/security/web/authentication/logout/CookieClearingLogoutHandler.html[CookieClearingLogoutHandler]
  99. - {security-api-url}org/springframework/security/web/csrf/CsrfLogoutHandler.html[CsrfLogoutHandler]
  100. - {security-api-url}org/springframework/security/web/authentication/logout/SecurityContextLogoutHandler.html[SecurityContextLogoutHandler]
  101. - {security-api-url}org/springframework/security/web/authentication/logout/HeaderWriterLogoutHandler.html[HeaderWriterLogoutHandler]
  102. See xref:servlet/authentication/rememberme.adoc#remember-me-impls[Remember-Me Interfaces and Implementations] for details.
  103. Instead of providing `LogoutHandler` implementations directly, the fluent API also provides shortcuts that provide the respective `LogoutHandler` implementations under the covers.
  104. For example, `deleteCookies()` lets you specify the names of one or more cookies to be removed on logout success.
  105. This is a shortcut compared to adding a `CookieClearingLogoutHandler`.
  106. [[jc-logout-success-handler]]
  107. == LogoutSuccessHandler
  108. The `LogoutSuccessHandler` is called after a successful logout by the `LogoutFilter`, to handle (for example)
  109. redirection or forwarding to the appropriate destination.
  110. Note that the interface is almost the same as the `LogoutHandler` but may raise an exception.
  111. Spring Security provides the following implementations:
  112. - {security-api-url}org/springframework/security/web/authentication/logout/SimpleUrlLogoutSuccessHandler.html[SimpleUrlLogoutSuccessHandler]
  113. - HttpStatusReturningLogoutSuccessHandler
  114. As mentioned earlier, you need not specify the `SimpleUrlLogoutSuccessHandler` directly.
  115. Instead, the fluent API provides a shortcut by setting the `logoutSuccessUrl()`.
  116. This sets up the `SimpleUrlLogoutSuccessHandler` under the covers.
  117. The provided URL is redirected to after a logout has occurred.
  118. The default is `/login?logout`.
  119. The `HttpStatusReturningLogoutSuccessHandler` can be interesting in REST API type scenarios.
  120. Instead of redirecting to a URL upon the successful logout, this `LogoutSuccessHandler` lets you provide a plain HTTP status code to be returned.
  121. If not configured, a status code 200 is returned by default.
  122. [[jc-logout-references]]
  123. == Further Logout-Related References
  124. - xref:servlet/authentication/session-management.adoc#properly-clearing-authentication[Properly Clearing Authentication When Explicit Save Is Enabled]
  125. - <<ns-logout, Logout Handling>>
  126. - xref:servlet/test/mockmvc/logout.adoc#test-logout[Testing Logout]
  127. - xref:servlet/integrations/servlet-api.adoc#servletapi-logout[`HttpServletRequest.logout()`]
  128. - xref:servlet/authentication/rememberme.adoc#remember-me-impls[Remember-Me Interfaces and Implementations]
  129. - xref:servlet/exploits/csrf.adoc#servlet-considerations-csrf-logout[Logging Out] in section CSRF Caveats
  130. - Documentation for the xref:servlet/appendix/namespace/http.adoc#nsa-logout[logout element] in the Spring Security XML Namespace section