Skip to content

Commit 4d84933

Browse files
committed
Also call setHttpOnly property on Tomcat context
Update `ServerProperties` to also call `setHttpOnly` on the `TomcatContext`. It appears that this is required in addition to using the `ServletContextInitializer` to setup `SessionCookieConfig`. Closes gh-12580
1 parent b03f890 commit 4d84933

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -864,6 +864,17 @@ void customizeTomcat(ServerProperties serverProperties,
864864
.getIncludeStacktrace() == ErrorProperties.IncludeStacktrace.NEVER) {
865865
customizeErrorReportValve(factory);
866866
}
867+
Cookie cookie = serverProperties.getSession().getCookie();
868+
if (cookie.getHttpOnly() != null) {
869+
factory.addContextCustomizers(new TomcatContextCustomizer() {
870+
871+
@Override
872+
public void customize(Context context) {
873+
context.setUseHttpOnly(cookie.getHttpOnly());
874+
}
875+
876+
});
877+
}
867878
}
868879

869880
private void customizeErrorReportValve(

spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/web/ServerPropertiesTests.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232

3333
import org.apache.catalina.Context;
3434
import org.apache.catalina.Valve;
35+
import org.apache.catalina.core.StandardContext;
36+
import org.apache.catalina.startup.Tomcat;
3537
import org.apache.catalina.valves.AccessLogValve;
3638
import org.apache.catalina.valves.ErrorReportValve;
3739
import org.apache.catalina.valves.RemoteIpValve;
@@ -734,6 +736,18 @@ private void testCustomTomcatTldSkip(String... expectedJars) {
734736
"spring-boot-*.jar");
735737
}
736738

739+
@Test
740+
public void customTomcatHttpOnlyCookie() throws Exception {
741+
this.properties.getSession().getCookie().setHttpOnly(false);
742+
TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory();
743+
this.properties.customize(factory);
744+
EmbeddedServletContainer container = factory.getEmbeddedServletContainer();
745+
Tomcat tomcat = ((TomcatEmbeddedServletContainer) container).getTomcat();
746+
StandardContext context = (StandardContext) tomcat.getHost().findChildren()[0];
747+
assertThat(context.getUseHttpOnly()).isFalse();
748+
container.stop();
749+
}
750+
737751
@Test
738752
public void defaultUseForwardHeadersUndertow() throws Exception {
739753
UndertowEmbeddedServletContainerFactory container = spy(

0 commit comments

Comments
 (0)