Skip to content

Commit 47011eb

Browse files
committed
Polish transfer session's max inactive interval
Issue: gh-2693
1 parent 02b7d04 commit 47011eb

File tree

3 files changed

+36
-85
lines changed

3 files changed

+36
-85
lines changed

web/src/main/java/org/springframework/security/web/authentication/session/SessionFixationProtectionStrategy.java

+5-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -90,7 +90,7 @@ final HttpSession applySessionFixation(HttpServletRequest request) {
9090
}
9191

9292
Map<String, Object> attributesToMigrate = extractAttributes(session);
93-
int originMaxInactiveInterval = session.getMaxInactiveInterval();
93+
int maxInactiveIntervalToMigrate = session.getMaxInactiveInterval();
9494

9595
session.invalidate();
9696
session = request.getSession(true); // we now have a new session
@@ -100,7 +100,9 @@ final HttpSession applySessionFixation(HttpServletRequest request) {
100100
}
101101

102102
transferAttributes(attributesToMigrate, session);
103-
session.setMaxInactiveInterval(originMaxInactiveInterval);
103+
if (migrateSessionAttributes) {
104+
session.setMaxInactiveInterval(maxInactiveIntervalToMigrate);
105+
}
104106
return session;
105107
}
106108

web/src/test/java/org/springframework/security/web/authentication/session/SessionFixationProtectionStrategyTest.java

-81
This file was deleted.

web/src/test/java/org/springframework/security/web/session/DefaultSessionAuthenticationStrategyTests.java

+31-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2013 the original author or authors.
2+
* Copyright 2002-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -161,4 +161,34 @@ public void sessionIsCreatedIfAlwaysCreateTrue() {
161161
assertThat(request.getSession(false)).isNotNull();
162162
}
163163

164+
@Test
165+
public void onAuthenticationWhenMigrateSessionAttributesTrueThenMaxInactiveIntervalIsMigrated() {
166+
SessionFixationProtectionStrategy strategy = new SessionFixationProtectionStrategy();
167+
HttpServletRequest request = new MockHttpServletRequest();
168+
HttpSession session = request.getSession();
169+
session.setMaxInactiveInterval(1);
170+
171+
Authentication mockAuthentication = mock(Authentication.class);
172+
173+
strategy.onAuthentication(mockAuthentication, request,
174+
new MockHttpServletResponse());
175+
176+
assertThat(request.getSession().getMaxInactiveInterval()).isEqualTo(1);
177+
}
178+
179+
@Test
180+
public void onAuthenticationWhenMigrateSessionAttributesFalseThenMaxInactiveIntervalIsNotMigrated() {
181+
SessionFixationProtectionStrategy strategy = new SessionFixationProtectionStrategy();
182+
strategy.setMigrateSessionAttributes(false);
183+
HttpServletRequest request = new MockHttpServletRequest();
184+
HttpSession session = request.getSession();
185+
session.setMaxInactiveInterval(1);
186+
187+
Authentication mockAuthentication = mock(Authentication.class);
188+
189+
strategy.onAuthentication(mockAuthentication, request,
190+
new MockHttpServletResponse());
191+
192+
assertThat(request.getSession().getMaxInactiveInterval()).isNotEqualTo(1);
193+
}
164194
}

0 commit comments

Comments
 (0)