Skip to content

Commit d197919

Browse files
authored
feat: Do not serialize VaadinSession by default in development (#11304)
Avoids writing VaadinSession to the HTTP session when the controlling properties are set (devmode + parameter). For implementation reasons, something must be serialized so only a boolean is written and these kind of "not actually serialized" sessions are removed when loading them after deserialization. This makes it unnecessary to login again to application when developing it with live reload tools and the authentication data is stored in the HttpSession. Fixes #11151
1 parent 83e0051 commit d197919

12 files changed

Lines changed: 226 additions & 8 deletions

File tree

‎flow-server/src/main/java/com/vaadin/flow/server/InitParameters.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,14 @@ public class InitParameters implements Serializable {
129129
*/
130130
public static final String SERVLET_PARAMETER_DEVMODE_ENABLE_LIVE_RELOAD = "devmode.liveReload.enabled";
131131

132+
/**
133+
* Configuration parameter name for enabling session serialization in
134+
* development.
135+
*
136+
* @since
137+
*/
138+
public static final String APPLICATION_PARAMETER_DEVMODE_ENABLE_SERIALIZE_SESSION = "devmode.sessionSerialization.enabled";
139+
132140
/**
133141
* I18N provider property.
134142
*/

‎flow-server/src/main/java/com/vaadin/flow/server/VaadinService.java‎

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2173,15 +2173,24 @@ protected VaadinSession loadSession(WrappedSession wrappedSession) {
21732173
* Performs the actual read of the VaadinSession from the underlying HTTP
21742174
* session after sanity checks have been performed.
21752175
* <p>
2176+
* If a VaadinSession that was ignored during serialization is found, it is
2177+
* removed from the session and {@code null} is returned so a new, proper
2178+
* session can be created.
2179+
* <p>
21762180
* Called by {@link #loadSession(WrappedSession)}.
21772181
*
21782182
* @param wrappedSession
21792183
* the underlying HTTP session
21802184
* @return the VaadinSession or null if no session was found
21812185
*/
21822186
protected VaadinSession readFromHttpSession(WrappedSession wrappedSession) {
2183-
return (VaadinSession) wrappedSession
2187+
VaadinSession session = (VaadinSession) wrappedSession
21842188
.getAttribute(getSessionAttributeName());
2189+
if (session != null && session.isDeserializedAsEmpty()) {
2190+
wrappedSession.removeAttribute(getSessionAttributeName());
2191+
return null;
2192+
}
2193+
return session;
21852194
}
21862195

21872196
/**

‎flow-server/src/main/java/com/vaadin/flow/server/VaadinSession.java‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import com.vaadin.flow.function.DeploymentConfiguration;
4949
import com.vaadin.flow.i18n.I18NProvider;
5050
import com.vaadin.flow.internal.CurrentInstance;
51+
import com.vaadin.flow.server.startup.ApplicationConfiguration;
5152
import com.vaadin.flow.shared.communication.PushMode;
5253

5354
/**
@@ -138,6 +139,8 @@ public class VaadinSession implements HttpSessionBindingListener, Serializable {
138139

139140
private final StreamResourceRegistry resourceRegistry;
140141

142+
private transient boolean deserializedAsEmpty;
143+
141144
/**
142145
* Creates a new VaadinSession tied to a VaadinService.
143146
*
@@ -544,6 +547,7 @@ public boolean hasLock() {
544547
* and the session is not locked
545548
*/
546549
public void checkHasLock(String message) {
550+
assert deserializedAsEmpty == false : "The session was discarded during serialization and should not be used";
547551
if (configuration == null || configuration.isProductionMode()) {
548552
assert hasLock() : message;
549553
} else if (!hasLock()) {
@@ -1008,6 +1012,10 @@ public String getPushId() {
10081012
/**
10091013
* Override default deserialization logic to account for transient
10101014
* {@link #pendingAccessQueue}.
1015+
* <p>
1016+
* If a session was not serialized in writeObject, then only marks that this
1017+
* VaadinSession instance should not be used. The instance is removed by
1018+
* {@link VaadinService#readFromHttpSession(WrappedSession)}.
10111019
*
10121020
* @param stream
10131021
* the object to read
@@ -1018,6 +1026,11 @@ public String getPushId() {
10181026
*/
10191027
private void readObject(ObjectInputStream stream)
10201028
throws IOException, ClassNotFoundException {
1029+
boolean wasSerialized = stream.readBoolean();
1030+
this.deserializedAsEmpty = !wasSerialized;
1031+
if (!wasSerialized) {
1032+
return;
1033+
}
10211034
Map<Class<?>, CurrentInstance> old = CurrentInstance.setCurrent(this);
10221035
try {
10231036
stream.defaultReadObject();
@@ -1027,6 +1040,23 @@ private void readObject(ObjectInputStream stream)
10271040
}
10281041
}
10291042

1043+
private void writeObject(java.io.ObjectOutputStream stream)
1044+
throws IOException {
1045+
boolean serialize = true;
1046+
1047+
ApplicationConfiguration appConfiguration = ApplicationConfiguration
1048+
.get(getService().getContext());
1049+
if (!appConfiguration.isProductionMode()
1050+
&& !appConfiguration.isDevModeSessionSerializationEnabled()) {
1051+
serialize = false;
1052+
}
1053+
1054+
stream.writeBoolean(serialize);
1055+
if (serialize) {
1056+
stream.defaultWriteObject();
1057+
}
1058+
}
1059+
10301060
/**
10311061
* Refreshes the transient fields of the session to ensure they are up to
10321062
* date.
@@ -1066,4 +1096,12 @@ public StreamResourceRegistry getResourceRegistry() {
10661096
public String getCsrfToken() {
10671097
return csrfToken;
10681098
}
1099+
1100+
/**
1101+
* Checks if the session was deserialized without content and should be
1102+
* ignored and removed.
1103+
*/
1104+
boolean isDeserializedAsEmpty() {
1105+
return deserializedAsEmpty;
1106+
}
10691107
}

‎flow-server/src/main/java/com/vaadin/flow/server/startup/ApplicationConfiguration.java‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,4 +88,14 @@ static ApplicationConfiguration get(VaadinContext context) {
8888
*/
8989
FallbackChunk getFallbackChunk();
9090

91+
/**
92+
* Checks if development mode session serialization is enabled or not.
93+
* <p>
94+
* By default session serialization is disabled in development mode.
95+
*
96+
* @return {@code true} if dev mode session serialization is enabled,
97+
* {@code false} otherwise
98+
*/
99+
boolean isDevModeSessionSerializationEnabled();
100+
91101
}

‎flow-server/src/main/java/com/vaadin/flow/server/startup/DefaultApplicationConfigurationFactory.java‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
import elemental.json.impl.JsonUtil;
4242

4343
import static com.vaadin.flow.server.Constants.VAADIN_SERVLET_RESOURCES;
44+
import static com.vaadin.flow.server.InitParameters.APPLICATION_PARAMETER_DEVMODE_ENABLE_SERIALIZE_SESSION;
4445
import static com.vaadin.flow.server.InitParameters.SERVLET_PARAMETER_PRODUCTION_MODE;
4546
import static com.vaadin.flow.server.frontend.FrontendUtils.TOKEN_FILE;
4647

@@ -91,6 +92,13 @@ public FallbackChunk getFallbackChunk() {
9192
return fallbackChunk;
9293
}
9394

95+
@Override
96+
public boolean isDevModeSessionSerializationEnabled() {
97+
return getBooleanProperty(
98+
APPLICATION_PARAMETER_DEVMODE_ENABLE_SERIALIZE_SESSION,
99+
false);
100+
}
101+
94102
}
95103

96104
@Override

‎flow-server/src/test/java/com/vaadin/flow/server/SessionRouteRegistryTest.java‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@
4747
import com.vaadin.flow.router.RouterLayout;
4848
import com.vaadin.flow.router.RoutesChangedEvent;
4949
import com.vaadin.flow.router.internal.HasUrlParameterFormat;
50+
import com.vaadin.flow.server.startup.ApplicationConfiguration;
5051
import com.vaadin.flow.server.startup.ApplicationRouteRegistry;
5152
import com.vaadin.flow.shared.Registration;
5253

@@ -63,6 +64,14 @@ public void init() {
6364

6465
vaadinService = Mockito.mock(MockService.class);
6566
Mockito.when(vaadinService.getRouteRegistry()).thenReturn(registry);
67+
VaadinContext context = Mockito.mock(VaadinContext.class);
68+
ApplicationConfiguration applicationConfiguration = Mockito
69+
.mock(ApplicationConfiguration.class);
70+
Mockito.when(vaadinService.getContext()).thenReturn(context);
71+
Mockito.when(context.getAttribute(Mockito.any(), Mockito.any()))
72+
.thenReturn(applicationConfiguration);
73+
Mockito.when(applicationConfiguration.isProductionMode())
74+
.thenReturn(true);
6675

6776
VaadinService.setCurrent(vaadinService);
6877

‎flow-server/src/test/java/com/vaadin/tests/server/SerializationTest.java‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,28 @@
77
import java.io.ObjectOutputStream;
88
import java.io.Serializable;
99

10+
import com.vaadin.flow.server.VaadinContext;
11+
import com.vaadin.flow.server.VaadinService;
1012
import com.vaadin.flow.server.VaadinSession;
13+
import com.vaadin.flow.server.startup.ApplicationConfiguration;
14+
15+
import org.mockito.Mockito;
1116

1217
import junit.framework.TestCase;
1318

1419
public class SerializationTest extends TestCase {
1520

1621
public void testVaadinSession() throws Exception {
17-
VaadinSession session = new VaadinSession(null);
22+
VaadinService service = Mockito.mock(VaadinService.class);
23+
VaadinContext context = Mockito.mock(VaadinContext.class);
24+
ApplicationConfiguration applicationConfiguration = Mockito
25+
.mock(ApplicationConfiguration.class);
26+
Mockito.when(service.getContext()).thenReturn(context);
27+
Mockito.when(context.getAttribute(Mockito.any(), Mockito.any()))
28+
.thenReturn(applicationConfiguration);
29+
Mockito.when(applicationConfiguration.isProductionMode())
30+
.thenReturn(true);
31+
VaadinSession session = new VaadinSession(service);
1832

1933
session = serializeAndDeserialize(session);
2034

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<Configure id="testWebapp" class="org.eclipse.jetty.webapp.WebAppContext">
2+
<Get id="sh" name="sessionHandler">
3+
<Set name="sessionCache">
4+
<New class="org.eclipse.jetty.server.session.DefaultSessionCache">
5+
<Arg>
6+
<Ref id="sh"/>
7+
</Arg>
8+
<Set name="sessionDataStore">
9+
<New class="org.eclipse.jetty.server.session.FileSessionDataStore">
10+
<Set name="storeDir">
11+
<New class="java.io.File">
12+
<Arg>jetty-sessions</Arg>
13+
</New>
14+
</Set>
15+
</New>
16+
</Set>
17+
</New>
18+
</Set>
19+
</Get>
20+
</Configure>

‎flow-tests/test-jetty-reload/pom.xml‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<artifactId>jetty-maven-plugin</artifactId>
6262
<configuration>
6363
<!-- Scan target/scantrigger in 1 second intervals -->
64+
<contextXml>${project.basedir}/context.xml</contextXml>
6465
<scanIntervalSeconds>1</scanIntervalSeconds>
6566
<scanTargetPatterns>
6667
<scanTargetPattern>
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package com.vaadin.flow.uitest.ui;
2+
3+
import java.util.UUID;
4+
5+
import com.vaadin.flow.component.html.Div;
6+
import com.vaadin.flow.component.html.NativeButton;
7+
import com.vaadin.flow.router.Route;
8+
import com.vaadin.flow.server.VaadinSession;
9+
import com.vaadin.flow.server.WrappedSession;
10+
import com.vaadin.flow.uitest.servlet.ViewTestLayout;
11+
12+
@Route(value = "com.vaadin.flow.uitest.ui.SessionValueView", layout = ViewTestLayout.class)
13+
public class SessionValueView extends Div {
14+
15+
// This ensures the view is not serializable
16+
private Object preventSerialization = new Object();
17+
18+
// Test that session values outside VaadinSession are preserved on reload
19+
public SessionValueView() {
20+
21+
final NativeButton triggerButton = new NativeButton("Trigger reload",
22+
event -> {
23+
WebpackDevServerPortView.triggerJettyReload();
24+
});
25+
triggerButton.setId(WebpackDevServerPortView.TRIGGER_RELOAD_ID);
26+
add(triggerButton);
27+
28+
WrappedSession session = VaadinSession.getCurrent().getSession();
29+
String customAttribute = (String) session
30+
.getAttribute("custom-attribute");
31+
if (customAttribute == null) {
32+
customAttribute = UUID.randomUUID().toString();
33+
session.setAttribute("custom-attribute", customAttribute);
34+
}
35+
Div div = new Div();
36+
div.setId("customAttribute");
37+
div.setText("The custom value in the session is: " + customAttribute);
38+
add(div);
39+
40+
}
41+
}

0 commit comments

Comments
 (0)