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
In November, we not only have our Double Eleven S...
Switching technology is one of the important tech...
PacificRack is now offering a promotion for Multi...
The tribe has shared news about edgeNAT twice. Th...
As 5G technology develops rapidly, Wi-Fi technolo...
According to the latest "SASE & SD-WAN &...
Currently, the financial industry is in a critica...
On September 5, during HUAWEI CONNECT 2017, Huawe...
The Open Systems Interconnection (OSI) model is a...
September 22, 2021 – Aruba, a Hewlett Packard Ent...
Previous article: "The Internet of Things in...
Starting at 1:35 a.m. local time on July 2, a lar...
HostYun launched this year's Double 11 promot...
China Telecom leads strongly, China Mobile overta...
As we all know, Ethernet has become the most wide...