Skip to content

Commit 67d40a1

Browse files
Address SessionLimitStrategy
Closes gh-16206
1 parent 2ff3c82 commit 67d40a1

File tree

56 files changed

+167
-7
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+167
-7
lines changed

config/src/main/java/org/springframework/security/config/SecurityNamespaceHandler.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public BeanDefinition parse(Element element, ParserContext pc) {
9696
pc.getReaderContext()
9797
.fatal("You cannot use a spring-security-2.0.xsd or spring-security-3.0.xsd or "
9898
+ "spring-security-3.1.xsd schema or spring-security-3.2.xsd schema or spring-security-4.0.xsd schema "
99-
+ "with Spring Security 6.4. Please update your schema declarations to the 6.4 schema.",
99+
+ "with Spring Security 6.5. Please update your schema declarations to the 6.5 schema.",
100100
element);
101101
}
102102
String name = pc.getDelegate().getLocalName(element);
@@ -221,7 +221,7 @@ private boolean namespaceMatchesVersion(Element element) {
221221

222222
private boolean matchesVersionInternal(Element element) {
223223
String schemaLocation = element.getAttributeNS("http://www.w3.org/2001/XMLSchema-instance", "schemaLocation");
224-
return schemaLocation.matches("(?m).*spring-security-6\\.4.*.xsd.*")
224+
return schemaLocation.matches("(?m).*spring-security-6\\.5.*.xsd.*")
225225
|| schemaLocation.matches("(?m).*spring-security.xsd.*")
226226
|| !schemaLocation.matches("(?m).*spring-security.*");
227227
}

config/src/main/resources/META-INF/spring.schemas

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
# limitations under the License.
1515
#
1616

17-
http\://www.springframework.org/schema/security/spring-security.xsd=org/springframework/security/config/spring-security-6.4.xsd
17+
http\://www.springframework.org/schema/security/spring-security.xsd=org/springframework/security/config/spring-security-6.5.xsd
18+
http\://www.springframework.org/schema/security/spring-security-6.5.xsd=org/springframework/security/config/spring-security-6.5.xsd
1819
http\://www.springframework.org/schema/security/spring-security-6.4.xsd=org/springframework/security/config/spring-security-6.4.xsd
1920
http\://www.springframework.org/schema/security/spring-security-6.3.xsd=org/springframework/security/config/spring-security-6.3.xsd
2021
http\://www.springframework.org/schema/security/spring-security-6.2.xsd=org/springframework/security/config/spring-security-6.2.xsd
@@ -40,7 +41,8 @@ http\://www.springframework.org/schema/security/spring-security-2.0.xsd=org/spri
4041
http\://www.springframework.org/schema/security/spring-security-2.0.1.xsd=org/springframework/security/config/spring-security-2.0.1.xsd
4142
http\://www.springframework.org/schema/security/spring-security-2.0.2.xsd=org/springframework/security/config/spring-security-2.0.2.xsd
4243
http\://www.springframework.org/schema/security/spring-security-2.0.4.xsd=org/springframework/security/config/spring-security-2.0.4.xsd
43-
https\://www.springframework.org/schema/security/spring-security.xsd=org/springframework/security/config/spring-security-6.4.xsd
44+
https\://www.springframework.org/schema/security/spring-security.xsd=org/springframework/security/config/spring-security-6.5.xsd
45+
https\://www.springframework.org/schema/security/spring-security-6.5.xsd=org/springframework/security/config/spring-security-6.5.xsd
4446
https\://www.springframework.org/schema/security/spring-security-6.4.xsd=org/springframework/security/config/spring-security-6.4.xsd
4547
https\://www.springframework.org/schema/security/spring-security-6.3.xsd=org/springframework/security/config/spring-security-6.3.xsd
4648
https\://www.springframework.org/schema/security/spring-security-6.2.xsd=org/springframework/security/config/spring-security-6.2.xsd

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

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@
3434
import org.springframework.beans.factory.xml.XmlBeanDefinitionStoreException;
3535
import org.springframework.security.config.test.SpringTestContext;
3636
import org.springframework.security.config.test.SpringTestContextExtension;
37+
import org.springframework.security.core.Authentication;
38+
import org.springframework.security.web.session.SessionLimitStrategy;
3739
import org.springframework.test.web.servlet.MockMvc;
3840
import org.springframework.test.web.servlet.ResultMatcher;
3941
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
@@ -840,6 +842,69 @@ public void requestWhenSessionManagementConcurrencyControlMaxSessionIsUnlimited(
840842
assertThat(firstSession.getId()).isNotEqualTo(secondSession.getId());
841843
}
842844

845+
@Test
846+
public void requestWhenSessionManagementConcurrencyControlMaxSessionRefIsOneForNonAdminUsers() throws Exception {
847+
this.spring.configLocations(this.xml("DefaultsSessionManagementConcurrencyControlMaxSessionsRef")).autowire();
848+
// @formatter:off
849+
MockHttpServletRequestBuilder requestBuilder = post("/login")
850+
.with(csrf())
851+
.param("username", "user")
852+
.param("password", "password");
853+
HttpSession firstSession = this.mvc.perform(requestBuilder)
854+
.andExpect(status().is3xxRedirection())
855+
.andExpect(redirectedUrl("/"))
856+
.andReturn()
857+
.getRequest()
858+
.getSession(false);
859+
// @formatter:on
860+
assertThat(firstSession).isNotNull();
861+
// @formatter:off
862+
this.mvc.perform(requestBuilder)
863+
.andExpect(status().isFound())
864+
.andExpect(redirectedUrl("/login?error"));
865+
// @formatter:on
866+
}
867+
868+
@Test
869+
public void requestWhenSessionManagementConcurrencyControlMaxSessionRefIsTwoForAdminUsers() throws Exception {
870+
this.spring.configLocations(this.xml("DefaultsSessionManagementConcurrencyControlMaxSessionsRef")).autowire();
871+
// @formatter:off
872+
MockHttpServletRequestBuilder requestBuilder = post("/login")
873+
.with(csrf())
874+
.param("username", "admin")
875+
.param("password", "password");
876+
HttpSession firstSession = this.mvc.perform(requestBuilder)
877+
.andExpect(status().is3xxRedirection())
878+
.andExpect(redirectedUrl("/"))
879+
.andReturn()
880+
.getRequest()
881+
.getSession(false);
882+
assertThat(firstSession).isNotNull();
883+
HttpSession secondSession = this.mvc.perform(requestBuilder)
884+
.andExpect(status().is3xxRedirection())
885+
.andExpect(redirectedUrl("/"))
886+
.andReturn()
887+
.getRequest()
888+
.getSession(false);
889+
assertThat(secondSession).isNotNull();
890+
// @formatter:on
891+
assertThat(firstSession.getId()).isNotEqualTo(secondSession.getId());
892+
// @formatter:off
893+
this.mvc.perform(requestBuilder)
894+
.andExpect(status().isFound())
895+
.andExpect(redirectedUrl("/login?error"));
896+
// @formatter:on
897+
}
898+
899+
@Test
900+
public void requestWhenSessionManagementConcurrencyControlWithInvalidMaxSessionConfig() {
901+
assertThatExceptionOfType(BeanDefinitionParsingException.class)
902+
.isThrownBy(() -> this.spring
903+
.configLocations(this.xml("DefaultsSessionManagementConcurrencyControlWithInvalidMaxSessionsConfig"))
904+
.autowire())
905+
.withMessageContaining("Cannot use 'max-sessions' attribute and 'max-sessions-ref' attribute together.");
906+
}
907+
843908
private static ResultMatcher includesDefaults() {
844909
return includes(defaultHeaders);
845910
}
@@ -890,4 +955,16 @@ public String ok() {
890955

891956
}
892957

958+
public static class CustomSessionLimit implements SessionLimitStrategy {
959+
960+
@Override
961+
public Integer apply(Authentication authentication) {
962+
if ("admin".equals(authentication.getName())) {
963+
return 2;
964+
}
965+
return 1;
966+
}
967+
968+
}
969+
893970
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2002-2024 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xmlns="http://www.springframework.org/schema/security"
21+
xsi:schemaLocation="
22+
http://www.springframework.org/schema/security
23+
https://www.springframework.org/schema/security/spring-security.xsd
24+
http://www.springframework.org/schema/beans
25+
https://www.springframework.org/schema/beans/spring-beans.xsd">
26+
27+
<http auto-config="true">
28+
<session-management>
29+
<concurrency-control max-sessions-ref="customSessionLimit"
30+
error-if-maximum-exceeded="true"/>
31+
</session-management>
32+
<intercept-url pattern="/**" access="permitAll"/>
33+
</http>
34+
35+
<b:bean name="simple" class="org.springframework.security.config.http.HttpHeadersConfigTests.SimpleController"/>
36+
37+
<b:bean name="customSessionLimit" class="org.springframework.security.config.http.HttpHeadersConfigTests.CustomSessionLimit"/>
38+
39+
<b:import resource="userservice.xml"/>
40+
</b:beans>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
~ Copyright 2002-2024 the original author or authors.
4+
~
5+
~ Licensed under the Apache License, Version 2.0 (the "License");
6+
~ you may not use this file except in compliance with the License.
7+
~ You may obtain a copy of the License at
8+
~
9+
~ https://www.apache.org/licenses/LICENSE-2.0
10+
~
11+
~ Unless required by applicable law or agreed to in writing, software
12+
~ distributed under the License is distributed on an "AS IS" BASIS,
13+
~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
~ See the License for the specific language governing permissions and
15+
~ limitations under the License.
16+
-->
17+
18+
<b:beans xmlns:b="http://www.springframework.org/schema/beans"
19+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
20+
xmlns="http://www.springframework.org/schema/security"
21+
xsi:schemaLocation="
22+
http://www.springframework.org/schema/security
23+
https://www.springframework.org/schema/security/spring-security.xsd
24+
http://www.springframework.org/schema/beans
25+
https://www.springframework.org/schema/beans/spring-beans.xsd">
26+
27+
<http auto-config="true">
28+
<session-management>
29+
<concurrency-control max-sessions="10"
30+
max-sessions-ref="customSessionLimit"
31+
error-if-maximum-exceeded="true"/>
32+
</session-management>
33+
<intercept-url pattern="/**" access="permitAll"/>
34+
</http>
35+
36+
<b:bean name="simple" class="org.springframework.security.config.http.HttpHeadersConfigTests.SimpleController"/>
37+
38+
<b:bean name="customSessionLimit" class="org.springframework.security.config.http.HttpHeadersConfigTests.CustomSessionLimit"/>
39+
40+
<b:import resource="userservice.xml"/>
41+
</b:beans>

config/src/test/resources/org/springframework/security/config/method-security.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
xmlns:tx="http://www.springframework.org/schema/tx"
2323
xsi:schemaLocation="http://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
2424
http://www.springframework.org/schema/tx https://www.springframework.org/schema/tx/spring-tx.xsd
25-
http://www.springframework.org/schema/security org/springframework/security/config/spring-security-6.4.xsd">
25+
http://www.springframework.org/schema/security org/springframework/security/config/spring-security-6.5.xsd">
2626

2727
<tx:annotation-driven />
2828

0 commit comments

Comments
 (0)