2
0

logout.adoc 8.5 KB

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