Last active
July 2, 2019 15:14
-
-
Save SharkFourSix/798a6c46151496837d290428ab661d7b to your computer and use it in GitHub Desktop.
Anti-XSRF Cookies in Spark Java using 'SameSite' flag
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class UsageTest { | |
public static void main(String... args) { | |
port(8080); | |
get("/", (request, response) -> { | |
// Create cookie | |
AntiXsrfCookie.CookieBaker baker = | |
AntiXsrfCookie.CookieBaker.with("chocolate_chip", "yummy!") | |
.secure() | |
.strict() | |
.domain("domain.tld") | |
.invisible() | |
.path("/") | |
.maxAge((int) TimeUnit.HOURS.toSeconds(1)); | |
response.header("Set-Cookie", baker.build()); | |
response.type("text/html"); | |
halt(200, "<h1>Done!</h1>"); | |
return response; | |
}); | |
post("/delete-cookie", (request, response) -> { | |
AntiXsrfCookie.CookieBaker baker = | |
AntiXsrfCookie.CookieBaker.with("chocolate_chip", "") | |
.path("/") | |
.domain("domain.tld") | |
.maxAge(-1); | |
response.header("Set-Cookie", baker.build()); | |
response.type("text/html"); | |
halt(200, "<h1>Cookie is expired. No longer yummy :(</h1>"); | |
return response; | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment