signature 部分是对前两部分的签名,防止数据篡改。
起首需要指定一个密钥(secret)。这个密钥只有服务器才知道,不能走漏给用户。然后利用Header 里面指定的签名算法(默认是 HMAC SHA256)。
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.
要创建签名部分,您必须获取编码的标头、编码的有效载荷、秘密、标头中指定的算法,并对其举行签名。
例如,如果您想利用 HMAC SHA256算法,签名将按以下方式创建:
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret)
复制代码
该签名用于验证消息在实行过程中没有被更改,并且,对于利用私钥签名的令牌,它还可以验证 JWT 的发送方是否就是它所说的那个人。
Putting all together
输出是三个由点分隔的 Base64-URL 字符串,这些字符串可以在 HTML 和 HTTP 环境中轻松传递,同时与基于 XML 的标准(如 SAML)相比更加紧凑。
The following diagram shows how a JWT is obtained and used to access APIs or resources:
The application or client requests authorization to the authorization server. This is performed through one of the different authorization flows. For example, a typical OpenID Connect compliant web application will go through the /oauth/authorize endpoint using the authorization code flow.
When the authorization is granted, the authorization server returns an access token to the application.
The application uses the access token to access a protected resource (like an API).