Web浏览器的Cookie密钥的最大大小是多少?

本文翻译自:What is the maximum size of a web browser's cookie's key?

What is the maximum size of a web browser's cookie's key? Web浏览器的Cookie密钥的最大大小是多少?

I know the maximum size of a cookie is 4KB, but does the key have a limitation as well? 我知道Cookie的最大大小为4KB,但是密钥也有限制吗?


#1楼

参考:https://stackoom.com/question/2GJI/Web浏览器的Cookie密钥的最大大小是多少


#2楼

You can also use web storage too if the app specs allows you that (it has support for IE8+). 如果应用规范允许,您也可以使用Web存储(它支持IE8 +)。

It has 5M (most browsers) or 10M (IE) of memory at its disposal. 它拥有5M (大多数浏览器)或10M (IE) 可用的内存。

" Web Storage (Second Edition) " is the API and " HTML5 Local Storage " is a quick start. Web存储(第二版) ”是API,“ HTML5本地存储 ”是快速入门。


#3楼

After testing a few browsers myself, and using Browser Shots I have compiled the following list 在自己测试了几个浏览器并使用Browser Shots之后,我编译了以下列表

在此处输入图片说明


#4楼

The 4K limit you read about is for the entire cookie, including name, value, expiry date etc. If you want to support most browsers, I suggest keeping the name under 4000 bytes, and the overall cookie size under 4093 bytes. 您所了解的4K限制适用于整个Cookie,包括名称,值,有效期等。如果要支持大多数浏览器,建议将名称保留在4000字节以下,并将Cookie的整体大小保留在4093字节以下。

One thing to be careful of: if the name is too big you cannot delete the cookie (at least in JavaScript). 要注意的一件事:如果名称太大,则无法删除cookie(至少在JavaScript中)。 A cookie is deleted by updating it and setting it to expire. 通过更新cookie并将其设置为过期来删除cookie。 If the name is too big, say 4090 bytes, I found that I could not set an expiry date. 如果名称太大(例如4090字节),我发现无法设置有效期限。 I only looked into this out of interest, not that I plan to have a name that big. 我只是出于兴趣而调查了此内容,而不是打算计划这么大的名字。

To read more about it, here are the " Browser Cookie Limits " for common browsers. 要了解更多信息,这里是普通浏览器的“ 浏览器Cookie限制 ”。


While on the subject, if you want to support most browsers, then do not exceed 50 cookies per domain , and 4093 bytes per domain . 在此主题上,如果您想支持大多数浏览器,则每个域的Cookie不能超过50 个,每个域不能超过4093个字节 That is, the size of all cookies should not exceed 4093 bytes. 也就是说,所有cookie的大小不应超过4093字节。

This means you can have 1 cookie of 4093 bytes, or 2 cookies of 2045 bytes, etc. 这意味着您可以拥有1个4093字节的cookie,或2个2045字节的cookie,等等。


I used to say 4095 bytes due to IE7, however now Mobile Safari comes in with 4096 bytes with a 3 byte overhead per cookie, so 4093 bytes max. 由于IE7,我曾经说4095个字节,但是现在Mobile Safari附带4096个字节,每个cookie的开销为3个字节,因此最大为4093个字节。


#5楼

A cookie key(used to identify a session) and a cookie are the same thing being used in different ways. cookie键(用于标识会话)和cookie是同一事物,只是使用方式不同。 So the limit would be the same. 因此限制将是相同的。 According to Microsoft its 4096 bytes. 根据Microsoft的4096字节。

MSDN MSDN

cookies are usually limited to 4096 bytes and you can't store more than 20 cookies per site. Cookie通常限制为4096个字节,并且每个站点最多只能存储20个Cookie。 By using a single cookie with subkeys, you use fewer of those 20 cookies that your site is allotted. 通过将单个Cookie与子键一起使用,您可以在分配给网站的那20个Cookie中使用较少的cookie。 In addition, a single cookie takes up about 50 characters for overhead (expiration information, and so on), plus the length of the value that you store in it, all of which counts toward the 4096-byte limit. 此外,单个cookie会占用约50个字符的开销(到期信息等),以及您存储在其中的值的长度,所有这些都计入4096字节的限制。 If you store five subkeys instead of five separate cookies, you save the overhead of the separate cookies and can save around 200 bytes. 如果存储五个子项而不是五个单独的cookie,则可以节省单独的cookie的开销,并且可以节省大约200个字节。


#6楼

Actually, RFC 2965, the document that defines how cookies work, specifies that there should be no maximum length of a cookie's key or value size , and encourages implementations to support arbitrarily large cookies. 实际上,RFC 2965是定义cookie的工作方式的文档,它指定cookie的键或值的大小不应超过最大长度 ,并鼓励实现支持任意大的 cookie。 Each browser's implementation maximum will necessarily be different, so consult individual browser documentation. 每个浏览器的实现最大值一定会有所不同,因此请查阅各个浏览器文档。

See section 5.3, "Implementation Limits", in the RFC . 请参阅RFC第5.3节“实施限制”。

在Flask应用使用JWT(JSON Web Tokens)保护所有视图要以下几个步骤: 1. **安装依赖**: 首先,你要安装`PyJWT`库,它用于生成和验证JWT。你可以通过pip安装: ``` pip install pyjwt flask-jwt_extended ``` 2. **配置JWT**: 在Flask应用初始化时,配置`flask_jwt_extended`。设置SECRET_KEY作为JWT的签名密钥,并启用token过期机制,比如30秒: ```python from flask_jwt_extended import JWTManager app = Flask(__name__) jwt = JWTManager(app) app.config['JWT_SECRET_KEY'] = 'your_secret_key' # 替换为你的密钥 app.config['JWT_ACCESS_TOKEN_EXPIRES'] = timedelta(seconds=30) # 30秒过期时间 ``` 3. **身份验证间件**: 在每个要鉴权的视图前添加JWT身份验证间件。例如: ```python @app.route('/protected', methods=['GET']) @jwt_required() # 这会检查是否有有效的JWT token def protected_view(): # ... ``` 4. **登出处理**: 用户登出时,可以注销他们的JWT并将其从服务器端清除。这通常涉及到从数据库或缓存删除相关的令牌信息: ```python def logout_user(response): jti = get_jwt_identity() # 获取当前JWT的jti(JWT ID) # 删除该用户的令牌信息 remove_token_from_db(jti) # 根据你的存储方式删除 return response @app.route('/logout') def logout(): resp = make_response(logout_user('', 200)) # 返回一个空响应并强制刷新浏览器 resp.delete_cookie('access_token') # 清除cookie的JWT return resp ``` 5. **清除过期的令牌**: 可以定期清理过期的令牌,但这是一个额外的优化步骤,而不是必的。如果你选择这样做,可以在Flask-JWT-Extended的回调实现: ```python @jwt.token_in_blacklist_loader def check_if_revoked(decrypted_token): # 检查是否已被撤销(例如,在Token Revocation List) # 如果被撤销,返回True return decrypted_token['revoked'] @jwt.revoked_token_loader def revoked_token_callback(): return jsonify({"message": "The token has been revoked."}), 401 ``` 每次用户请求资源时,都会检查他们是否有有效的JWT。当令牌过期或者用户主动登出时,服务器将不再接受请求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值