【问题标题】:Set-cookie ignored for HTTP response with status 302 in Spring Boot application在 Spring Boot 应用程序中忽略状态为 302 的 HTTP 响应的 Set-cookie
【发布时间】:2019-12-11 21:37:49
【问题描述】:

我通过spring security实现认证。

如果我收到 targetUrl 并且身份验证成功,则重定向到该 targetUrl 并使用该域创建 Cookie。

但是,在本地环境中测试时,Set-Cookie 标头不起作用。

请求

Request URL: http://auth.develop.in/login?targetUrl=https://test2.test.develop.in/
Request Method: POST
Status Code: 302 
Remote Address: 127.0.0.1:80
Referrer Policy: no-referrer-when-downgrade

响应

Response Header
Content-Length: 0
Date: Sat, 03 Aug 2019 16:38:06 GMT
Location: https://test2.test.develop.in/
Set-Cookie: JWT=eyJ0eXAiOiJKV1QiLpEls; Domain=test.develop.in; Path=/
X-Content-Type-Options: nosniff
X-Frame-Options: DENY
X-XSS-Protection: 1; mode=block

代码

fun setCookie(request: HttpServletRequest, response: HttpServletResponse, value: String) {
    val cookie = Cookie(cookieName, value)
    cookie.path = getCookiePath(request)
    if (cookieDomain != null) {
        cookie.domain = cookieDomain
    }
    response.addCookie(cookie)
}
Cookie {Cookie@9383}
name = "JWT"
value = "eyJ0eXAiOiJKV1Q~~"
version = 0
comment = null
domain = "test.develop.in"
maxAge = -1
path = "/"
secure = false
httpOnly = false

我希望Cookie在正常重定向后创建。

关于 Cookie 有什么我不明白的地方吗?

如果我不重定向,则会创建 cookie。

【问题讨论】:

  • 你的意思是什么在本地环境中测试时Set-Cookie标头不起作用。显示您对本地环境的请求。
  • @dur Set-Cookie 标头设置并重定向,但未创建任何 cookie。
  • 显示您对当地环境的要求。

标签: spring-boot google-chrome redirect cookies spring-security


【解决方案1】:

问题解决了。

无法在 auth.develop.in 上为 test.develop.in 创建 cookie

将 /etc/hosts 域 auth.develop.in 更改为 auth.test.develop.in

【讨论】:

    猜你喜欢
    • 2015-10-08
    • 2021-02-25
    • 2012-05-27
    • 2023-03-11
    • 1970-01-01
    • 2017-03-31
    • 2020-05-04
    • 2011-09-27
    • 2013-02-08
    相关资源
    最近更新 更多