advancedJWT: Token-Based Authentication

What's the security risk of storing a JWT in browser localStorage versus an HttpOnly cookie?

localStorage is accessible to any JavaScript running on the page, including malicious scripts injected via an XSS vulnerability — making token theft straightforward if XSS exists anywhere on the site. An HttpOnly cookie can't be read by JavaScript at all, mitigating XSS-based theft, but re-introduces CSRF risk (since browsers auto-attach cookies), which must then be mitigated with CSRF tokens or SameSite cookie attributes.

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Spring Ecosystem Mastery library.

What's the security risk of storing a JWT in browser localStorage versus an HttpOnly cookie?

Next Step

Continue to What's the difference between an opaque token and a JWT (self-contained token) from the resource server's perspective?← Back to all Spring Security questions