java - Spring 4 Session Cookie 更改字段
问题描述
我正在使用 Spring 4,并使用request.getSession()
我观察到创建了一个会话 cookie。响应头包含以下内容:
Set-Cookie: SESSION=ZTgwZWMxMDItOTA1MC00ZTZjLWIxMmUtZmM3NmQxNzJmNDBm; Path=/myApp/; Secure; HttpOnly
在创建的 Cookie 中,我需要 SameSite=Lax。目前,SameSite 没有价值。
所以在我的代码中,我做了以下尝试覆盖 SESSION cookie。
// request is of type HttpServletRequest
// response is of type HttpServletResponse
HttpSession session = request.getSession();
String base64value = Base64.getEncoder().encodeToString(session.getId().getBytes());
response.setHeader("Set-Cookie","SESSION=" + base64value + ";path=/myApp/ ;HttpOnly ;Secure;SameSite=lax");
但是现在创建了 2 个 SESSION cookie,并且可以在响应标头中看到:
Set-Cookie: SESSION=ZTgwZWMxMDItOTA1MC00ZTZjLWIxMmUtZmM3NmQxNzJmNDBm;path=/myApp/ ;HttpOnly ;Secure;SameSite=lax
Set-Cookie: SESSION=ZTgwZWMxMDItOTA1MC00ZTZjLWIxMmUtZmM3NmQxNzJmNDBm; Path=/myApp/; Secure; HttpOnly
我怎样才能在 Spring 4 中使用 SameSite=Lax 只有 1 个 SESSION cookie?
解决方案
您正在手动发送与Set-Cookie
Spring 会话管理设置的标头重复的标头。
如果 Spring 4 允许设置SameSite
会话 cookie 的属性(不幸的是,我找不到这方面的文档,所以不能确定),那么我希望它在你的web.xml
:
<session-config>
<cookie-config>
<http-only>true</http-only>
<secure>true</secure>
<!-- Maybe there's a SameSite option? -->
</cookie-config>
</session-config>
推荐阅读
- oracle - 为什么 Oracle 数据库跟踪文件中出现 TNS 错误
- pine-script - 止损超时
- android - 用联结表连接三个表
- python - 编写 CLI 时如何从子目录中导入类?
- scala - Flink的DataStream的timeWindow有问题[求助]
- ruby-on-rails - 如何在没有 Webpacker 的情况下将 Tailwind CSS 添加到 Rails?
- c++ - ptrace(PTRACE_PEEKDATA) ,从内存地址读取问题
- javascript - 如何在浏览器同步存储中建立多对多关系?
- ios - 如何将单元格宽度与框架宽度不同的 UICollectionviewCell 居中并启用分页
- handlebars.js - 这段代码在 handlebar.js {{{number this.col1}}} 中是什么意思?