I have used JWT as an authentication token in my projects in recent years. I have always had a question: where should the JWT issued by the server to the browser be stored? Here we only discuss the browser scenario, in which there are three options. CookieThe server can send the JWT token to the browser through Cookie. When the browser requests the server interface, it will automatically carry the JWT token in the Cookie header. The server can verify the JWT token in the Cookie header to achieve identity authentication. However, it is vulnerable to CSRF attacks. The solution is to set the SameSite attribute of the cookie to Strict. Cookies will not be sent across sites. In other words, cookies will only be sent if the URL of the current web page is consistent with the request target. Cookies are vulnerable to CSRF attacks as well as XSS attacks. Hackers can read the information in cookies through JS scripts. To prevent this, you can set the cookie attribute to HttpOnly.
You can set its lifetime by setting Max-Age. localStorageLocalStorage can also store JWT tokens, which is not vulnerable to CSRF. However, unlike cookies, it does not automatically carry the token in the request, and needs to be implemented through code. However, this will be vulnerable to XSS attacks. In addition, if the user does not actively clear the JWT token, it will be stored in localStorage forever. sessionStorageMost of the features of sessionStorage are similar to localStorage, but its life cycle is different from localStorage. It is session-level storage and will be cleared after closing the page or browser. SummarizeYou may notice that all 3 methods have the same disadvantage – “vulnerable to XSS attacks”. Please pay special attention to XSS prevention and always follow the best practices of XSS protection. in conclusionAll three forms are vulnerable to XSS attacks, so if you have high security requirements, you need to configure them specifically. Among the three methods, Cookie provides a bunch of security options, such as SameSite, HttpOnly, etc. Therefore, it is best to use Cookie. This article is reprinted from the WeChat public account "Coder Xiaopangge", which can be followed through the following QR code. To reprint this article, please contact the WeChat public account of Coder Xiaopangge. |
<<: Facing Huawei 5G, the United States is showing its hand
>>: Aruba Again Named a Leader in Gartner Magic Quadrant for WAN Edge Infrastructure
Two of the five most important wireless technolog...
On May 27, 2024 , the 2024 SD-WAN+Innovation &quo...
This afternoon, Huawei's new product launch c...
Manufacturing has always been the lifeline of the...
[[393766]] What is 5G network? "5G" act...
As early as the World Telecommunication Day event...
I have shared information about GreenCloudVPS sev...
[[432368]] This year's 11.11 Shopping Festiva...
RAKsmart launched the "New Year's Big Di...
【51CTO.com original article】Just last week, the W...
In June, the three major operators who obtained 5...
With the exhaustion of IPv4 address resources and...
In this last technical article of 2020, I will ta...
In January, we shared the news about Maxthon'...
[[267883]] 1. Seven-layer network model The http ...