问题是,当我使用端点登录时,它会在响应标头中返回一个 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,
}))
"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
};
提前谢谢您
当我使用端点登录时,它会在响应标头中返回一个 set-cookie,它是一个仅 http 的 cookie,但是当我对其他端点执行其他请求时,我在请求标头中看不到 set-cookie”
Set-Cookie
header 只需要在登录时设置。不需要在响应每个请求时指定它来“维持”cookie 的存在。
Cookie
标头。检查浏览器的开发人员工具,确保后续请求包含 Cookie
标头。