为什么我的 set-cookie 在我发出后续请求后会被删除

问题描述 投票:0回答:1

问题是,当我使用端点登录时,它会在响应标头中返回一个 set-cookie,它是一个仅 http 的 cookie,但是当我对其他端点执行其他请求时,我在中看不到 set-cookie请求标头,我已尝试解决此问题

下面是 service.ts 中的角度代码,

private apiUrl = environment.baseUrl +  '/api/v1/login';
private apiUrl2 = environment.baseUrl + '/api/v1/anotherRequest';
loginAdmin(req: Login): Observable<string> {
    const headers = new HttpHeaders({'Content-Type': 'application/json'});
    return this.httpClient.post<string>(this.apiUrl,JSON.stringify(req),{ headers })
  }
anotherRequestBeingMade(): Observable<any]> {
    const headers = new HttpHeaders({'Content-Type': 'application/json'});
   
    return this.httpClient.get<any>(this.apiUrl2,{withCredentials:true})
  }

这就是我使用 golang 在光纤中设置 http only cookie 的方式

cookie := fiber.Cookie{
        Name:     "my_cookie",
        Value:    *thereisavaluehere,
        Expires:  time.Now().Add(time.Hour * 24),
        HTTPOnly: true,
        Secure:   true,
        SameSite: "None",
        Path:     "/",
    }

    c.Cookie(&cookie)

我尝试过的:

  • 我已将

    {withCredentials:true} 
    包含在 get httpClient 中

  • 我尝试修复我的后端中用 golang 编写的 cors 问题

  app.Use(cors.New(cors.Config{
       AllowOrigins:     "http://localhost:4200",
       AllowHeaders:     "Origin, Content-Type, Accept",
       AllowMethods:     "GET, POST, PUT, DELETE, OPTIONS",
       ExposeHeaders:    "Set-Cookie",
       AllowCredentials: true,
   }))
  • 通过在与 package.json 相同级别创建 proxy.conf.json 并配置 package.json 脚本对象来尝试绕过代理,部分以
  • 开头
"start": "ng serve --proxy-config proxy.conf.json",
{
"/api/*": {
  "target": "http://localhost:8080",
  "secure": false,
  "logLevel": "debug"
}
}

然后我在 Angular 应用程序的 src 文件夹中添加了一个 enviroment.ts 文件,其中有

export const environment = {
    production: false,
    baseUrl: 'http://localhost:8080' // Adjust this to match your backend server URL
  };

提前谢谢您

angular typescript go cors
1个回答
0
投票

当我使用端点登录时,它会在响应标头中返回一个 set-cookie,它是一个仅 http 的 cookie,但是当我对其他端点执行其他请求时,我在请求标头中看不到 set-cookie”

这不是问题。

Set-Cookie
header 只需要在登录时设置。不需要在响应每个请求时指定它来“维持”cookie 的存在。

客户端的类似物是

Cookie
标头。检查浏览器的开发人员工具,确保后续请求包含
Cookie
标头。

© www.soinside.com 2019 - 2024. All rights reserved.