logout.adoc 8.1 KB

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