Cookie, Token with the Session presentation (non-original)

Article Outline

A, Cookie introduce
two, Token introduce
three, Session introduce
four, Token, Cookie and Session compare
V. Reference article

 

A, Cookie introduction

What is 1. Cookie

cookie mechanism is the use of the program (the cookie to efforts to solve the HTTP protocol is stateless defects made) in the client on hold. cookie use is automatically sent to the server by the browser in accordance with certain principles in the background. Browser to check all stored cookie, a cookie if the scope is not less than the stated location of the resources will be requested, put the cookie attached to the request to send the head of the resource HTTP request to the server.
the cookie should include: name, value, expiration time, path and domain.
Together form the cookie path scope domain.
If not set an expiration time, then the lifetime of the cookie for the duration of the browser session, close the browser window, cookie disappears. This period of life of the browser session cookie is called a session cookie. Session cookie is generally not stored on the hard but kept in memory, of course, such behavior is not the norm prescribed.
If you set an expiration time, the browser cookie stored on the hard drive, open the browser again after closing, these cookie remain valid until the expiration time exceeds the set. Cookie stored on the hard disk can be shared between different browser process, such as two IE windows. For stored in memory cookie, different browsers have different approach.

2. cookie shortcomings

(1) CSRF (cross-site request forgery) attacks, Ye Hao solve this problem many frameworks are shielded
(2) Some clients do not support cookie, need to manually set, such as small program
(3) there are restrictions on the browser cookie You can not manually set the cookie, for mixed nested development problems, such as small program jumps H5 pages and can not carry cookie
(4) browser data stored on a single cookie can not exceed 4K, many browsers are limited to a maximum of preservation site 20 cookie

Two, Token Introduction

1. Token is what

token means "token", is the user identity authentication, the simplest token consisting of: uid (user's unique identity), time (timestamp of the current time), sign (signed by former token several + salt hashing algorithms and data structures knowledge base ") compressed into fixed length hexadecimal string, a malicious third party can be prevented splicing request token server). the same parameters may also be put token, avoid multiple check times library

2. Token generation process

Request procedure:
1, the client requests to the server, sending a username and password
2, the server generates a token by encrypting user information, including user account information, token expiration time, particularly by the custom server.
3, the server returns a token to the client
4, the client with the cookie stored token, the future requests are put token
. 5, the server decrypts the token, the user confirmation message is correct, as it shows a properly authenticated.
6, the server to verify the results returned to the client

 

Three, Session Introduction

What is 1. Session

session机制是一种服务器端的机制,服务器使用一种类似于散列表的结构(也可能就是使用散列表)来保存信息。
当程序需要为某个客户端的请求创建一个session时,服务器首先检查这个客户端的请求里是否已包含了一个session标识(称为session id),如果已包含则说明以前已经为此客户端创建过session,服务器就按照session id把这个session检索出来使用(检索不到,会新建一个),如果客户端请求不包含session id,则为此客户端创建一个session并且生成一个与此session相关联的session id,session id的值应该是一个既不会重复,又不容易被找到规律以伪造的字符串,这个session id将被 在本次响应中返回给客户端保存。保存这个session id的方式可以采用cookie,这样在交互过程中浏览器 可以自动的按照规则把这个标识发送给服务器。一般这个cookie的名字就是类似于SESSIONID。

2. Session生成过程

请求过程:
1、客户端向服务器请求,发送用户名和密码
2、服务器生成sessionId,绑定用户数据存储在数据库
3、服务器返回sessionId给客户端
4、客户端用cookie存储sessionId,以后的请求都带上这个sessionId
5、服务器如果收到这个sessionId,那么就去数据库查找用户数据,如果找到了说明验证通过
6、服务器把验证结果返回客户端

 

3. Session缺点

(1)负载均衡多服务器的情况,不好确认当前用户是否登录,因为多服务器不共享seesion。这个问题也可以将session存在一个服务器中来解决,但是就不能完全达到负载均衡的效果。
(2)每个客户端只需存储自己的session id,但是服务端却需要存储所有用户session id,对服务器也是一个压力

四、Token、Cookie与Session比较

1. Cookie与Session的区别

(1)cookie数据存放在客户端上,session数据放在服务器上。
(2)cookie不是很安全,别人可以分析存放在本地的COOKIE并进行COOKIE欺骗
考虑到安全应当使用session。
(3)session会在一定时间内保存在服务器上。当访问增多,会比较占用你服务器的性能
考虑到减轻服务器性能方面,应当使用COOKIE。
(4)单个cookie保存的数据不能超过4K,很多浏览器都限制一个站点最多保存20个cookie。
(5)所以个人建议:
将登陆信息等重要信息存放为SESSION
其他信息如果需要保留,可以放在COOKIE中

2. Session与Token的区别

  session 和 oauth token并不矛盾,作为身份认证 token安全性比session好,因为每个请求都有签名还能防止监听以及重放攻击,而session就必须靠链路层来保障通讯安全了。如上所说,如果你需要实现有状态的会话,仍然可以增加session来在服务器端保存一些状态
  App通常用restful api跟server打交道。Rest是stateless的,也就是app不需要像browser那样用cookie来保存session,因此用session token来标示自己就够了,session/state由api server的逻辑处理。 如果你的后端不是stateless的rest api, 那么你可能需要在app里保存session.可以在app里嵌入webkit,用一个隐藏的browser来管理cookie session.
  Session 是一种HTTP存储机制,目的是为无状态的HTTP提供的持久机制。所谓Session 认证只是简单的把User 信息存储到Session 里,因为SID 的不可预测性,暂且认为是安全的。这是一种认证手段。 而Token ,如果指的是OAuth Token 或类似的机制的话,提供的是 认证 和 授权 ,认证是针对用户,授权是针对App 。其目的是让 某App有权利访问 某用户 的信息。这里的 Token是唯一的。不可以转移到其它 App上,也不可以转到其它 用户 上。 转过来说Session 。Session只提供一种简单的认证,即有此 SID,即认为有此 User的全部权利。是需要严格保密的,这个数据应该只保存在站方,不应该共享给其它网站或者第三方App。 所以简单来说,如果你的用户数据可能需要和第三方共享,或者允许第三方调用 API 接口,用 Token 。如果永远只是自己的网站,自己的 App,用什么就无所谓了。
  “只要关闭浏览器 ,session就消失了”?不对。对session来说,除非程序通知服务器删除一个session,否则服务器会一直保留,程序一般都是在用户做log off的时候发个指令去删除session。
  然而浏览器从来不会主动在关闭之前通知服务器它将要关闭,因此服务器根本不会有机会知道浏览器已经关闭,之所以会有这种错觉,是大部分session机制都使用会话cookie来保存session id,而关闭浏览器后这个session id就消失了,再次连接服务器时也就无法找到原来的session。如果服务器设置的cookie被保存在硬盘上,或者使用某种手段改写浏览器发出的HTTP请求头,把原来的session id发送给服务器,则再次打开浏览器仍然能够打开原来的session.
  恰恰是由于关闭浏览器不会导致session被删除,迫使服务器为session设置了一个失效时间,当距离客户端上一次使用session的时间超过这个失效时间时,服务器就可以以为客户端已经停止了活动,才会把session删除以节省存储空间。

五、参考文章

    1. https://junyiseo.com/php/757.html
    2. https://blog.csdn.net/Double2hao/article/details/83744659

Guess you like

Origin www.cnblogs.com/WUXIAOCHANG/p/10949616.html