












You are absolutely right: Traditional web projects use Session/Cookie, while backend-frontend separated interface projects (RESTful APIs) must use JWT The core reason is the architecture has changed — traditionally, the server rendered pages, now it is frontend independent, interfaces stateless.
I will explain the differences between the two in the most popular and core way:
SessionID That Exists in Cookie
Therefore: Interface authentication must discard the "server-side state storage" approach → JWT emerges
JWT = JSON Web Token
In a nutshell: Encrypt user identity information into a string and store it on the client side, with the server not storing any login state .
Workflow:
Table
| Comparison Dimension | Session / Cookie | JWT |
|---|---|---|
| Storage Location | Server-side storage, client only stores ID | All exist on the client, zero storage on the server |
| State | Stateful(Server needs to check session) | Stateless(Stateless, interface naturally supports distributed) |
| Cross-domain / Cross-end | Poor(Relies on Cookies, large cross-domain restrictions) | Extremely strong(Supports Web, mini programs, App, third-party interfaces) |
| Distributed deployment | Troublesome(Requires session sharing) | Extremely Simple(Multiple servers use the same key for verification) |
| Request Method | Browser automatically carries Cookies | FrontendManually transmitted(Request headers / Parameters) |
| Security Features | Vulnerable to CSRF attacks | No CSRF risk |
| Applicable Scenarios | Traditional websites, server-side rendering | Frontend-backend separation, RESTful API, microservices |
You can simply remember:
Use Session for websites, use JWT for interfaces; stateful use Session, stateless use JWT.
<!-- JWT 工具:jjwt,Spring5 兼容 -->
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
This content is automatically aggregated by InertiaRSS (RSS Reader) for reading reference only. Original from — Copyright belongs to the original author.