资讯专栏INFORMATION COLUMN

token与sessionId的区别——学习笔记

liaosilzu2007 / 2666人阅读

摘要:学开发半年多,之前一直有个疑问为什么要用,好好的用不好吗其实就是新技术与老技术,但是还是想弄懂这个问题之前一直疑惑,今天搞懂了,整合了一下学习过程,先对比一下与一简述的生成方式与的生成方式的生成方式浏览器第一次访问服务器时,服务器创建一个,

学开发半年多,之前一直有个疑问:
为什么要用token,好好的用sessionID不好吗
(其实就是新技术老技术,但是还是想弄懂)
这个问题之前一直疑惑,今天搞懂了,整合了一下学习过程,先对比一下sessionID与token

一、简述sessionID的生成方式与token的生成方式 1.sessionID的生成方式

浏览器第一次访问服务器时,服务器创建一个session,同时生成一个唯一的会话key,即sessionID。接着sessionID及session分别作为key和value保存到缓存中,也可以保存到数据库中,然后服务器把sessionID以cookie的形式发送给浏览器,浏览器下次访问服务器时直接携带上cookie中的sessionID,服务器再根据sessionID找到对应的session进行匹配
这里我们注意到两点:1.sessionID会自动由浏览器带上 2.session是需要存储空间的

2.token的生成方式

浏览器第一次访问服务器时,服务器根据传过来的唯一标识userId,通过一些算法,加一个密钥,生成一个token,接着通过base64编码将token返回给客户端。客户端将token保存起来,下次请求时需要带着token,服务器收到请求后,用相同的算法和密钥去验证token
这里我们注意到两点:1.token需要代码才能带上 2.token可以不需要存储空间(JWT)(当然也有存入缓存的处理,特别是要进行revoke操作时),通过算法和密钥验证

3.区别

以上我们提出来的注意点就可以看出两点区别了:1.浏览器方面,是否直接带上 2.服务器方面,是否需要存储空间

二.回答之前的疑问 1.安卓,ios,微信小程序,微信内置浏览器上

安卓与ios用原生接口发请求时,每一次创建一个会话(C/S模式)
https://blog.csdn.net/qq_4219...
https://www.cnblogs.com/liuji...

微信小程序通过微信官方提供的登录能力获取微信提供的用户身份标识,快速建立小程序内的用户体系
https://developers.weixin.qq....

微信内置浏览器会对所有请求做代理,导致出去的ip不固定
https://blog.csdn.net/wangjun...

2.安全问题上,防止CSRF攻击

(原来浏览器加载image标签中的地址也会发送sessionID的)
这里对技术机制没有透彻的了解,不敢随意翻译而误导,大家看一下以下的段落吧
摘自https://guides.rubyonrails.or...
Cross-Site Request Forgery (CSRF)
This attack method works by including malicious code or a link in a page that accesses a web application that the user is believed to have authenticated. If the session for that web application has not timed out, an attacker may execute unauthorized commands.
In the session chapter you have learned that most Rails applications use cookie-based sessions. Either they store the session ID in the cookie and have a server-side session hash, or the entire session hash is on the client-side. In either case the browser will automatically send along the cookie on every request to a domain, if it can find a cookie for that domain. The controversial point is that if the request comes from a site of a different domain, it will also send the cookie. Let"s start with an example:

Bob browses a message board and views a post from a hacker where there is a crafted HTML image element. The element references a command in Bob"s project management application, rather than an image file: By viewing the post, the browser finds an image tag. It tries to load the suspected image from www.webapp.com. As explained before, it will also send along the cookie with the valid session ID.
The web application at www.webapp.com verifies the user information in the corresponding session hash and destroys the project with the ID 1. It then returns a result page which is an unexpected result for the browser, so it will not display the image.
Bob doesn"t notice the attack - but a few days later he finds out that project number one is gone.

It is important to notice that the actual crafted image or link doesn"t necessarily have to be situated in the web application"s domain, it can be anywhere - in a forum, blog post or email.
CSRF appears very rarely in CVE (Common Vulnerabilities and Exposures) - less than 0.1% in 2006 - but it really is a "sleeping giant" [Grossman]. This is in stark contrast to the results in many security contract works - CSRF is an important security issue.

三.总结

广义上来说一切维护用户状态的技术都是session,然后sessionid与token就是老技术与技术
可是就是想知道为什么
以上内容如有不对请指出

参考资料:

Session,Token相关区别:https://www.cnblogs.com/xiaoz...
基于 Token 的身份验证:JSON Web Token(附:Node.js 项目):https://ninghao.net/blog/2834
基于 session 和基于 token 的用户认证方式到底该如何选择?:https://www.v2ex.com/t/276207
我想知道token和sessionid的区别是什么:https://www.v2ex.com/t/80003
为什么 APP 要用 token 而不用 session 认证?:https://www.v2ex.com/t/148426
移动端开发为什么不采用session而是用token:https://www.zhihu.com/questio...

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/62013.html

相关文章

  • JSON Web Token学习笔记

    摘要:一什么是为什么使用是机制的一种替代方案。这种情况下使用就会有更加方便。的数据结构一般为三部分组成头部一般包含签名的算法和令牌的属性负载实际需要传递的数据一般不加密,因此不要把重要信息放在里面签名部分是对前两部分的签名,防止数据篡改。 一、什么是JSON Web Token? 1、为什么使用JSON Web Token JSON Web Token是cookie session机制的一种...

    cangck_X 评论0 收藏0
  • 前端应该知道web登录

    摘要:客户端发起非登录请求时,服务端通过中的找到对应的来知道此次请求是谁发出的。数量随着登录用户的增多而增多,存储会增加很多。还记得在上家公司做全干工程师的时候,基本从页面写到运维,当时做登录这块的时候,被session、cookie、token各种概念差点整蒙圈了,上网查询相关概念,发现很多人都是类似的疑惑,比如:showImg(https://user-gold-cdn.xitu.io/201...

    NervosNetwork 评论0 收藏0
  • 大话javascript 7期:Cookie、Session和Token那些事儿

    摘要:服务器检查该,以此来辨认用户状态。如果为,表示删除该。防篡改签名服务器为每个项生成签名。服务端根据接收到的内容和签名,校验内容是否被篡改。算法得到的签名和请求中数据的签名不一致,则证明数据被篡改。 一、登录认证机制 随着互联网的不断发展,无论是网站还是app,一般都会要求用户注册/登录。主要的登录方式有账户密码登录、第三方登录(微信登录、QQ登录、微博登录等) 登录可分为三个阶段(登录...

    tianren124 评论0 收藏0
  • Laravel学习笔记之Session源码解析(上)

    摘要:然后中间件使用方法来启动获取实例,使用类来管理主要分为两步获取实例,主要步骤是通过该实例从存储介质中读取该次请求所需要的数据,主要步骤是。 说明:本文主要通过学习Laravel的session源码学习Laravel是如何设计session的,将自己的学习心得分享出来,希望对别人有所帮助。Laravel在web middleware中定义了session中间件IlluminateSess...

    NervosNetwork 评论0 收藏0

发表评论

0条评论

liaosilzu2007

|高级讲师

TA的文章

阅读更多
最新活动
阅读需要支付1元查看
<