Skip to content

Commit ab8846e

Browse files
authored
Add new configuration option - cookie-max-age (#56)
Fix #55
1 parent 73d7e7e commit ab8846e

File tree

3 files changed

+14
-1
lines changed

3 files changed

+14
-1
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,10 @@ To set up Hazelcast Session Clustering:
104104
<param-name>cookie-path</param-name>
105105
<param-value>/</param-value>
106106
</init-param>
107+
<init-param>
108+
<param-name>cookie-max-age</param-name>
109+
<param-value>-1</param-value>
110+
</init-param>
107111
<init-param>
108112
<param-name>use-request-parameter</param-name>
109113
<param-value>false</param-value>
@@ -136,6 +140,7 @@ Following are the descriptions of parameters included in the above XML.
136140
- `cookie-path`: Path of the session ID cookie. Its default value is based on the context path of the incoming request.
137141
- `cookie-secure`: Specifies whether the cookie only be sent using a secure protocol. Its default value is false.
138142
- `cookie-http-only`: Specifies whether the attribute `HttpOnly` can be set on cookie. Its default value is false.
143+
- `cookie-max-age`: Specifies the maximum age of the cookie in seconds. Its default value is `-1`, meaning the cookie is not stored persistently and will be deleted when the browser exits.
139144
- `config-location`: Location of Hazelcast configuration. It can be specified as a servlet resource, classpath resource or as a URL. Its default value is `hazelcast-default.xml` or `hazelcast.xml` in the classpath.
140145
- `instance-name`: Name of an existing Hazelcast instance, if you want to use it. Its default value is null. If you do not have an instance, then you should create one.
141146
- `use-client`: Specifies whether you want to connect to an existing cluster as a client. Its default value is false.

src/main/java/com/hazelcast/web/WebFilter.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,7 @@ private void addSessionCookie(final HazelcastRequestWrapper req, final String se
263263
path = "/";
264264
}
265265
sessionCookie.setPath(path);
266-
sessionCookie.setMaxAge(-1);
266+
sessionCookie.setMaxAge(config.getCookieMaxAge());
267267
if (config.getCookieDomain() != null) {
268268
sessionCookie.setDomain(config.getCookieDomain());
269269
}

src/main/java/com/hazelcast/web/WebFilterConfig.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public final class WebFilterConfig {
5959
private static final String COOKIE_SECURE = "cookie-secure";
6060
private static final String COOKIE_HTTP_ONLY = "cookie-http-only";
6161
private static final String COOKIE_PATH = "cookie-path";
62+
private static final String COOKIE_MAX_AGE = "cookie-max-age";
6263

6364
private boolean useClient;
6465
private URL configUrl;
@@ -75,6 +76,7 @@ public final class WebFilterConfig {
7576
private boolean cookieSecure;
7677
private boolean cookieHttpOnly;
7778
private String cookiePath;
79+
private int cookieMaxAge;
7880

7981
private WebFilterConfig() {
8082
}
@@ -108,6 +110,7 @@ public static WebFilterConfig create(FilterConfig filterConfig, Properties prope
108110
boolean cookieSecure = getBoolean(filterConfig, properties, COOKIE_SECURE, false);
109111
boolean cookieHttpOnly = getBoolean(filterConfig, properties, COOKIE_HTTP_ONLY, false);
110112
String cookiePath = getString(filterConfig, properties, COOKIE_PATH, null);
113+
int cookieMaxAge = getInt(filterConfig, properties, COOKIE_MAX_AGE, -1);
111114

112115
WebFilterConfig wfc = new WebFilterConfig();
113116
wfc.useClient = useClient;
@@ -125,6 +128,7 @@ public static WebFilterConfig create(FilterConfig filterConfig, Properties prope
125128
wfc.cookieSecure = cookieSecure;
126129
wfc.cookieHttpOnly = cookieHttpOnly;
127130
wfc.cookiePath = cookiePath;
131+
wfc.cookieMaxAge = cookieMaxAge;
128132
return wfc;
129133
}
130134

@@ -188,6 +192,10 @@ public String getCookiePath() {
188192
return cookiePath;
189193
}
190194

195+
public int getCookieMaxAge() {
196+
return cookieMaxAge;
197+
}
198+
191199
private static boolean getBoolean(FilterConfig filterConfig, Properties properties, String paramName, boolean defaultValue) {
192200
String value = getValue(filterConfig, properties, paramName);
193201
if (StringUtil.isNullOrEmptyAfterTrim(value)) {

0 commit comments

Comments
 (0)