Skip to content

Commit 8bd4374

Browse files
committed
Throw exception if session created after response
Closes gh-1798
1 parent 15f29f8 commit 8bd4374

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

spring-session-core/src/main/java/org/springframework/session/web/http/SessionRepositoryFilter.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -310,6 +310,10 @@ public HttpSessionWrapper getSession(boolean create) {
310310
if (!create) {
311311
return null;
312312
}
313+
if (SessionRepositoryFilter.this.httpSessionIdResolver instanceof CookieHttpSessionIdResolver
314+
&& this.response.isCommitted()) {
315+
throw new IllegalArgumentException("Cannot create a session after the response has been committed");
316+
}
313317
if (SESSION_LOGGER.isDebugEnabled()) {
314318
SESSION_LOGGER.debug(
315319
"A new session was created. To help you troubleshoot where the session was created we provided a StackTrace (this is not an error). You can prevent this from appearing by disabling DEBUG logging for "

spring-session-core/src/test/java/org/springframework/session/web/http/SessionRepositoryFilterTests.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,18 @@ public void doFilter(HttpServletRequest wrappedRequest) {
423423
assertThat(this.response.getCookie("SESSION")).isNotNull();
424424
}
425425

426+
@Test
427+
void doFilterGetSessionNewWhenResponseCommittedThenException() {
428+
assertThatIllegalArgumentException().isThrownBy(() -> doFilter(new DoInFilter() {
429+
@Override
430+
public void doFilter(HttpServletRequest wrappedRequest, HttpServletResponse wrappedResponse)
431+
throws IOException {
432+
wrappedResponse.getWriter().flush();
433+
wrappedRequest.getSession();
434+
}
435+
}));
436+
}
437+
426438
@Test
427439
void doFilterGetSessionNew() throws Exception {
428440
doFilter(new DoInFilter() {

0 commit comments

Comments
 (0)