登录
首页 >  Golang >  Go问答

响应中出现了CORS set-cookie 标头,但未生成 cookie

来源:stackoverflow

时间:2024-03-05 22:33:32 270浏览 收藏

IT行业相对于一般传统行业,发展更新速度更快,一旦停止了学习,很快就会被行业所淘汰。所以我们需要踏踏实实的不断学习,精进自己的技术,尤其是初学者。今天golang学习网给大家整理了《响应中出现了CORS set-cookie 标头,但未生成 cookie》,聊聊,我们一起来看看吧!

问题内容

我正在尝试从go(127.0.0.1:8080)中创建的api在我的客户端(127.0.0.1:3000)中创建一个cookie,我想我已经在cors和所需的响应中添加了大部分标头cookie 具有 samesite=nonesecure=true 但仍未设置。下面我添加了请求和响应的图片,如 chrome devtools 网络选项卡中所示,以及发出请求的 axios 代码和在 api 服务器中处理请求的 go 代码。

开始:

//handles account sign ins
func login(w http.responsewriter, r *http.request, _ httprouter.params) {
    fmt.println("login handler")
    //decoder := json.newdecoder(r.body)
    //var t models.loginuserdata
    //err := decoder.decode(&t)
    //if err != nil {
    //  log.println(err)
    //}
    //middleware.signin(t.user.username, t.user.password)

    http.setcookie(w, &http.cookie{name: "sabor", value: "merda", path: "/", httponly: false, samesite: http.samesitenonemode, secure: true, domain: "127.0.0.1"})
    header := w.header()
    header.set("access-control-allow-credentials", "true")

    //header.set("access-control-expose-headers", "set-cookie")
    header.set("access-control-allow-headers", "content-type, withcredentials")
    header.set("access-control-allow-origin", "http://127.0.0.1:3000")
    header.set("access-control-allow-methods", "post, get, options")
    w.writeheader(http.statusok)
}

axios:

let config = {
      headers: {
        withCredentials: true,
      },
    };

axios
      .post(
        "http://127.0.0.1:8080/login",

        {
          User: {
            Username: username,
            Password: password,
          },
        },

        config
      )
      .then(function (response) {
        console.log(response)
        console.log(response.data)
        console.log(response.headers)
        console.log(response.data.cookie)
        if (response.status === 200) {
          console.log("if works")
          
        }
      })
      .catch(function (error) {
        console.log(error);
      });

请求和响应图像: 请求和响应图像


正确答案


您将 cookie 标记为“安全”(https),但对您的 api 的请求是通过不安全 (http) 连接发出的。

您可以尝试以下操作;

func Login(w http.ResponseWriter, r *http.Request, _ httprouter.Params) {
    // In case you don't have separate CORS middleware
    if r.Method == http.MethodOptions {
        header := w.Header()
        header.Set("Access-Control-Allow-Credentials", "true")
        header.Set("Access-Control-Allow-Headers", "Content-Type, withCredentials")
        header.Set("Access-Control-Allow-Origin", "http://localhost:3000")
        header.Set("Access-Control-Allow-Methods", "POST, GET, OPTIONS")
        w.WriteHeader(http.StatusOK)
        return
    }
    
    http.SetCookie(w, &http.Cookie{Name: "sabor", Value: "merda", Path: "/", HttpOnly: false, SameSite: http.SameSiteNoneMode, Secure: false, Domain: "localhost"})
    
}

从 axios 中,只需将端点替换为 localhost; axios.post( “http://localhost:8080/登录”, ...

今天关于《响应中出现了CORS set-cookie 标头,但未生成 cookie》的内容就介绍到这里了,是不是学起来一目了然!想要了解更多关于的内容请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>