网站首页 > 技术文章 正文
- Cookie 和 Session 都是用来跟踪浏览器用户身份的会话方式,但是两者的应用场景不太一样。
- Cookie 一般用来保存用户信息
- 我们在 Cookie 中保存已经登录过得用户信息,下次访问网站的时候页面可以自动帮你登录的一些基本信息给填了
- 一般的网站都会有保持登录也就是说下次你再访问网站的时候就不需要重新登录了,这是因为用户登录的时候我们可以存放了一个 Token 在 Cookie 中,下次登录的时候只需要根据 Token 值来查找用户即可(为了安全考虑,重新登录一般要将 Token 重写);
- 登录一次网站后访问网站其他页面不需要重新登录。Session 的主要作用就是通过服务端记录用户的状态。 典型的场景是购物车,当你要添加商品到购物车的时候,系统不知道是哪个用户操作的,因为 HTTP 协议是无状态的。服务端给特定的用户创建特定的 Session 之后就可以标识这个用户并且跟踪这个用户了。
- Cookie 数据保存在客户端(浏览器端),Session 数据保存在服务器端。
- Cookie 存储在客户端中,而 Session 存储在服务器上,相对来说 Session 安全性更高。如果要在 Cookie 中存储一些敏感信息,不要直接写入 Cookie 中,最好能将 Cookie 信息加密然后使用到的时候再去服务器端解密。
#Cookie
服务器端返回数据的时候通过 Set-Cookie 设置 Header,设置到浏览器中;浏览器保存了这个 Cookie 之后,在下次请求的时候(同域)会携带这个 Cookie,来保证记录用户状态(因为 Http 是无状态的);如果在请求头中没有设置 Cookie 的过期时间,默认是浏览器关闭后 Cookie 就会清空。
#属性
- max-age 和 expires 设置过期时间(expires 是在哪个时间点过期,max-age 是多长时间)
- Secure 只在 https 的时候发送
- HttpOnly 无法通过 document.cookie 访问
#node 实践
#设置 Cookie
const http = require('http')
const fs = require('fs')
http.createServer(function(req, res) {
if (req.url === '/') {
const html = fs.readFileSync('test.html', 'utf-8')
res.writeHead(200, {
'Content-Type': 'text/html',
'Set-Cookie': 'id=123'
// 可以设置多个 Cookie
'Set-Cookie': ['id=123', 'abc=456']
})
res.end(html)
}
}).listen(8888)
document.cookie 得到的结果:id=123; abc=456 注意分号后面有一个空格。
#设置过期时间
设置过期时间:下面设置了 id=123 这个值过期时间是两秒。如果我们第一次请求完之后,等待两秒后再次发送请求,会发现请求中携带的 cookie 中只有 abc=456; id=123 过期了。
const http = require('http')
const fs = require('fs')
http.createServer(function(req, res) {
if (req.url === '/') {
const html = fs.readFileSync('test.html', 'utf-8')
res.writeHead(200, {
'Content-Type': 'text/html',
'Set-Cookie': ['id=123; max-age=2', 'abc=456']
})
res.end(html)
}
}).listen(8888)
#设置 HttpOnly
const http = require('http')
const fs = require('fs')
http.createServer(function(req, res) {
if (req.url === '/') {
const html = fs.readFileSync('test.html', 'utf-8')
res.writeHead(200, {
'Content-Type': 'text/html',
'Set-Cookie': ['id=123; max-age=2', 'abc=456; HttpOnly']
})
res.end(html)
}
}).listen(8888)
在浏览器中使用 document.cookie 是获取不到 abc=456 的值。
#设置 Domain
在同一个主域名下的二级域名可以共享 Cookie,比如主域名 test.com 有两个二级域名 a.test.com 和 b.test.com 两个:
const http = require('http')
const fs = require('fs')
http.createServer(function(req, res) {
const host = req.headers.host
if (req.url === '/') {
const html = fs.readFileSync('test.html', 'utf-8')
if (host === 'test.com') {
res.writeHead(200, {
'Content-Type': 'text/html',
'Set-Cookie': ['id=123; max-age=2', 'abc=456; domain=test.com']
})
}
res.end(html)
}
}).listen(8888)
const http = require('http')
const fs = require('fs')
http.createServer(function(req, res) {
const host = req.headers.host
if (req.url === '/') {
const html = fs.readFileSync('test.html', 'utf-8')
if (host === 'test.com') {
res.writeHead(200, {
'Content-Type': 'text/html',
'Set-Cookie': ['id=123; max-age=2', 'abc=456; domain=test.com']
})
}
res.end(html)
}
}).listen(8888)
猜你喜欢
- 2024-11-22 Http无状态、Cookie、Session、Token三者之间的区别
- 2024-11-22 零基础web前端学习路线,前端入门到精通
- 2024-11-22 太多人把Cookie当缓存用,我无语了
- 2024-11-22 JavaScript知识点——详细的Cookie总结
- 2024-11-22 node 中间层如何转存 cookie
- 2024-11-22 好程序员Web前端教程分享Cookie和Session有什么不同
- 2024-11-22 cookie、session、token区别
- 2024-11-22 Web前端cookie,sessionStorage的区别
- 2024-11-22 连环画解析“单点登录”原理,保证你能看懂
- 2024-11-22 前端中cookie和localStorage的区别
你 发表评论:
欢迎- 07-10Oracle 与 Google Cloud 携手大幅扩展多云服务
- 07-10分享收藏的 oracle 11.2.0.4各平台的下载地址
- 07-10Oracle 和 Microsoft 推出 Oracle Exadata 数据库服务
- 07-10Oracle Database@Azure 推进到南美等新区域并增加了新服务
- 07-10Oracle宣布推出 Oracle Database@AWS 的有限预览版
- 07-10Oracle与Nextcloud合作,推出主权云上的安全协作平台
- 07-10NodeRED魔改版连接MsSql、PostgreSQL、MySQL、OracleDB存储无忧
- 07-10对于企业数据云备份,“多备份”承诺的是成本更低,管理更高效#36氪开放日深圳站#
- 602℃几个Oracle空值处理函数 oracle处理null值的函数
- 594℃Oracle分析函数之Lag和Lead()使用
- 582℃0497-如何将Kerberos的CDH6.1从Oracle JDK 1.8迁移至OpenJDK 1.8
- 579℃Oracle数据库的单、多行函数 oracle执行多个sql语句
- 574℃Oracle 12c PDB迁移(一) oracle迁移到oceanbase
- 567℃【数据统计分析】详解Oracle分组函数之CUBE
- 554℃最佳实践 | 提效 47 倍,制造业生产 Oracle 迁移替换
- 548℃Oracle有哪些常见的函数? oracle中常用的函数
- 最近发表
-
- Oracle 与 Google Cloud 携手大幅扩展多云服务
- 分享收藏的 oracle 11.2.0.4各平台的下载地址
- Oracle 和 Microsoft 推出 Oracle Exadata 数据库服务
- Oracle Database@Azure 推进到南美等新区域并增加了新服务
- Oracle宣布推出 Oracle Database@AWS 的有限预览版
- Oracle与Nextcloud合作,推出主权云上的安全协作平台
- NodeRED魔改版连接MsSql、PostgreSQL、MySQL、OracleDB存储无忧
- 对于企业数据云备份,“多备份”承诺的是成本更低,管理更高效#36氪开放日深圳站#
- 解读丨《归档文件整理规则》— 电子文件元数据存储
- Data Guard跳归档恢复的实践(dataguard failover)
- 标签列表
-
- 前端设计模式 (75)
- 前端性能优化 (51)
- 前端模板 (66)
- 前端跨域 (52)
- 前端缓存 (63)
- 前端aes加密 (58)
- 前端脚手架 (56)
- 前端md5加密 (54)
- 前端路由 (61)
- 前端数组 (73)
- 前端js面试题 (50)
- 前端定时器 (59)
- 前端获取当前时间 (50)
- Oracle RAC (76)
- oracle恢复 (77)
- oracle 删除表 (52)
- oracle 用户名 (80)
- oracle 工具 (55)
- oracle 内存 (55)
- oracle 导出表 (62)
- oracle约束 (54)
- oracle 中文 (51)
- oracle链接 (54)
- oracle的函数 (57)
- 前端调试 (52)
本文暂时没有评论,来添加一个吧(●'◡'●)