Add SameSite attribute to cookie in Java

(20/01/08 19:45 postscript)


javax.servlet.http.Cookie does not have an API to grant the SameSite attribute. It is a response in such a case. By the way, the SameSite attribute is supported by almost all browsers. Reference

Use the Set-Cookie header instead of the cookie class

I will try it with Spring boot.

public class DemoController {

    @GetMapping(value = "/")
    public String index(HttpServletResponse response) {

	    String name = "name";
	    String value = "takeshi";

	    String cookie = String.format("%s=%s; max-age=3600; Path=/; HttpOnly; Secure; SameSite=Lax;", name, value);

	    response.addHeader("Set-Cookie", cookie);

	    return "hello, world!";

Screen Shot 2019-08-06 at 17.11.55.png

Takeshi has entered safely.

Spring Boot

Spring Boot has a class called ResponseCookie that creates a cookie string for the header. If you use this class and return the response set by Set-Cookie, the cookie will be set.

public class DemoController {
    @GetMapping(value = "/")
    public String index(HttpServletRequest request, HttpServletResponse response) {

        String name = "name";
        String value = "takeshi";

        ResponseCookie cookie = ResponseCookie.from(name, value).domain("localhost").maxAge(Duration.ofDays((long) 1))
        response.addHeader("Set-Cookie", cookie.toString());

        return "Hello, World";


If you write cookies frequently, it seems better to create a custom class that receives and sets parameters. Alternatively, you can make batch settings on the Web server side such as Apache or nginx. Reference


Class Cookie

Class ResponseCookie

Recommended Posts

Add SameSite attribute to cookie in Java
Add multi-letter watermark to Java Word
Multithreaded to fit in [Java] template
How to learn JAVA in 7 days
Add watermark to Java to PDF document
How to use classes in Java?
[Java] Add WordArt to Word document
Add / remove watermark to Java PowerPoint
How to concatenate strings in java
Java (add2)
Java (add)
How to implement date calculation in Java
How to implement Kalman filter in Java
Multilingual Locale in Java How to use Locale
Try to solve Project Euler in Java
Easy to make Slack Bot in Java
Java reference to understand in the figure
[Java] How to add data to List (add, addAll)
How to add jar file in ScalaIDE
How to do base conversion in Java
Convert SVG files to PNG files in Java
How to implement coding conventions in Java
Add Document to Azure Search Service (Java)
How to get the date in java
[Java Bronze] 5 problems to keep in mind
Sample to unzip gz file in Java
How to specify id attribute in JSF
Java to C and C to Java in Android Studio
If you want to dynamically embed values & add text to attribute values in Thymeleaf 3
Reason to add L to the number to be put in Java long type
SameSite cookie in Spring Boot (Spring Web MVC + Tomcat)
Add original weapons to mods in Minecraft Forge 1.15.2
Two ways to start a thread in Java + @
[Java] Add quotes only to specific CSV columns
Add --enable-preview option in Java in Visual Studio Code
I want to send an email in Java.
How to display a web page in Java
CORBA seems to be removed in Java SE 11. .. ..
Code to escape a JSON string in Java
Try to create a bulletin board in Java
Changes in Java 11
How to add sound in the app (swift)
I tried to implement deep learning in Java
Rock-paper-scissors in Java
How to get Class from Element in Java
There seems to be no else-if in java
How to add a classpath in Spring Boot
rsync4j --I want to touch rsync in Java.
How to hide null fields in response in Java
Add, read, and delete Excel comments in Java
How to conditionally add html.erb class in Rails
Library "OSHI" to get system information in Java
[Java] Introduction to Java
Add classpath: to the path specified in spring.datasource.schema
[Java] Article to add validation with Spring Boot 2.3.1.
[Java] How to substitute Model Mapper in Jackson
Introduction to java
How to solve an Expression Problem in Java
I tried to create Alexa skill in Java
How to write Java String # getBytes in Kotlin?
Things to watch out for in Java equals