admin管理员组文章数量:1401193
Good day guys,
I'm working on a project that has Web API (RestAPI) and SPA(Single Page Application) solutions.
Based on the video that I was following on Udemy, he stored the jwt token in the localstorage but late I found out the storing in localstorage is a bit risky since the attacker can copy the actual token and make a request in the future.
I've read some blogs that storing token in the cookie is fine since you can set the cookie as httpOnly and secure. But the problem is, I don't know how to implement it.
Here's my sample code when the user has a valid login:
axios.post('api/login/', this.account).then(response=>{
if(response){
localStorage.setItem('token', response.data.token); // will successfully save to localstorage
// navigation here
}
}).catch(error=> console.log(error); );
How can I store this in cookie with secure settings?
Good day guys,
I'm working on a project that has Web API (RestAPI) and SPA(Single Page Application) solutions.
Based on the video that I was following on Udemy, he stored the jwt token in the localstorage but late I found out the storing in localstorage is a bit risky since the attacker can copy the actual token and make a request in the future.
I've read some blogs that storing token in the cookie is fine since you can set the cookie as httpOnly and secure. But the problem is, I don't know how to implement it.
Here's my sample code when the user has a valid login:
axios.post('api/login/', this.account).then(response=>{
if(response){
localStorage.setItem('token', response.data.token); // will successfully save to localstorage
// navigation here
}
}).catch(error=> console.log(error); );
How can I store this in cookie with secure settings?
Share Improve this question asked Oct 16, 2018 at 6:54 jsonGPPDjsonGPPD 1,0375 gold badges17 silver badges32 bronze badges 1- Which Udemy course were you following? I noticed that both Traversy's MERN course and Mosh' Node course have this problem. But it's likely that there are more courses with this problem. – Ben Commented Oct 26, 2019 at 7:36
3 Answers
Reset to default 3You can't set a HttpOnly cookie from client end code (like Javascript). As such cookies are meant not to be read using Javascript. You have to set such cookies from the server. You can send a cookie with the response of the server and browser will store them reading from the headers. After that browser will send that cookie to the server with every request send to the server untill the cookie expires.
You can set cookie from server as following..
Cookie cookie = new Cookie(name, value); //name and value of the cookie
cookie.setMaxAge(expire); //expire could be 60 (seconds)
cookie.setHttpOnly(true);
cookie.setPath("/");
response.addCookie(cookie);
I've read some blogs that storing token in the cookie is fine since you can set the cookie as httpOnly and secure. But the problem is, I don't know how to implement it.
You need to implement this on the server, not on the client.
Here is an example of the server code for a login endpoint:
// If the passwords match, generate a new jwt for this user
const token = user.generateAuthToken();
// Set the options for the cookie
let cookieOptions = {
// Delete the cookie after 90 days
expires: new Date(Date.now() + 90 * 24 * 60 * 60 * 1000 ),
// Set the cookie's HttpOnly flag to ensure the cookie is
// not accessible through JS, making it immune to XSS attacks
httpOnly: true,
};
// In production, set the cookie's Secure flag
// to ensure the cookie is only sent over HTTPS
if( process.env.NODE_ENV === 'production') {
cookieOptions.secure = true;
}
// Send a success response to the client
// including the jwt in a cookie
return res
.cookie('jwt', token, cookieOptions)
.status(200)
.json({
msg: 'Successfully logged in',
});
}
This looks similar to: Set a cookie to HttpOnly via Javascript
To add to the answer this source quotes:
To prevent cross-site scripting (XSS) attacks, HttpOnly cookies are inaccessible to JavaScript's Document.cookie API
In order to save the tokens using the httpOnly and secure flags, the server will have to response with this in the header (again taken from the above source):
Set-Cookie: id=a3fWa; Expires=Wed, 21 Oct 2015 07:28:00 GMT; Secure; HttpOnly
So, I don't think that you can save the cookies securely if the server is not responding with Set-Cookie header, and is rather returning the token as a response body.
本文标签: Storing Jwt Token in Cookie with Http and Secure instead of LocalStorage in JavascriptStack Overflow
版权声明:本文标题:Storing Jwt Token in Cookie with Http and Secure instead of LocalStorage in Javascript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744219033a2595775.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论