问题是,当我使用端点登录时,它在响应标头中返回一个set-cookie,这是一个仅http的cookie,但当我对其他端点执行其他请求时,我在请求标头中看不到set-cookie,我已try 修复此问题
下面是一个service.ts中的Angular 代码,
private apiUrl = environment.baseUrl + '/api/v1/login';
private apiUrl2 = environment.baseUrl + '/api/v1/anotherRequest';
login(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)
我在Postman上测试了它,它在响应头为Set-Cookie
的登录端点上设置了cookie.当执行后续请求时,它显示在请求头Cookie
上,但当我在Angular客户端上测试时,Set cookies
在执行登录端点时仍然存在,但Cookie
头在后续请求中不可用.
我try 了以下几点:
-
我已经在get httpClient中包含了
{withCredentials:true}
个 -
我试着在我的后端修复cors的问题,这是写在golang
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,
}))
- try 绕过代理,方法是在与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
};
先谢谢你