【问题标题】:Spring 4 Session Cookie change fieldSpring 4 Session Cookie 更改字段
【发布时间】:2020-05-12 14:02:49
【问题描述】:

我正在使用 Spring 4,并使用 request.getSession() 创建一个会话

我观察到创建了一个 SESSION 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?

【问题讨论】:

    标签: java spring tomcat cookies spring-4


    【解决方案1】:

    您正在手动发送 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>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-03-23
      • 1970-01-01
      • 1970-01-01
      • 2016-01-15
      • 2023-01-16
      • 2021-10-15
      • 1970-01-01
      相关资源
      最近更新 更多