Cookie 是可以伪造的,比如说学生证是可以伪造的
Session 是不可以伪造的,这是学校系统记录在册的
如何获取 Cookie
我们先用 Servlet 原生的获取 cookie 的方式

我们在浏览器进行访问

但是实际上目前是没有 cookie 的,我们按 F12 进行添加

然后再重新访问,就能在 idea 看到 cookie 了

接下来我们用 String 的方式获取 cookie

这样就能获取到 cookie 了

也可以一次性拿两个,自己设置,想拿几个拿几个
我们发现 Servlet 的方式可以拿到所有 cookie ,但是 Spring 的方式只能一个一个地拿 cookie
实际中具体情况具体选择哪种方式
获取 Session
Session 的结构内容如下


这是原始的获取 Session 的方式

刚开始的时候 Session 为空

所以接下来我们先存放一个 Session

运行并且访问之后发现 cookie 中多了一行数据

用 fiddler 抓包就能发现这行数据是怎么来的,服务器告诉客户端要帮我们设置一个cookie,设置的cookie 内容如下,然后客户端接收到之后就会在 cookie 中加入这行内容,这就是 cookie 和 Session的关系,sessionId 可以看做他们俩之间的桥梁

此时就可以 getSession 了

Cookie 和 Session 之间主要是通过 SessionId 关联起来的.SessionId 是 Cookie 和 Session 之间的桥梁
通过 Spring 来获取 Session

此时运行程序之后 Session 就会被清空,所以我们在此之前还需要先进行 setSession,再进行getSession2
下面是另一种方式获得 Session

获取 header
我们先用传统方式获取 user-agent,获取使用的是哪个浏览器


这样我们就拿到了浏览器的信息

下面是使用注解的方式获取 header
