首页 > 其他分享 >springboot 低于 2.6 版本设置 SameSite=None,springboot 1.x set SameSite=none in embedded tomcat

springboot 低于 2.6 版本设置 SameSite=None,springboot 1.x set SameSite=none in embedded tomcat

时间:2023-05-13 20:24:57浏览次数:37  
标签:None return String tomcat value SameSite SameSiteCookies springboot

speingboot 使用自带的 tomcat 运行,设置 SameSite。

springboot 过低的版本没有 SameSite 的属性设置,升级到 1.5.22 版本后,虽然 Rfc6265CookieProcessor 有 setSameSiteCookies 方法,但是方法逻辑有 BUG,当不是 None 时才可以设置成功:

SameSiteCookies sameSiteCookiesValue = this.getSameSiteCookies();
if (!sameSiteCookiesValue.equals(SameSiteCookies.NONE)) {
header.append("; SameSite=");
header.append(sameSiteCookiesValue.getValue());
}

但 SameSite 只是一串文本,因此,根据新版本的 tomcat 改写旧版本的 Rfc6265CookieProcessor 可以实现目的。

在 EmbeddedServletContainerFactory 中设置 TomcatContextCustomizers:

// 从配置文件读取值:custom.session.cookie.same-site=None
@Value("${custom.session.cookie.same-site:}")
private String sameSite;


@Bean public EmbeddedServletContainerFactory webServerFactory() { TomcatEmbeddedServletContainerFactory factory = new TomcatEmbeddedServletContainerFactory(); // 设置 tomcat 上下文 factory.setTomcatContextCustomizers(Collections.singletonList(sameSiteCookiesConfig())); return factory; } /** * 自定义 tomcat 上下文,设置 SameSite * * @return tomcat 上下文 */ public TomcatContextCustomizer sameSiteCookiesConfig() { return context -> { final Rfc6265CookieProcessor cookieProcessor = new Rfc6265CookieProcessor() { @Override public String generateHeader(Cookie cookie) { String sameSiteAppend = null; if (StringUtils.hasText(sameSite)) { sameSiteAppend = "; SameSite=" + SameSiteCookies.fromString(sameSite).getValue(); } return super.generateHeader(cookie) + sameSiteAppend; } }; context.setCookieProcessor(cookieProcessor); }; } /** * SameSite 枚举 */ enum SameSiteCookies { UNSET("Unset"), NONE("None"), LAX("Lax"), STRICT("Strict"); private final String value; SameSiteCookies(String value) { this.value = value; } public String getValue() { return this.value; } public static SameSiteCookies fromString(String value) { SameSiteCookies[] values = values(); StringBuilder builder = new StringBuilder("["); for (SameSiteCookies sameSiteCookies : values) { if (sameSiteCookies.value.equals(value)) { return sameSiteCookies; } builder.append(sameSiteCookies.getValue()).append(", "); } String substring = builder.substring(0, builder.length() - 2) + "]"; throw new IllegalStateException("invalid sameSite cookies: [" + value + "], valid sameSite value is " + substring); } }

 

Set-Cookie 似乎仅在首次访问时会响应(有时间去查一下),因此,页面 F12 DEBUG 时需要查看第一个请求才能看到 Set-Cookie 请求头信息。

标签:None,return,String,tomcat,value,SameSite,SameSiteCookies,springboot
From: https://www.cnblogs.com/64gdrifbottle/p/17398096.html

相关文章

  • Springboot 开启异步任务Async,邮件发送任务,定时任务
    异步任务1.主启动类开启异步注解 2.service目录下开启异步任务注解@ServicepublicclassAsyncService{@Async//异步任务注解的标志publicvoidhello(){try{Thread.sleep(3000);}catch(InterruptedExceptione){......
  • Stream流anyMatch,allMatch,noneMatch
    publicclassActor{privateStringname;privateintage;privateList<Person>personList=newArrayList<Person>();publicStringgetName(){returnname;}publicvoidsetName(Stringname){this.na......
  • SpringBoot整合Mybatis
    SpringBoot整合MyBatisSpringBoot整合MyBatisSpringBoot是一个快速开发应用程序的框架,而MyBatis是一个提供ORM支持的优秀框架。在本文中,我们将学习如何将SpringBoot与MyBatis整合,以便我们能够更加轻松地开发Web应用程序。步骤创建新的SpringBoot项目。在pom.xml文件中添加My......
  • java基于springboot+vue的农机电招平台、农机租赁管理系统,附源码+数据库+文档+PPT,适合
    1、项目介绍该系统包括前台操作和后台管理两个部分,一方面,为用户提供首页,农机,系统公告,个人中心,后台管理等功能;另一方面,为管理员提供首页,个人中心,农机机主管理,使用者管理,农机类型管理,农机管理,农机预约管理,系统管理等功能。项目获取,看这里2、技术框架编程语言:java系统架构:B/S......
  • SpringBoot 依赖注入方式
    前置知识SpringDI(DependencyInjection)依赖注入:组件之间依赖关系由容器在运行期间决定,即由容器动态的将某个依赖关系注入到组件中谁依赖谁:应用程序依赖IOC容器为什么需要依赖:应用程序需要IOC容器提供对象需要的外部资源谁注入谁:IOC容器注入应用程序某个对象,应用程序依赖的......
  • SpringBoot集成Jpa对数据进行排序、分页、条件查询和过滤
    之前介绍了SpringBoot集成Jpa的简单使用,接下来介绍一下使用Jpa连接数据库对数据进行排序、分页、条件查询和过滤操作。首先创建Springboot工程并已经继承JPA依赖,如果不知道可以查看我的另一篇文进行学习,这里不做介绍。文章地址(https://www.cnblogs.com/eternality/p/17391141.html......
  • springboot 大文件切片上传
    1.前端(vueelementui&原生)初始变量声明: currentFile:{},//当前上传的文件bigFileSliceCount:20,//大文件切片后的子文件数量(也可使用其它限定方式,如按照文件大小,每10MB切一片,此处采用的是固定切片的子文件数量的方式倒推切片大小) 接口:切片上传图片&合并......
  • java基于springboot+html的学生就业管理系统的设计与实现,附源码+数据库+文档,包安装调
    1、项目介绍本系统是利用现代化的计算机网络技术将传统信息宣传方式整合,按照实践过程设计完成的。同时完善服务,初步设计一个学生就业管理系统平台以利于相关的事务操作。为了使系统在各项管理中发挥更大的作用,实现计算机信息化高效的管理,现将开发目标功能需求介绍如下:(1)管理员模......
  • SpringBoot中单元测试如何对包含AopContext.currentProxy()的方法进行测试
    今天在工作中遇到一个问题,一个Service类中有一个方法,其中使用了AopContext.currentProxy()去访问自身的函数,例如intresult=((OrderServiceImpl)AopContext.currentProxy()).save();单元测试方法如下:@InjectMocksprivateOrderServiceImplorderServiceUnderTest;@Tes......
  • SpringBoot3.x中spring.factories SPI 服务发现机制的改变
    目录一、基础背景二、服务发现接口spring.factories三、服务发现机制调用1.启动SpringApplication2.加载SpringApplication.run1.SpringApplication.createApplicationContext2.SpringApplication.prepareContext3.SpringApplication.refreshContext4.AutoConfigurationImportSele......