28
28
import org .springframework .boot .autoconfigure .AutoConfigureAfter ;
29
29
import org .springframework .boot .autoconfigure .AutoConfigureBefore ;
30
30
import org .springframework .boot .autoconfigure .EnableAutoConfiguration ;
31
+ import org .springframework .boot .autoconfigure .condition .AnyNestedCondition ;
32
+ import org .springframework .boot .autoconfigure .condition .ConditionalOnBean ;
31
33
import org .springframework .boot .autoconfigure .condition .ConditionalOnClass ;
32
34
import org .springframework .boot .autoconfigure .condition .ConditionalOnMissingBean ;
33
35
import org .springframework .boot .autoconfigure .condition .ConditionalOnWebApplication ;
39
41
import org .springframework .boot .autoconfigure .hazelcast .HazelcastAutoConfiguration ;
40
42
import org .springframework .boot .autoconfigure .jdbc .DataSourceAutoConfiguration ;
41
43
import org .springframework .boot .autoconfigure .jdbc .JdbcTemplateAutoConfiguration ;
44
+ import org .springframework .boot .autoconfigure .web .ServerProperties ;
42
45
import org .springframework .boot .autoconfigure .web .reactive .HttpHandlerAutoConfiguration ;
43
46
import org .springframework .boot .context .properties .EnableConfigurationProperties ;
47
+ import org .springframework .boot .context .properties .PropertyMapper ;
48
+ import org .springframework .boot .web .servlet .server .Session .Cookie ;
44
49
import org .springframework .context .ApplicationContext ;
50
+ import org .springframework .context .annotation .Bean ;
51
+ import org .springframework .context .annotation .Conditional ;
45
52
import org .springframework .context .annotation .Configuration ;
46
53
import org .springframework .context .annotation .Import ;
47
54
import org .springframework .context .annotation .ImportSelector ;
48
55
import org .springframework .core .type .AnnotationMetadata ;
49
56
import org .springframework .session .ReactiveSessionRepository ;
50
57
import org .springframework .session .Session ;
51
58
import org .springframework .session .SessionRepository ;
59
+ import org .springframework .session .web .http .CookieHttpSessionIdResolver ;
60
+ import org .springframework .session .web .http .CookieSerializer ;
61
+ import org .springframework .session .web .http .DefaultCookieSerializer ;
62
+ import org .springframework .session .web .http .HttpSessionIdResolver ;
52
63
import org .springframework .util .StringUtils ;
53
64
54
65
/**
64
75
@ Configuration
65
76
@ ConditionalOnClass (Session .class )
66
77
@ ConditionalOnWebApplication
67
- @ EnableConfigurationProperties (SessionProperties .class )
78
+ @ EnableConfigurationProperties ({ ServerProperties . class , SessionProperties .class } )
68
79
@ AutoConfigureAfter ({ DataSourceAutoConfiguration .class , HazelcastAutoConfiguration .class ,
69
80
JdbcTemplateAutoConfiguration .class , MongoDataAutoConfiguration .class ,
70
81
MongoReactiveDataAutoConfiguration .class , RedisAutoConfiguration .class ,
@@ -78,6 +89,28 @@ public class SessionAutoConfiguration {
78
89
SessionRepositoryFilterConfiguration .class })
79
90
static class ServletSessionConfiguration {
80
91
92
+ private final ServerProperties serverProperties ;
93
+
94
+ ServletSessionConfiguration (ServerProperties serverProperties ) {
95
+ this .serverProperties = serverProperties ;
96
+ }
97
+
98
+ @ Bean
99
+ @ Conditional (DefaultCookieSerializerCondition .class )
100
+ public DefaultCookieSerializer cookieSerializer () {
101
+ Cookie cookie = this .serverProperties .getServlet ().getSession ().getCookie ();
102
+ DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer ();
103
+ PropertyMapper map = PropertyMapper .get ().alwaysApplyingWhenNonNull ();
104
+ map .from (cookie ::getName ).to (cookieSerializer ::setCookieName );
105
+ map .from (cookie ::getDomain ).to (cookieSerializer ::setDomainName );
106
+ map .from (cookie ::getPath ).to (cookieSerializer ::setCookiePath );
107
+ map .from (cookie ::getHttpOnly ).to (cookieSerializer ::setUseHttpOnlyCookie );
108
+ map .from (cookie ::getSecure ).to (cookieSerializer ::setUseSecureCookie );
109
+ map .from (cookie ::getMaxAge ).to ((maxAge ) -> cookieSerializer
110
+ .setCookieMaxAge ((int ) maxAge .getSeconds ()));
111
+ return cookieSerializer ;
112
+ }
113
+
81
114
@ Configuration
82
115
@ ConditionalOnMissingBean (SessionRepository .class )
83
116
@ Import ({ ServletSessionRepositoryImplementationValidator .class ,
@@ -103,6 +136,31 @@ static class ReactiveSessionRepositoryConfiguration {
103
136
104
137
}
105
138
139
+ /**
140
+ * Condition to trigger the creation of a {@link DefaultCookieSerializer}. This kicks
141
+ * in if either no {@link HttpSessionIdResolver} and {@link CookieSerializer} beans
142
+ * are registered, or if {@link CookieHttpSessionIdResolver} is registered but
143
+ * {@link CookieSerializer} is not.
144
+ */
145
+ static class DefaultCookieSerializerCondition extends AnyNestedCondition {
146
+
147
+ DefaultCookieSerializerCondition () {
148
+ super (ConfigurationPhase .REGISTER_BEAN );
149
+ }
150
+
151
+ @ ConditionalOnMissingBean ({ HttpSessionIdResolver .class , CookieSerializer .class })
152
+ static class NoComponentsAvailable {
153
+
154
+ }
155
+
156
+ @ ConditionalOnBean (CookieHttpSessionIdResolver .class )
157
+ @ ConditionalOnMissingBean (CookieSerializer .class )
158
+ static class CookieHttpSessionIdResolverAvailable {
159
+
160
+ }
161
+
162
+ }
163
+
106
164
/**
107
165
* {@link ImportSelector} base class to add {@link StoreType} configuration classes.
108
166
*/
0 commit comments