【问题标题】:SetSecure to true for a Cookie将 Cookie 设置为 true
【发布时间】:2016-12-29 16:51:07
【问题描述】:

我正在尝试创建一个cookieSetSecure 作为true。这会在保存或更新方法中产生问题,并且系统会重定向到错误页面/抛出 403 错误。如果我删除SetSecure,那么它工作正常。

Cookie ck= new Cookie("key",value);
ck.setsecure(true);

response.addCookie(ck);// HttpResponse

Q1) 只设置secure flag to true 可以吗?还是我必须处理我的webapp 中的更多更改?

Q2) 我正在使用http 连接到我的webapp。这是SetSecure flaghttp protocol 一起使用吗?还是必须是Https

提前致谢。

【问题讨论】:

    标签: java spring-mvc jakarta-ee cookies spring-security


    【解决方案1】:

    安全标志为 true 的 cookie 仅表示如果连接未加密(例如在 http 协议中),另一端的浏览器不会将其发送到服务器。"The purpose of the secure flag is to prevent cookies from being observed by unauthorized parties due to the transmission of a the cookie in clear text."

    因此,在您的情况下,cookie 在创建后将永远不会被发送回服务器,这就是您收到错误的原因。如果您出于某种原因确实需要使用此标志,则应使用 https 服务器。

    【讨论】:

    • 我使用的是 https。但我仍然收到这个 403 错误。
    • 如果我的 cookie.setsecure() 真的设置为 true,如何测试这个
    • @ShubhroMukherjee,在浏览器的开发工具“网络”选项卡中,您可以检查任何请求和响应的所有标头,例如,安全 cookie 可能如下所示:Set-Cookie: JSESSIONID =someId123;路径=/;仅http; 安全; SameSite=strict
    猜你喜欢
    • 2012-04-02
    • 1970-01-01
    • 2013-12-04
    • 2021-09-05
    • 1970-01-01
    • 1970-01-01
    • 2018-01-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多