Where Should You Store JWT in the Browser? Cookie vs localStorage vs sessionStorage

This article compares three browser storage options for JWT—Cookie, localStorage, and sessionStorage—examining their automatic transmission, CSRF and XSS vulnerabilities, and security configurations such as SameSite and HttpOnly to help developers choose the safest method.

macrozheng
macrozheng
macrozheng
Where Should You Store JWT in the Browser? Cookie vs localStorage vs sessionStorage

In recent projects I have used JWT as an authentication token and often wonder where the server‑issued JWT should be stored in the browser. In a browser‑only scenario there are three choices.

Cookie

The server can send the JWT via a Cookie; the browser automatically includes the Cookie in subsequent requests, and the server validates the token. However, this approach is vulnerable to CSRF attacks.

To mitigate CSRF, set the Cookie's SameSite attribute to Strict, which prevents the Cookie from being sent on cross‑site requests.

Cookies are also exposed to XSS attacks, as malicious JavaScript can read them. Setting the HttpOnly attribute prevents client‑side scripts from accessing the Cookie.

response.setHeader("Set-Cookie","jwt=jwt_value;Path=/;Domain=domainvalue;Max-Age=seconds;HttpOnly");
You can control the token's lifetime by setting the Max-Age attribute.

localStorage

localStorage can also hold the JWT. It is not susceptible to CSRF because the token is not sent automatically, but the application must add it to requests manually. This method is vulnerable to XSS, and the token persists in localStorage until the user explicitly clears it.

sessionStorage

sessionStorage behaves similarly to localStorage, but its lifecycle is limited to the browsing session; the data is cleared when the page or browser is closed.

Summary

All three methods share the same major drawback—susceptibility to XSS attacks. Developers should pay special attention to XSS protection and follow best practices.

Conclusion

Because all three storage options can be attacked via XSS, high‑security applications should apply targeted configurations. Cookies provide a range of security options such as SameSite and HttpOnly, making them the preferred choice for storing JWTs.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

CSRFXSSJWTWeb SecurityCookielocalStoragesessionStorage
macrozheng
Written by

macrozheng

Dedicated to Java tech sharing and dissecting top open-source projects. Topics include Spring Boot, Spring Cloud, Docker, Kubernetes and more. Author’s GitHub project “mall” has 50K+ stars.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.