From 73d6d30951cb145bbd6b2e2fcdc0388019b15ae9 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 26 Mar 2015 17:28:26 -0400 Subject: [PATCH] Add flag whether to create HTTP session Issue: SPR-12840 --- .../HttpSessionHandshakeInterceptor.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java index ba7de0c02df..57f9cd0d735 100644 --- a/spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java +++ b/spring-websocket/src/main/java/org/springframework/web/socket/server/support/HttpSessionHandshakeInterceptor.java @@ -54,6 +54,8 @@ public class HttpSessionHandshakeInterceptor implements HandshakeInterceptor { private boolean copyHttpSessionId = true; + private boolean createSession; + /** * Default constructor for copying all HTTP session attributes and the HTTP @@ -121,6 +123,22 @@ public class HttpSessionHandshakeInterceptor implements HandshakeInterceptor { return this.copyHttpSessionId; } + /** + * Whether to allow the HTTP session to be created while accessing it. + *

By default set to {@code false}. + * @see javax.servlet.http.HttpServletRequest#getSession(boolean) + */ + public void setCreateSession(boolean createSession) { + this.createSession = createSession; + } + + /** + * Whether the HTTP session is allowed to be created. + */ + public boolean isCreateSession() { + return this.createSession; + } + @Override public boolean beforeHandshake(ServerHttpRequest request, ServerHttpResponse response, @@ -145,7 +163,7 @@ public class HttpSessionHandshakeInterceptor implements HandshakeInterceptor { private HttpSession getSession(ServerHttpRequest request) { if (request instanceof ServletServerHttpRequest) { ServletServerHttpRequest serverRequest = (ServletServerHttpRequest) request; - return serverRequest.getServletRequest().getSession(false); + return serverRequest.getServletRequest().getSession(isCreateSession()); } return null; }