Skip to content

Commit a51996f

Browse files
committed
Merge pull request #16889 from vpavic
* pr/16889: Polish "Fix NoClassDefFound when missing Spring Security" Fix NoClassDefFound when missing Spring Security
2 parents 5fc67c5 + 6913ea2 commit a51996f

File tree

3 files changed

+96
-6
lines changed

3 files changed

+96
-6
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/session/SessionAutoConfiguration.java

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
import org.springframework.session.web.http.CookieSerializer;
6262
import org.springframework.session.web.http.DefaultCookieSerializer;
6363
import org.springframework.session.web.http.HttpSessionIdResolver;
64+
import org.springframework.util.ClassUtils;
6465
import org.springframework.util.StringUtils;
6566

6667
/**
@@ -84,6 +85,8 @@
8485
@AutoConfigureBefore(HttpHandlerAutoConfiguration.class)
8586
public class SessionAutoConfiguration {
8687

88+
private static final String REMEMBER_ME_SERVICES_CLASS = "org.springframework.security.web.authentication.RememberMeServices";
89+
8790
@Configuration
8891
@ConditionalOnWebApplication(type = Type.SERVLET)
8992
@Import({ ServletSessionRepositoryValidator.class,
@@ -92,8 +95,8 @@ static class ServletSessionConfiguration {
9295

9396
@Bean
9497
@Conditional(DefaultCookieSerializerCondition.class)
95-
public DefaultCookieSerializer cookieSerializer(ServerProperties serverProperties,
96-
ObjectProvider<SpringSessionRememberMeServices> springSessionRememberMeServices) {
98+
public DefaultCookieSerializer cookieSerializer(
99+
ServerProperties serverProperties) {
97100
Cookie cookie = serverProperties.getServlet().getSession().getCookie();
98101
DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer();
99102
PropertyMapper map = PropertyMapper.get().alwaysApplyingWhenNonNull();
@@ -104,9 +107,11 @@ public DefaultCookieSerializer cookieSerializer(ServerProperties serverPropertie
104107
map.from(cookie::getSecure).to(cookieSerializer::setUseSecureCookie);
105108
map.from(cookie::getMaxAge).to((maxAge) -> cookieSerializer
106109
.setCookieMaxAge((int) maxAge.getSeconds()));
107-
springSessionRememberMeServices.ifAvailable((
108-
rememberMeServices) -> cookieSerializer.setRememberMeRequestAttribute(
109-
SpringSessionRememberMeServices.REMEMBER_ME_LOGIN_ATTR));
110+
if (ClassUtils.isPresent(REMEMBER_ME_SERVICES_CLASS,
111+
getClass().getClassLoader())) {
112+
new RememberMeServicesCookieSerializerCustomizer()
113+
.apply(cookieSerializer);
114+
}
110115
return cookieSerializer;
111116
}
112117

@@ -135,6 +140,19 @@ static class ReactiveSessionRepositoryConfiguration {
135140

136141
}
137142

143+
/**
144+
* Customization log for {@link SpringSessionRememberMeServices} that is only
145+
* instantiated when Spring Security is on the classpath.
146+
*/
147+
static class RememberMeServicesCookieSerializerCustomizer {
148+
149+
public void apply(DefaultCookieSerializer cookieSerializer) {
150+
cookieSerializer.setRememberMeRequestAttribute(
151+
SpringSessionRememberMeServices.REMEMBER_ME_LOGIN_ATTR);
152+
}
153+
154+
}
155+
138156
/**
139157
* Condition to trigger the creation of a {@link DefaultCookieSerializer}. This kicks
140158
* in if either no {@link HttpSessionIdResolver} and {@link CookieSerializer} beans

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/session/AbstractSessionAutoConfigurationTests.java

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2018 the original author or authors.
2+
* Copyright 2012-2019 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.
@@ -16,10 +16,16 @@
1616

1717
package org.springframework.boot.autoconfigure.session;
1818

19+
import java.util.Collections;
20+
1921
import org.springframework.boot.test.context.assertj.AssertableReactiveWebApplicationContext;
2022
import org.springframework.boot.test.context.assertj.AssertableWebApplicationContext;
23+
import org.springframework.context.annotation.Bean;
24+
import org.springframework.context.annotation.Configuration;
25+
import org.springframework.session.MapSessionRepository;
2126
import org.springframework.session.ReactiveSessionRepository;
2227
import org.springframework.session.SessionRepository;
28+
import org.springframework.session.config.annotation.web.http.EnableSpringHttpSession;
2329
import org.springframework.session.web.http.SessionRepositoryFilter;
2430
import org.springframework.web.server.session.WebSessionManager;
2531

@@ -51,4 +57,15 @@ protected <T extends ReactiveSessionRepository<?>> T validateSessionRepository(
5157
return type.cast(repository);
5258
}
5359

60+
@Configuration
61+
@EnableSpringHttpSession
62+
static class SessionRepositoryConfiguration {
63+
64+
@Bean
65+
public MapSessionRepository mySessionRepository() {
66+
return new MapSessionRepository(Collections.emptyMap());
67+
}
68+
69+
}
70+
5471
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
/*
2+
* Copyright 2012-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.boot.autoconfigure.session;
18+
19+
import org.junit.Test;
20+
import org.junit.runner.RunWith;
21+
22+
import org.springframework.boot.autoconfigure.AutoConfigurations;
23+
import org.springframework.boot.test.context.runner.WebApplicationContextRunner;
24+
import org.springframework.boot.testsupport.runner.classpath.ClassPathExclusions;
25+
import org.springframework.boot.testsupport.runner.classpath.ModifiedClassPathRunner;
26+
import org.springframework.session.web.http.DefaultCookieSerializer;
27+
28+
import static org.assertj.core.api.Assertions.assertThat;
29+
30+
/**
31+
* Tests for {@link SessionAutoConfiguration} when Spring Security is not on the
32+
* classpath.
33+
*
34+
* @author Vedran Pavic
35+
*/
36+
@RunWith(ModifiedClassPathRunner.class)
37+
@ClassPathExclusions("spring-security-*")
38+
public class SessionAutoConfigurationWithoutSecurityTests
39+
extends AbstractSessionAutoConfigurationTests {
40+
41+
private final WebApplicationContextRunner contextRunner = new WebApplicationContextRunner()
42+
.withConfiguration(AutoConfigurations.of(SessionAutoConfiguration.class));
43+
44+
@Test
45+
public void sessionCookieConfigurationIsAppliedToAutoConfiguredCookieSerializer() {
46+
this.contextRunner.withUserConfiguration(SessionRepositoryConfiguration.class)
47+
.run((context) -> {
48+
DefaultCookieSerializer cookieSerializer = context
49+
.getBean(DefaultCookieSerializer.class);
50+
assertThat(cookieSerializer).hasFieldOrPropertyWithValue(
51+
"rememberMeRequestAttribute", null);
52+
});
53+
}
54+
55+
}

0 commit comments

Comments
 (0)