mirror of
				https://github.com/spring-projects/spring-security.git
				synced 2025-10-31 06:38:42 +00:00 
			
		
		
		
	SEC-2055: SaveContextServletOutputStream flush/close delegates to original ServletOutputStream instead of using super
This commit is contained in:
		
							parent
							
								
									abe5e4af48
								
							
						
					
					
						commit
						dbc88f3226
					
				| @ -203,16 +203,14 @@ public abstract class SaveContextOnUpdateOrErrorResponseWrapper extends HttpServ | |||||||
|             this.delegate.write(b); |             this.delegate.write(b); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |  | ||||||
|         public void flush() throws IOException { |         public void flush() throws IOException { | ||||||
|             doSaveContext(); |             doSaveContext(); | ||||||
|             super.flush(); |             delegate.flush(); | ||||||
|         } |         } | ||||||
| 
 | 
 | ||||||
|         @Override |  | ||||||
|         public void close() throws IOException { |         public void close() throws IOException { | ||||||
|             doSaveContext(); |             doSaveContext(); | ||||||
|             super.close(); |             delegate.close(); | ||||||
|         } |         } | ||||||
|     } |     } | ||||||
| } | } | ||||||
|  | |||||||
| @ -16,6 +16,8 @@ import static org.junit.Assert.*; | |||||||
| import static org.mockito.Mockito.*; | import static org.mockito.Mockito.*; | ||||||
| import static org.springframework.security.web.context.HttpSessionSecurityContextRepository.*; | import static org.springframework.security.web.context.HttpSessionSecurityContextRepository.*; | ||||||
| 
 | 
 | ||||||
|  | import javax.servlet.ServletOutputStream; | ||||||
|  | import javax.servlet.http.HttpServletResponse; | ||||||
| import javax.servlet.http.HttpSession; | import javax.servlet.http.HttpSession; | ||||||
| 
 | 
 | ||||||
| import org.junit.After; | import org.junit.After; | ||||||
| @ -257,6 +259,38 @@ public class HttpSessionSecurityContextRepositoryTests { | |||||||
|         assertEquals(SecurityContextHolder.getContext(), request.getSession().getAttribute("imTheContext")); |         assertEquals(SecurityContextHolder.getContext(), request.getSession().getAttribute("imTheContext")); | ||||||
|     } |     } | ||||||
| 
 | 
 | ||||||
|  |     // SEC-SEC-2055 | ||||||
|  |     @Test | ||||||
|  |     public void outputStreamCloseDelegate() throws Exception { | ||||||
|  |         HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository(); | ||||||
|  |         repo.setSpringSecurityContextKey("imTheContext"); | ||||||
|  |         MockHttpServletRequest request = new MockHttpServletRequest(); | ||||||
|  |         HttpServletResponse response = mock(HttpServletResponse.class); | ||||||
|  |         ServletOutputStream outputstream = mock(ServletOutputStream.class); | ||||||
|  |         when(response.getOutputStream()).thenReturn(outputstream); | ||||||
|  |         HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response); | ||||||
|  |         SecurityContextHolder.setContext(repo.loadContext(holder)); | ||||||
|  |         SecurityContextHolder.getContext().setAuthentication(testToken); | ||||||
|  |         holder.getResponse().getOutputStream().close(); | ||||||
|  |         verify(outputstream).close(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|  |     // SEC-SEC-2055 | ||||||
|  |     @Test | ||||||
|  |     public void outputStreamFlushesDelegate() throws Exception { | ||||||
|  |         HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository(); | ||||||
|  |         repo.setSpringSecurityContextKey("imTheContext"); | ||||||
|  |         MockHttpServletRequest request = new MockHttpServletRequest(); | ||||||
|  |         HttpServletResponse response = mock(HttpServletResponse.class); | ||||||
|  |         ServletOutputStream outputstream = mock(ServletOutputStream.class); | ||||||
|  |         when(response.getOutputStream()).thenReturn(outputstream); | ||||||
|  |         HttpRequestResponseHolder holder = new HttpRequestResponseHolder(request, response); | ||||||
|  |         SecurityContextHolder.setContext(repo.loadContext(holder)); | ||||||
|  |         SecurityContextHolder.getContext().setAuthentication(testToken); | ||||||
|  |         holder.getResponse().getOutputStream().flush(); | ||||||
|  |         verify(outputstream).flush(); | ||||||
|  |     } | ||||||
|  | 
 | ||||||
|     @Test |     @Test | ||||||
|     public void noSessionIsCreatedIfSessionWasInvalidatedDuringTheRequest() throws Exception { |     public void noSessionIsCreatedIfSessionWasInvalidatedDuringTheRequest() throws Exception { | ||||||
|         HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository(); |         HttpSessionSecurityContextRepository repo = new HttpSessionSecurityContextRepository(); | ||||||
|  | |||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user