| 
									
										
										
										
											2021-08-16 13:09:42 -06:00
										 |  |  | [[reactive-logout]] | 
					
						
							|  |  |  | = Logout | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Spring Security provides a logout endpoint by default. | 
					
						
							|  |  |  | Once logged in, you can `GET /logout` to see a default logout confirmation page, or you can `POST /logout` to initiate logout. | 
					
						
							|  |  |  | This will: | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | - clear the `ServerCsrfTokenRepository`, `ServerSecurityContextRepository`, and | 
					
						
							|  |  |  | - redirect back to the login page | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | Often, you will want to also invalidate the session on logout. | 
					
						
							|  |  |  | To achieve this, you can add the `WebSessionServerLogoutHandler` to your logout configuration, like so: | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:31:35 -05:00
										 |  |  | [tabs] | 
					
						
							|  |  |  | ====== | 
					
						
							|  |  |  | Java:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2022-02-10 23:07:20 +07:00
										 |  |  | [source,java,role="primary"] | 
					
						
							| 
									
										
										
										
											2021-08-16 13:09:42 -06:00
										 |  |  | ---- | 
					
						
							|  |  |  | @Bean | 
					
						
							|  |  |  | SecurityWebFilterChain http(ServerHttpSecurity http) throws Exception { | 
					
						
							|  |  |  |     DelegatingServerLogoutHandler logoutHandler = new DelegatingServerLogoutHandler( | 
					
						
							| 
									
										
										
										
											2024-03-04 15:42:05 -07:00
										 |  |  |             new SecurityContextServerLogoutHandler(), new WebSessionServerLogoutHandler() | 
					
						
							| 
									
										
										
										
											2021-08-16 13:09:42 -06:00
										 |  |  |     ); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     http | 
					
						
							|  |  |  |         .authorizeExchange((exchange) -> exchange.anyExchange().authenticated()) | 
					
						
							|  |  |  |         .logout((logout) -> logout.logoutHandler(logoutHandler)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |     return http.build(); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2022-02-10 23:07:20 +07:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-06-18 21:31:35 -05:00
										 |  |  | Kotlin:: | 
					
						
							|  |  |  | + | 
					
						
							| 
									
										
										
										
											2022-02-10 23:07:20 +07:00
										 |  |  | [source,kotlin,role="secondary"] | 
					
						
							|  |  |  | ---- | 
					
						
							|  |  |  | @Bean | 
					
						
							|  |  |  | fun http(http: ServerHttpSecurity): SecurityWebFilterChain { | 
					
						
							|  |  |  |     val customLogoutHandler = DelegatingServerLogoutHandler( | 
					
						
							| 
									
										
										
										
											2024-03-04 15:42:05 -07:00
										 |  |  |         SecurityContextServerLogoutHandler(), WebSessionServerLogoutHandler() | 
					
						
							| 
									
										
										
										
											2022-02-10 23:07:20 +07:00
										 |  |  |     ) | 
					
						
							|  |  |  |      | 
					
						
							|  |  |  |     return http { | 
					
						
							|  |  |  |         authorizeExchange { | 
					
						
							|  |  |  |             authorize(anyExchange, authenticated) | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |         logout { | 
					
						
							|  |  |  |             logoutHandler = customLogoutHandler | 
					
						
							|  |  |  |         } | 
					
						
							|  |  |  |     } | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | ---- | 
					
						
							| 
									
										
										
										
											2023-06-18 21:31:35 -05:00
										 |  |  | ====== |