令牌认证和使用cookie的认证有什么区别?
我正在尝试实现Ember Auth Rails演示,但我不理解使用Ember Auth FAQ中关于“为什么token身份验证?”的问题所描述的令牌身份验证背后的原因。
令牌认证和使用cookie的认证有什么区别?
我正在尝试实现Ember Auth Rails演示,但我不理解使用Ember Auth FAQ中关于“为什么token身份验证?”的问题所描述的令牌身份验证背后的原因。
当前回答
基于令牌的认证是无状态的,服务器不需要在会话中存储用户信息。这样就可以扩展应用程序,而不用担心用户已经登录到哪里。有web服务器框架的亲缘性基于cookie,而这不是一个问题,基于令牌。因此,可以使用相同的令牌从我们登录的域以外的域获取安全资源,从而避免了另一个uid/pwd身份验证。
非常好的文章:
http://www.toptal.com/web/cookie-free-authentication-with-json-web-tokens-an-example-in-laravel-and-angularjs
其他回答
基于令牌的认证是无状态的,服务器不需要在会话中存储用户信息。这样就可以扩展应用程序,而不用担心用户已经登录到哪里。有web服务器框架的亲缘性基于cookie,而这不是一个问题,基于令牌。因此,可以使用相同的令牌从我们登录的域以外的域获取安全资源,从而避免了另一个uid/pwd身份验证。
非常好的文章:
http://www.toptal.com/web/cookie-free-authentication-with-json-web-tokens-an-example-in-laravel-and-angularjs
使用Token时…
需要联邦。例如,您希望使用一个提供者(令牌分发器)作为令牌颁发者,然后使用api服务器作为令牌验证器。应用程序可以向令牌分发器进行身份验证,接收令牌,然后将该令牌提交给api服务器进行验证。(同样适用于谷歌登录。或贝宝。或Salesforce.com。等)
Asynchrony is required. For example, you want the client to send in a request, and then store that request somewhere, to be acted on by a separate system "later". That separate system will not have a synchronous connection to the client, and it may not have a direct connection to a central token dispensary. a JWT can be read by the asynchronous processing system to determine whether the work item can and should be fulfilled at that later time. This is, in a way, related to the Federation idea above. Be careful here, though: JWT expire. If the queue holding the work item does not get processed within the lifetime of the JWT, then the claims should no longer be trusted.
客户需要签署请求。在这里,请求由客户端使用他的私钥签署,服务器将使用客户端已经注册的公钥进行验证。
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就像一张会员卡,检查它的前台必须打电话或查询数据库来检查你是否是会员。而认证令牌就像一张有签名和到期日期的支票。然后,安全性来自于基于难以伪造的假设的签名,而无需直接询问发行者。
在这两种情况下,它们都处理授权,而不是身份验证。任何持有会员卡或支票的人都可以获得访问权,它们不能证明你是谁,只是证明你有权使用你要求的资源。正因为如此,它们必须小心防范盗窃,这对于更难撤销的认证令牌来说尤其如此。