I developed a spring boot api using jwt auth and I got the endpoint
'http://localhost:8080/api/signin'
If the login is successful, it will return a JSESSIONID, which is a jwtToken. So far so good, but I can get other methods from the API in the browser since the cookie is not passed in the request body.
The cookie generated by jwt is as follows: JSESSIONID=eyJhbGciOiJIUzUxMiJ9.eyJzdWIiOiJhZG1pbiIsImlhdCI6MTY4NTE1Mzk5NSwiZXhwIjoxNjg1MjQwMzk1fQ.9jxHyzDJKVra8IryxbH8se0xSl4_Dka p NsKmjRCvlJs_R8M3x3RBMeo-1VPAJv6YSQwC6ukJutRwGEyfeYrGwQ;path=/;Http only;expire=Sunday, May 28, 2023 02:38:33 GMT;
For example: If I do this
curl --location 'http://localhost:8080/user/admin/all' --header 'Cookie: JSESSIONID=mycookie'
Everything goes fine and returns the json I want, but when I run it on the browser, in the js script on my frontend
axios .get("http://localhost:8080/user/admin/all", { headers: { Cookie: "JSESSIONID=mycookie", }, }) .then((response) => { console.log(response.headers); console.log(response.data); }) .catch((error) => { console.error("Erro:", error); });
I get this error:
GET http://localhost:8080/user/admin/all 401
Cookie not set. I tried so many things and almost lost my mind. Do you know what it will be?
You need to set
withCredentials: true