JWT简述

290 阅读4分钟

翻译自JSON Web tokens简介

什么是JSON Web Token?

JSON Web Token(JWT)是一种开放式标准(RFC 7519),它定义了一种紧凑自包含的方式,用于在各方之间以JSON对象安全传输信息。这些信息可以通过数字签名进行验证和信任。可以使用秘密(使用HMAC算法)或使用RSA的公钥/私钥对对JWT进行签名。

我们来进一步解释一下这个定义的一些概念。

  • 紧凑:由于它们尺寸较小,JWT可以通过URL,POST参数或HTTP标头内发送。另外,尺寸越小意味着传输速度越快。

  • 自包含:有效负载包含有关用户的所有必需信息,避免了多次查询数据库的需要。

什么时候应该使用JSON Web令牌?

以下是JSON Web Tokens有用的一些场景:

  • 身份验证:这是使用JWT最常见的情况。一旦用户登录,每个后续请求都将包含JWT,允许用户访问该令牌允许的路由,服务和资源。单点登录是当今广泛使用JWT的一项功能,因为它的开销很小,并且能够轻松地跨不同域使用。

  • 信息交换:JSON Web令牌是在各方之间安全传输信息的好方法。因为JWT可以签名 - 例如使用公钥/私钥对,所以可以确定发件人是他们自称的人。此外,由于使用标头和有效载荷计算签名,因此您还可以验证内容是否未被篡改。

什么是JSON Web令牌结构?

JSON Web令牌包含三个由点(.)分隔的部分,它们是:

  • 头 Head
  • 有效载荷 Payload
  • 签名 Signature

因此,JWT通常看起来如下所示。 xxxxx.yyyyy.zzzzz

头 Head

头通常由两部分组成:令牌的类型,即JWT和正在使用的散列算法,如HMAC SHA256或RSA。

例如:

{
  "alg": "HS256",
  "typ": "JWT"
}

然后,这个JSON被Base64Url编码,形成JWT的第一部分。

有效载荷 Payload

The second part of the token is the payload, which contains the claims.Claims are statements about an entity (typically, the user) and additional metadata. There are three types of claims: registered , public , and private claims.

  • Registered claims : These are a set of predefined claims which are not mandatory but recommended, to provide a set of useful, interoperable claims.Some of them are: iss (issuer), exp (expiration time), sub (subject), aud (audience), and others .
  • Public claims: These can be defined at will by those using JWTs. But to avoid collisions they should be defined in the IANA JSON Web Token Registry or be defined as a URI that contains a collision resistant namespace.
  • Private claims: These are the custom claims created to share information between parties that agree on using them and are neither registered or public claims.

一个有效载荷可以是:

{
  "sub": "1234567890",
  "name": "John Doe",
  "admin": true
}

然后将有效载荷Base64Url编码以形成JSON Web令牌的第二部分。

签名 Signature

To create the signature part you have to take the encoded header, the encoded payload, a secret, the algorithm specified in the header, and sign that.

For example if you want to use the HMAC SHA256 algorithm, the signature will be created in the following way:

HMACSHA256(
  base64UrlEncode(header) + "." +
  base64UrlEncode(payload),
  secret)

The signature is used to verify that the sender of the JWT is who it says it is and to ensure that the message wasn't changed along the way.

整合

输出是三个由点分隔的Base64字符串,可以在HTML和HTTP环境中轻松传递,而与基于XML的标准(如SAML)相比,它更加紧凑。

以下显示了一个JWT,它具有先前的标头和有效负载编码,并且使用秘密进行签名。

编码JWT

如果您想要使用JWT并将这些概念付诸实践,则可以使用jwt.io调试器来解码,验证和生成JWT。

JWT.IO调试器

JSON Web令牌如何工作?

在身份验证中,当用户使用他们的凭证成功登录时,JSON Web Token将被返回并且必须保存在本地(通常在local storage中,但也可以使用Cookie),而不是在传统方法中创建Session。服务器并返回一个cookie。 无论何时用户想要访问受保护的路由或资源,用户代理都应该使用承载模式(Bearer Schema)来发送JWT,通常在Authorization标头中。Head的内容应该如下所示:

Authorization: Bearer <token>

这是一种无状态身份验证机制,因为用户状态永远不会保存在服务器内存中。服务器的受保护路由将在授权标头中检查有效的JWT,如果存在,将允许用户访问受保护的资源。由于JWT是独立的,所有必要的信息都在那里,减少了多次查询数据库的需求。

这使您可以完全依赖无状态的数据API,甚至向下游服务提出请求。无论哪些域正在为您的API提供服务并不重要,因此跨源资源共享(CORS)不会成为问题,因为它不使用Cookie。

下图显示了这个过程:

enter image description here