Skip to content

Commit c907838

Browse files
mazenaissajzheaux
authored andcommitted
Make max-session configurable
Closes gh-9202
1 parent 6d59b10 commit c907838

File tree

5 files changed

+51
-4
lines changed

5 files changed

+51
-4
lines changed

config/src/main/java/org/springframework/security/config/http/HttpConfigurationBuilder.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,8 @@ else if (StringUtils.hasText(sessionAuthStratRef)) {
387387
concurrentSessionStrategy = BeanDefinitionBuilder
388388
.rootBeanDefinition(ConcurrentSessionControlAuthenticationStrategy.class);
389389
concurrentSessionStrategy.addConstructorArgValue(this.sessionRegistryRef);
390-
String maxSessions = sessionCtrlElt.getAttribute("max-sessions");
390+
String maxSessions = this.pc.getReaderContext().getEnvironment()
391+
.resolvePlaceholders(sessionCtrlElt.getAttribute("max-sessions"));
391392
if (StringUtils.hasText(maxSessions)) {
392393
concurrentSessionStrategy.addPropertyValue("maximumSessions", maxSessions);
393394
}

config/src/main/resources/org/springframework/security/config/spring-security-5.5.rnc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,7 @@ concurrency-control =
714714

715715
concurrency-control.attlist &=
716716
## The maximum number of sessions a single authenticated user can have open at the same time. Defaults to "1". A negative value denotes unlimited sessions.
717-
attribute max-sessions {xsd:integer}?
717+
attribute max-sessions {xsd:token}?
718718
concurrency-control.attlist &=
719719
## The URL a user will be redirected to if they attempt to use a session which has been "expired" because they have logged in again.
720720
attribute expired-url {xsd:token}?

config/src/main/resources/org/springframework/security/config/spring-security-5.5.xsd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2161,7 +2161,7 @@
21612161
</xs:attributeGroup>
21622162

21632163
<xs:attributeGroup name="concurrency-control.attlist">
2164-
<xs:attribute name="max-sessions" type="xs:integer">
2164+
<xs:attribute name="max-sessions" type="xs:token">
21652165
<xs:annotation>
21662166
<xs:documentation>The maximum number of sessions a single authenticated user can have open at the same time.
21672167
Defaults to "1". A negative value denotes unlimited sessions.

config/src/test/java/org/springframework/security/config/http/SessionManagementConfigTests.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 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.
@@ -79,6 +79,7 @@
7979
* @author Rob Winch
8080
* @author Josh Cummings
8181
* @author Onur Kagan Ozcan
82+
* @author Mazen Aissa
8283
*/
8384
public class SessionManagementConfigTests {
8485

@@ -356,6 +357,18 @@ public void requestWhenMaxSessionsIsSetThenErrorsWhenExceeded() throws Exception
356357
// @formatter:on
357358
}
358359

360+
@Test
361+
public void requestWhenMaxSessionsIsSetWithPlaceHolderThenErrorsWhenExceeded() throws Exception {
362+
System.setProperty("sessionManagement.maxSessions", "1");
363+
this.spring.configLocations(xml("ConcurrencyControlMaxSessionsPlaceHolder")).autowire();
364+
// @formatter:off
365+
this.mvc.perform(get("/auth").with(httpBasic("user", "password")))
366+
.andExpect(status().isOk());
367+
this.mvc.perform(get("/auth").with(httpBasic("user", "password")))
368+
.andExpect(redirectedUrl("/max-exceeded"));
369+
// @formatter:on
370+
}
371+
359372
@Test
360373
public void autowireWhenSessionFixationProtectionIsNoneAndCsrfDisabledThenSessionManagementFilterIsNotWired() {
361374
this.spring.configLocations(xml("NoSessionManagementFilter")).autowire();
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!-- ~ Copyright 2002-2020 the original author or authors. ~ ~ Licensed under
3+
the Apache License, Version 2.0 (the "License"); ~ you may not use this file
4+
except in compliance with the License. ~ You may obtain a copy of the License
5+
at ~ ~ https://www.apache.org/licenses/LICENSE-2.0 ~ ~ Unless required by
6+
applicable law or agreed to in writing, software ~ distributed under the
7+
License is distributed on an "AS IS" BASIS, ~ WITHOUT WARRANTIES OR CONDITIONS
8+
OF ANY KIND, either express or implied. ~ See the License for the specific
9+
language governing permissions and ~ limitations under the License. -->
10+
11+
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
12+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
13+
xmlns="http://www.springframework.org/schema/security"
14+
xsi:schemaLocation="
15+
http://www.springframework.org/schema/security
16+
https://www.springframework.org/schema/security/spring-security.xsd
17+
http://www.springframework.org/schema/beans
18+
https://www.springframework.org/schema/beans/spring-beans.xsd">
19+
20+
<http auto-config="true">
21+
<session-management
22+
session-authentication-error-url="/max-exceeded">
23+
<concurrency-control
24+
max-sessions="${sessionManagement.maxSessions}"
25+
error-if-maximum-exceeded="true" />
26+
</session-management>
27+
</http>
28+
29+
<b:bean name="basicController"
30+
class="org.springframework.security.config.http.SessionManagementConfigTests.BasicController"/>
31+
32+
<b:import resource="userservice.xml"/>
33+
</b:beans>

0 commit comments

Comments
 (0)