Springboot设置Cookie

Http请求的request header中,Cookie无法通过前端设置,必须通过后端的Set-Cookie设置。
并且Cookie使用有诸多细节,详细可参考Cookie文档。
众所周知,Spingboot内置了Tomcat,该依赖为

 <dependency>
   <groupId>org.springframework.bootgroupId>
   <artifactId>spring-boot-starter-tomcatartifactId>
 <dependency>
 <dependency>
  <groupId>org.apache.tomcat.embedgroupId>
     <artifactId>tomcat-embed-coreartifactId>
 dependency>

返回的Cookie设置在HttpServletResponse类中。
如果后端协议使用的Https,则注意设置Cookie的SameSite属性,否则将无法使用。
如果设置父域名的Cookie,则注意使用Cookie的domain和path属性,否则其他子域名无法使用。
目前HttpServletResponse类使用的Cookie并没有添加SameSite属性,所以不能通过addCookie函数添加,只能通过addHeader函数添加。如果使用setHeader函数,则只能携带一个Cookie。
示例:

response.addHeader(HttpHeaders.SET_COOKIE,"token=123;  Secure; SameSite=None; domain=.father.com; path=/");
response.addHeader(HttpHeaders.SET_COOKIE,"boeType=1; Secure; SameSite=None; domain=.father.com; path=/");

注意:domain使用父域名需要用 .father.com。path必须为 /

你可能感兴趣的:(spring,boot,firefox,后端)