Springboot设置Cookie

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

 <dependency>
   <groupId>org.springframework.boot</groupId>
   <artifactId>spring-boot-starter-tomcat</artifactId>
 <dependency>
 <dependency>
  <groupId>org.apache.tomcat.embed</groupId>
     <artifactId>tomcat-embed-core</artifactId>
 </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必须为 /

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值