res.setCookie() now accepts boolean httponly and secure flags

httponly defaults to true, secure to false
This commit is contained in:
Robert Gaggl 2017-03-27 15:50:01 +02:00 committed by Tobi Schäfer
parent 97db46a66c
commit 62291171e3
3 changed files with 72 additions and 41 deletions

View file

@ -24,15 +24,13 @@ import javax.servlet.http.Cookie;
* of an HTTP cookie. * of an HTTP cookie.
*/ */
public final class CookieTrans implements Serializable { public final class CookieTrans implements Serializable {
private static final long serialVersionUID = 1811202114296536258L; private String name;
private String value;
String name; private String path;
String value; private String domain;
String path; private boolean isHttpOnly = true;
String domain; private boolean isSecure = false;
int days = -1; private int days = -1;
boolean secure;
boolean httpOnly;
CookieTrans(String name, String value) { CookieTrans(String name, String value) {
this.name = name; this.name = name;
@ -55,9 +53,15 @@ public final class CookieTrans implements Serializable {
this.domain = domain; this.domain = domain;
} }
void setHttpOnly(boolean isHttpOnly) {
this.isHttpOnly = isHttpOnly;
}
void setSecure(boolean isSecure) {
this.isSecure = isSecure;
}
/** /**
*
*
* @return ... * @return ...
*/ */
public String getName() { public String getName() {
@ -65,8 +69,6 @@ public final class CookieTrans implements Serializable {
} }
/** /**
*
*
* @return ... * @return ...
*/ */
public String getValue() { public String getValue() {
@ -74,8 +76,6 @@ public final class CookieTrans implements Serializable {
} }
/** /**
*
*
* @return ... * @return ...
*/ */
public int getDays() { public int getDays() {
@ -83,8 +83,6 @@ public final class CookieTrans implements Serializable {
} }
/** /**
*
*
* @return ... * @return ...
*/ */
public String getPath() { public String getPath() {
@ -92,28 +90,24 @@ public final class CookieTrans implements Serializable {
} }
/** /**
*
*
* @return ... * @return ...
*/ */
public String getDomain() { public String getDomain() {
return domain; return domain;
} }
public boolean isSecure() { /**
return secure; * @return ...
*/
public boolean getHttpOnly() {
return isHttpOnly;
} }
void isSecure(boolean secure) { /**
this.secure = secure; * @return ...
} */
public boolean getSecure() {
public boolean isHttpOnly() { return isSecure;
return httpOnly;
}
void isHttpOnly(boolean httpOnly) {
this.httpOnly = httpOnly;
} }
/** /**
@ -147,8 +141,8 @@ public final class CookieTrans implements Serializable {
c.setDomain(defaultDomain); c.setDomain(defaultDomain);
} }
c.setHttpOnly(httpOnly); c.setHttpOnly(isHttpOnly);
c.setSecure(secure); c.setSecure(isSecure);
return c; return c;
} }

View file

@ -143,7 +143,7 @@ public class ResponseBean implements Serializable {
* @param value the cookie value * @param value the cookie value
*/ */
public void setCookie(String key, String value) { public void setCookie(String key, String value) {
res.setCookie(key, value, -1, null, null); res.setCookie(key, value, -1, null, null, true, false);
} }
/** /**
@ -156,7 +156,7 @@ public class ResponseBean implements Serializable {
* @param days number of days the cookie should be stored * @param days number of days the cookie should be stored
*/ */
public void setCookie(String key, String value, int days) { public void setCookie(String key, String value, int days) {
res.setCookie(key, value, days, null, null); res.setCookie(key, value, days, null, null, true, false);
} }
/** /**
@ -171,7 +171,42 @@ public class ResponseBean implements Serializable {
* @param path the URL path to apply the cookie to * @param path the URL path to apply the cookie to
*/ */
public void setCookie(String key, String value, int days, String path) { public void setCookie(String key, String value, int days, String path) {
res.setCookie(key, value, days, path, null); res.setCookie(key, value, days, path, null, true, false);
}
/**
* Set a HTTP cookie with the name and value that is only applied to
* the URLs matching the given path and is stored by the
* HTTP client for the given number of days. A days value of 0 means the
* cookie should be immediately discarded.
*
* @param key the cookie name
* @param value the cookie value
* @param days number of days the cookie should be stored
* @param path the URL path to apply the cookie to
* @param domain domain
* @param isHttpOnly marks the cookie as HttpOnly
*/
public void setCookie(String key, String value, int days, String path, String domain, boolean isHttpOnly) {
res.setCookie(key, value, days, path, domain, isHttpOnly, false);
}
/**
* Set a HTTP cookie with the name and value that is only applied to
* the URLs matching the given path and is stored by the
* HTTP client for the given number of days. A days value of 0 means the
* cookie should be immediately discarded.
*
* @param key the cookie name
* @param value the cookie value
* @param days number of days the cookie should be stored
* @param path the URL path to apply the cookie to
* @param domain domain
* @param isHttpOnly marks the cookie as HttpOnly
* @param isSecure limits the cookie to secure protocols
*/
public void setCookie(String key, String value, int days, String path, String domain, boolean isHttpOnly, boolean isSecure) {
res.setCookie(key, value, days, path, domain, isHttpOnly, isSecure);
} }
/** /**
@ -187,7 +222,7 @@ public class ResponseBean implements Serializable {
* @param domain domain * @param domain domain
*/ */
public void setCookie(String key, String value, int days, String path, String domain) { public void setCookie(String key, String value, int days, String path, String domain) {
res.setCookie(key, value, days, path, domain); res.setCookie(key, value, days, path, domain, true, false);
} }
/** /**
@ -197,7 +232,7 @@ public class ResponseBean implements Serializable {
* @param key the name of the cookie to be discarded * @param key the name of the cookie to be discarded
*/ */
public void unsetCookie(String key) { public void unsetCookie(String key) {
res.setCookie(key, "", 0, null, null); res.setCookie(key, "", 0, null, null, true, false);
} }
/** /**

View file

@ -974,8 +974,10 @@ public final class ResponseTrans extends Writer implements Serializable {
* @param days the cookie's lifespan in days * @param days the cookie's lifespan in days
* @param path the URL path to apply the cookie to * @param path the URL path to apply the cookie to
* @param domain the domain to apply the cookie to * @param domain the domain to apply the cookie to
* @param isHttpOnly marks the cookie as HttpOnly
* @param isSecure limits the cookie for use with secure protocols
*/ */
public void setCookie(String key, String value, int days, String path, String domain) { public void setCookie(String key, String value, int days, String path, String domain, boolean isHttpOnly, boolean isSecure) {
CookieTrans c = null; CookieTrans c = null;
if (cookies == null) { if (cookies == null) {
@ -1001,11 +1003,11 @@ public final class ResponseTrans extends Writer implements Serializable {
c.setDomain(domain); c.setDomain(domain);
if (!"false".equalsIgnoreCase(app.getProperty("cookies.httpOnly"))) { if (!"false".equalsIgnoreCase(app.getProperty("cookies.httpOnly"))) {
c.isHttpOnly(true); c.setHttpOnly(true);
} }
if ("true".equalsIgnoreCase(app.getProperty("cookies.secure"))) { if ("true".equalsIgnoreCase(app.getProperty("cookies.secure"))) {
c.isSecure(true); c.setSecure(true);
} }
} }