|
38 | 38 | import org.springframework.boot.autoconfigure.jdbc.JdbcTemplateAutoConfiguration; |
39 | 39 | import org.springframework.boot.autoconfigure.mongo.MongoAutoConfiguration; |
40 | 40 | import org.springframework.boot.autoconfigure.mongo.MongoReactiveAutoConfiguration; |
| 41 | +import org.springframework.boot.autoconfigure.web.ServerProperties; |
41 | 42 | import org.springframework.boot.autoconfigure.web.reactive.HttpHandlerAutoConfiguration; |
42 | 43 | import org.springframework.boot.context.properties.EnableConfigurationProperties; |
43 | 44 | import org.springframework.context.ApplicationContext; |
| 45 | +import org.springframework.context.annotation.Bean; |
44 | 46 | import org.springframework.context.annotation.Configuration; |
45 | 47 | import org.springframework.context.annotation.Import; |
46 | 48 | import org.springframework.context.annotation.ImportSelector; |
47 | 49 | import org.springframework.core.type.AnnotationMetadata; |
48 | 50 | import org.springframework.session.ReactiveSessionRepository; |
49 | 51 | import org.springframework.session.Session; |
50 | 52 | import org.springframework.session.SessionRepository; |
| 53 | +import org.springframework.session.web.http.CookieSerializer; |
| 54 | +import org.springframework.session.web.http.DefaultCookieSerializer; |
| 55 | +import org.springframework.session.web.http.HeaderHttpSessionIdResolver; |
51 | 56 |
|
52 | 57 | /** |
53 | 58 | * {@link EnableAutoConfiguration Auto-configuration} for Spring Session. |
|
62 | 67 | @Configuration |
63 | 68 | @ConditionalOnClass(Session.class) |
64 | 69 | @ConditionalOnWebApplication |
65 | | -@EnableConfigurationProperties(SessionProperties.class) |
| 70 | +@EnableConfigurationProperties({ ServerProperties.class, SessionProperties.class }) |
66 | 71 | @AutoConfigureAfter({ DataSourceAutoConfiguration.class, HazelcastAutoConfiguration.class, |
67 | 72 | JdbcTemplateAutoConfiguration.class, MongoAutoConfiguration.class, |
68 | 73 | MongoReactiveAutoConfiguration.class, RedisAutoConfiguration.class, |
69 | 74 | RedisReactiveAutoConfiguration.class }) |
70 | 75 | @AutoConfigureBefore(HttpHandlerAutoConfiguration.class) |
71 | 76 | public class SessionAutoConfiguration { |
72 | 77 |
|
| 78 | + private final ServerProperties serverProperties; |
| 79 | + |
| 80 | + public SessionAutoConfiguration(ServerProperties serverProperties) { |
| 81 | + this.serverProperties = serverProperties; |
| 82 | + } |
| 83 | + |
| 84 | + @Bean |
| 85 | + @ConditionalOnMissingBean({ CookieSerializer.class, |
| 86 | + HeaderHttpSessionIdResolver.class }) |
| 87 | + public DefaultCookieSerializer cookieSerializer() { |
| 88 | + ServerProperties.Session.Cookie cookie = this.serverProperties.getSession() |
| 89 | + .getCookie(); |
| 90 | + DefaultCookieSerializer cookieSerializer = new DefaultCookieSerializer(); |
| 91 | + if (cookie.getName() != null) { |
| 92 | + cookieSerializer.setCookieName(cookie.getName()); |
| 93 | + } |
| 94 | + if (cookie.getDomain() != null) { |
| 95 | + cookieSerializer.setDomainName(cookie.getDomain()); |
| 96 | + } |
| 97 | + if (cookie.getPath() != null) { |
| 98 | + cookieSerializer.setCookiePath(cookie.getPath()); |
| 99 | + } |
| 100 | + if (cookie.getHttpOnly() != null) { |
| 101 | + cookieSerializer.setUseHttpOnlyCookie(cookie.getHttpOnly()); |
| 102 | + } |
| 103 | + if (cookie.getSecure() != null) { |
| 104 | + cookieSerializer.setUseSecureCookie(cookie.getSecure()); |
| 105 | + } |
| 106 | + if (cookie.getMaxAge() != null) { |
| 107 | + cookieSerializer.setCookieMaxAge(cookie.getMaxAge()); |
| 108 | + } |
| 109 | + return cookieSerializer; |
| 110 | + } |
| 111 | + |
73 | 112 | @Configuration |
74 | 113 | @ConditionalOnWebApplication(type = Type.SERVLET) |
75 | 114 | @Import({ ServletSessionRepositoryValidator.class, |
|
0 commit comments