Skip to content

Commit 04aab7f

Browse files
committed
Add SameSite Cookie attribute
See jakartaee#175 Signed-off-by: Adam Klinkosz <spyro@o2.pl>
1 parent 5f4d951 commit 04aab7f

File tree

2 files changed

+78
-0
lines changed

2 files changed

+78
-0
lines changed

api/src/main/java/javax/servlet/SessionCookieConfig.java

+22
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@
1717

1818
package javax.servlet;
1919

20+
import javax.servlet.http.Cookie;
21+
2022
/**
2123
* Class that may be used to configure various properties of cookies used for session tracking purposes.
2224
*
@@ -236,4 +238,24 @@ public interface SessionCookieConfig {
236238
* @see javax.servlet.http.Cookie#getMaxAge
237239
*/
238240
public int getMaxAge();
241+
242+
/**
243+
* Returns the <i>SameSite</i> attribute of the cookie.
244+
*
245+
* @see javax.servlet.http.Cookie.SameSite
246+
* @see javax.servlet.http.Cookie#getSameSite()
247+
*
248+
* @return the <i>SameSite</i> attribute of the cookie
249+
*/
250+
public Cookie.SameSite getSameSite();
251+
252+
/**
253+
* Sets the <i>SameSite</i> attribute of the cookie.
254+
*
255+
* @see javax.servlet.http.Cookie.SameSite
256+
* @see javax.servlet.http.Cookie#setSameSite
257+
*
258+
* @param sameSite the <i>SameSite</i> attribute of the cookie
259+
*/
260+
public void setSameSite(final Cookie.SameSite sameSite);
239261
}

api/src/main/java/javax/servlet/http/Cookie.java

+56
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ public class Cookie implements Cloneable, Serializable {
9292
private boolean secure; // ;Secure ... e.g. use SSL
9393
private int version = 0; // ;Version=1 ... means RFC 2109++ style
9494
private boolean isHttpOnly = false;
95+
private SameSite sameSite;
9596

9697
/**
9798
* Constructs a cookie with the specified name and value.
@@ -422,4 +423,59 @@ public void setHttpOnly(boolean isHttpOnly) {
422423
public boolean isHttpOnly() {
423424
return isHttpOnly;
424425
}
426+
427+
/**
428+
* Returns the <i>SameSite</i> attribute of the cookie.
429+
*
430+
* @return the <i>SameSite</i> attribute of the cookie
431+
*/
432+
public SameSite getSameSite() {
433+
return sameSite;
434+
}
435+
436+
/**
437+
* Sets the <i>SameSite</i> attribute of the cookie.
438+
*
439+
* @param sameSite the <i>SameSite</i> attribute of the cookie
440+
*/
441+
public void setSameSite(SameSite sameSite) {
442+
this.sameSite = sameSite;
443+
}
444+
445+
/**
446+
* Available SameSite directives for the cookie as described in RFC6265bis.
447+
*/
448+
public enum SameSite {
449+
450+
/**
451+
* The cookie will only be sent if the site for the cookie matches the current
452+
* site URL. The cookie will not be sent along with requests initiated by
453+
* third party websites.
454+
*/
455+
STRICT("Strict"),
456+
457+
/**
458+
* The cookie will only be sent if the site for the cookie matches the current
459+
* site URL. The cookie will be sent along with the GET request initiated by
460+
* third party website.
461+
*/
462+
LAX("Lax"),
463+
464+
/**
465+
* The cookie will be sent cross-origin. This directive requires the Secure
466+
* attribute.
467+
*/
468+
NONE("None");
469+
470+
private final String value;
471+
472+
SameSite(final String value) {
473+
this.value = value;
474+
}
475+
476+
@Override
477+
public String toString() {
478+
return this.value;
479+
}
480+
}
425481
}

0 commit comments

Comments
 (0)