logout.adoc 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. [[reactive-logout]]
  2. = Logout
  3. Spring Security provides a logout endpoint by default.
  4. Once logged in, you can `GET /logout` to see a default logout confirmation page, or you can `POST /logout` to initiate logout.
  5. This will:
  6. - clear the `ServerCsrfTokenRepository`, `ServerSecurityContextRepository`, and
  7. - redirect back to the login page
  8. Often, you will want to also invalidate the session on logout.
  9. To achieve this, you can add the `WebSessionServerLogoutHandler` to your logout configuration, like so:
  10. .Java
  11. [source,java,role="primary"]
  12. ----
  13. @Bean
  14. SecurityWebFilterChain http(ServerHttpSecurity http) throws Exception {
  15. DelegatingServerLogoutHandler logoutHandler = new DelegatingServerLogoutHandler(
  16. new WebSessionServerLogoutHandler(), new SecurityContextServerLogoutHandler()
  17. );
  18. http
  19. .authorizeExchange((exchange) -> exchange.anyExchange().authenticated())
  20. .logout((logout) -> logout.logoutHandler(logoutHandler));
  21. return http.build();
  22. }
  23. ----
  24. .Kotlin
  25. [source,kotlin,role="secondary"]
  26. ----
  27. @Bean
  28. fun http(http: ServerHttpSecurity): SecurityWebFilterChain {
  29. val customLogoutHandler = DelegatingServerLogoutHandler(
  30. WebSessionServerLogoutHandler(), SecurityContextServerLogoutHandler()
  31. )
  32. return http {
  33. authorizeExchange {
  34. authorize(anyExchange, authenticated)
  35. }
  36. logout {
  37. logoutHandler = customLogoutHandler
  38. }
  39. }
  40. }
  41. ----