logout.adoc 8.1 KB

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