11package com .test .vulnerablejavawebapp .config ;
22
3+ import java .util .Collections ;
4+
35import javax .servlet .Filter ;
6+ import javax .servlet .ServletContext ;
7+ import javax .servlet .ServletException ;
8+ import javax .servlet .SessionCookieConfig ;
9+ import javax .servlet .SessionTrackingMode ;
410import javax .sql .DataSource ;
511
612import org .apache .catalina .connector .Connector ;
915import org .springframework .boot .SpringApplication ;
1016import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
1117import org .springframework .boot .context .embedded .EmbeddedServletContainerFactory ;
18+ import org .springframework .boot .context .embedded .ServletContextInitializer ;
1219import org .springframework .boot .context .embedded .tomcat .TomcatConnectorCustomizer ;
1320import org .springframework .boot .context .embedded .tomcat .TomcatEmbeddedServletContainerFactory ;
1421import org .springframework .boot .web .servlet .FilterRegistrationBean ;
@@ -67,6 +74,24 @@ public Filter sessionUserFilter() {
6774 return new SessionUserFilter ();
6875 }
6976
77+ public static void main (String [] args ) throws Exception {
78+ SpringApplication .run (AppLauncher .class , args );
79+ }
80+
81+ // The following configurations are usally on the application servers and not in the code
82+ // but since we are using spring boot to make things simple we configure via code
83+ @ Bean
84+ public ServletContextInitializer servletContextInitializer () {
85+ return new ServletContextInitializer () {
86+ @ Override
87+ public void onStartup (ServletContext servletContext ) throws ServletException {
88+ servletContext .setSessionTrackingModes (Collections .singleton (SessionTrackingMode .COOKIE ));
89+ SessionCookieConfig sessionCookieConfig = servletContext .getSessionCookieConfig ();
90+ sessionCookieConfig .setHttpOnly (true );
91+ }
92+ };
93+
94+ }
7095 @ Bean
7196 public EmbeddedServletContainerFactory servletContainer () {
7297 // https config
@@ -75,8 +100,6 @@ public EmbeddedServletContainerFactory servletContainer() {
75100 final String keystoreProvider = "SunJSSE" ;
76101 final String keystoreAlias = "tomcat" ;
77102 final String keystoreAbsolutePath = getClass ().getClassLoader ().getResource ("ssl/keystore.p12" ).getFile ();
78-
79-
80103
81104 TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory ();
82105 factory .addConnectorCustomizers ((TomcatConnectorCustomizer ) (Connector con ) -> {
@@ -93,8 +116,4 @@ public EmbeddedServletContainerFactory servletContainer() {
93116
94117 return factory ;
95118 }
96-
97- public static void main (String [] args ) throws Exception {
98- SpringApplication .run (AppLauncher .class , args );
99- }
100119}
0 commit comments