令牌认证和使用cookie的认证有什么区别?
我正在尝试实现Ember Auth Rails演示,但我不理解使用Ember Auth FAQ中关于“为什么token身份验证?”的问题所描述的令牌身份验证背后的原因。
令牌认证和使用cookie的认证有什么区别?
我正在尝试实现Ember Auth Rails演示,但我不理解使用Ember Auth FAQ中关于“为什么token身份验证?”的问题所描述的令牌身份验证背后的原因。
当前回答
A typical web app is mostly stateless, because of its request/response nature. The HTTP protocol is the best example of a stateless protocol. But since most web apps need state, in order to hold the state between server and client, cookies are used such that the server can send a cookie in every response back to the client. This means the next request made from the client will include this cookie and will thus be recognized by the server. This way the server can maintain a session with the stateless client, knowing mostly everything about the app's state, but stored in the server. In this scenario at no moment does the client hold state, which is not how Ember.js works.
在Ember.js中情况有所不同。Ember.js使程序员的工作变得更容易,因为它确实在客户端为您保存了状态,可以随时了解其状态,而不必向服务器请求状态数据。
然而,在客户端保存状态有时也会引入在无状态情况下不存在的并发问题。然而,Ember.js也为您处理这些问题;具体地说,ember-data是基于这一点构建的。总之,Ember.js是为有状态客户端设计的框架。
Ember.js不像典型的无状态web应用程序那样,会话、状态和相应的cookie几乎完全由服务器处理。Ember.js将其状态完全保存在Javascript中(在客户端的内存中,而不是像其他框架那样在DOM中),并且不需要服务器来管理会话。这导致Ember.js在许多情况下更加通用,例如当你的应用程序处于离线模式时。
显然,出于安全原因,每次发出请求时都需要向服务器发送某种令牌或唯一密钥,以便进行身份验证。通过这种方式,服务器可以查找发送令牌(最初由服务器发出),并在将响应发送回客户端之前验证它是否有效。
在我看来,使用认证令牌而不是在Ember Auth FAQ中所述的cookie的主要原因是Ember.js框架的性质,也因为它更适合有状态的web应用程序范例。因此,cookie机制并不是构建Ember.js应用程序的最佳方法。
我希望我的回答能让你的问题更有意义。
其他回答
A typical web app is mostly stateless, because of its request/response nature. The HTTP protocol is the best example of a stateless protocol. But since most web apps need state, in order to hold the state between server and client, cookies are used such that the server can send a cookie in every response back to the client. This means the next request made from the client will include this cookie and will thus be recognized by the server. This way the server can maintain a session with the stateless client, knowing mostly everything about the app's state, but stored in the server. In this scenario at no moment does the client hold state, which is not how Ember.js works.
在Ember.js中情况有所不同。Ember.js使程序员的工作变得更容易,因为它确实在客户端为您保存了状态,可以随时了解其状态,而不必向服务器请求状态数据。
然而,在客户端保存状态有时也会引入在无状态情况下不存在的并发问题。然而,Ember.js也为您处理这些问题;具体地说,ember-data是基于这一点构建的。总之,Ember.js是为有状态客户端设计的框架。
Ember.js不像典型的无状态web应用程序那样,会话、状态和相应的cookie几乎完全由服务器处理。Ember.js将其状态完全保存在Javascript中(在客户端的内存中,而不是像其他框架那样在DOM中),并且不需要服务器来管理会话。这导致Ember.js在许多情况下更加通用,例如当你的应用程序处于离线模式时。
显然,出于安全原因,每次发出请求时都需要向服务器发送某种令牌或唯一密钥,以便进行身份验证。通过这种方式,服务器可以查找发送令牌(最初由服务器发出),并在将响应发送回客户端之前验证它是否有效。
在我看来,使用认证令牌而不是在Ember Auth FAQ中所述的cookie的主要原因是Ember.js框架的性质,也因为它更适合有状态的web应用程序范例。因此,cookie机制并不是构建Ember.js应用程序的最佳方法。
我希望我的回答能让你的问题更有意义。
我认为这里有些混乱。基于cookie的身份验证与HTML5 Web Storage之间的显著区别在于,浏览器被构建为每当从设置它们的域请求资源时都发送cookie数据。如果不关掉cookie,你无法阻止这种情况。除非页面中的代码发送数据,否则浏览器不会从Web存储发送数据。页面只能访问自己存储的数据,而不能访问其他页面存储的数据。
因此,如果用户担心自己的cookie数据可能被谷歌或Facebook使用,可能会关闭cookie。但是,他们没有理由关闭网络存储(直到广告商也找到一种方法来使用它)。
所以,这就是基于cookie和基于令牌的区别,后者使用Web存储。
Tokens need to be stored somewhere (local/session storage or cookies) Tokens can expire like cookies, but you have more control Local/session storage won't work across domains, use a marker cookie Preflight requests will be sent on each CORS request When you need to stream something, use the token to get a signed request It's easier to deal with XSS than XSRF The token gets sent on every request, watch out its size If you store confidential info, encrypt the token JSON Web Tokens can be used in OAuth Tokens are not silver bullets, think about your authorization use cases carefully
http://blog.auth0.com/2014/01/27/ten-things-you-should-know-about-tokens-and-cookies/
http://blog.auth0.com/2014/01/07/angularjs-authentication-with-cookies-vs-token/
简而言之:
JWT vs Cookie Auth
| | Cookie | JWT |
| Stateless | No | Yes |
| Cross domain usage | No | Yes |
| Mobile ready | No | Yes |
| Performance | Low | High (no need in request to DB) |
| Add to request | Automatically | Manually (if not in cookie) |
一个主要的区别是cookie服从同源策略,而令牌则不是。这就产生了各种各样的下游效应。
由于cookie只发送给特定的主机,该主机必须承担验证用户身份的责任,用户必须在该主机上创建一个具有安全数据的帐户,以便进行验证。
Tokens on the other hand are issued and are not subject to same origin policy. The issuer can be literally anybody and it is up to the host to decide which issuers to trust. An issuer like Google and Facebook is typically well trusted so a host can shift the burden of authenticating the user (including storing all user security data) to another party and the user can consolidate their personal data under a specific issuer and not have to remember a bunch of different passwords for each host they interact with.
这允许单点登录场景,从而减少用户体验中的整体摩擦。从理论上讲,网络也变得更加安全,因为专门的身份提供者出现了,提供认证服务,而不是每个ma和pa网站都有自己的,可能不成熟的认证系统。随着这些提供商的出现,为非常基本的资源提供安全网络资源的成本也趋向于零。
因此,总的来说,令牌减少了与提供身份验证相关的摩擦和成本,并将安全web各个方面的负担转移到能够更好地实现和维护安全系统的中心化方。