我正在使用Spring 4,并使用request.getSession()
创建了一个会话
我已经观察到创建了一个SESSION cookie。 Response标头包含以下内容:
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中只有1个SameSite = Lax的SESSION cookie?
您正在手动发送Set-Cookie
标头,该标头与Spring的会话管理设置的标头重复。
[如果Spring 4允许为会话cookie设置SameSite
属性(不幸的是,我找不到此文档,所以不能确定),那么我希望它位于您的web.xml
中:
<session-config>
<cookie-config>
<http-only>true</http-only>
<secure>true</secure>
<!-- Maybe there's a SameSite option? -->
</cookie-config>
</session-config>