JSON Web Token (JWT) is an open standard (RFC 7519) that defines a compact and self-contained way for securely transmitting information between parties as a JSON object. This information can be verified and trusted because it is digitally signed. JWTs can be signed using a secret (with the HMAC algorithm) or a public/private key pair using RSA or ECDSA.
JSON Web令牌(JWT)是一个开放标准(RFC 7519),它定义了一种紧凑且独立的方法,用于在各方之间安全地将信息作为JSON对象传输。 由于此信息是经过数字签名的,因此可以被验证和信任。 可以使用秘密(使用HMAC算法)或使用RSA或ECDSA的公用/专用密钥对对JWT进行签名。
The header typically consists of two parts: the type of the token, which is JWT,and the signing algorithm being used, such as HMAC SHA256 or RSA.
头信息包含两部分:
(1) token,也就是 JWT,是Base64编码的Url(2) 加密算法(如 HMAC SHA256 or RSA)
举例:
{"alg":"HS256","typ":"JWT"}
1.2.2 payload
The second part of the token is the payload, which contains theclaims. Claims are statements about an entity(typically, the user)and additional data. There are three types of claims: registered,public,andprivate claims.
这部分主要是声明,内容如下:
标准的实体信息,如用户
附加信息
声明分为三种:
注册式声明
公开声明
私有声明
示例:
{"sub":"1234567890","name":"John Doe","admin":true}
Base64 编码
注意:
请注意,对于已签名的令牌,此信息尽管可以防止篡改,但任何人都可以读取。除非将其加密,否则请勿将机密信息放入JWT的有效负载或报头元素中。输出是三个由点分隔的Base64-URL字符串,可以在HTML和HTTP环境中轻松传递这些字符串,与基于XML的标准(例如SAML)相比,它更紧凑。
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.
这些是一组非强制性的但建议使用的预定义要求,以提供一组有用的,可互操作的要求。 其中一些是:iss(发行者),exp(到期时间),sub(主题),aud(受众)等。
Public claims
These can be defined at will by those using JWTs.But to avoid collisions they should be defined in the IANAJSONWebTokenRegistry or be defined as a URI that contains a collision resistant namespace.
JWT使用者可以随意定义声明。 但是为避免冲突,应在IANAJSON Web令牌注册表中定义它们,或将其定义为包含抗冲突名称空间的URI。
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.
这些是自定义声明,旨在使用者之间共享信息,既不是注册声明也不是公共声明。
1.2.3 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.
要创建签名部分,您必须获取编码的标头,编码的有效载荷,机密,标头中指定
的算法,并对其进行签名。
如,如果要使用HMACSHA256算法,则将通过以下方式创建签名:
HMACSHA256(base64UrlEncode(header)+"."+base64UrlEncode(payload),
secret)
签名用于验证消息在此过程中没有更改,并且对于使用私钥进行签名的令牌,它还可以验证JWT的发送者是它所说的真实身份。