Excerpt: https://www.cnblogs.com/gdragon/p/11878935.html
Full stack items | small bookcase | server development -JWT Comments
JWT#
The basic content of the article is a translation of the official network, good English, students can click on the link above to directly see the English documentation.
What is the JWT #
JWT
Full name is JSON Web Token(JWT)
a 开放标准(RFC 7519)
, which defines a compact and self-contained manner, as between the parties for JSON
the object information is transmitted securely. Since this information is digitally signed, and therefore can be verified and trust.
You can use the key ( HMAC算法
) or use RSA
or ECDSA
public / private key pair JWT
for signing.
When to use JWT verify #
- Authorization
(Authorization)
This is done usingJWT
the most common situations. Once the user logs on, will include every subsequent requestJWT
, thereby allowing the user to access the token allows routing, services and resources. Single Sign-On is widely used todayJWT
a feature, because it's low overhead and can easily be used in different domains. - Information exchange
(Information Exchange)
JWT
is a good way to transfer information securely between the parties. Because you can toJWT
sign (for example, using public / private key pairs), so you can be sure that the sender is who they say. In addition, because the signature is the useHeader
andpayload
calculation, so you can verify that the content has not been tampered with.
JWT's structure format #
Consists of three parts, these parts by the points .
separated, namely:
Header
Payload
Signature
Thus, JWT
as shown generally as follows.
xxxxx.yyyyy.zzzzz
Header#
Normally it consists of two parts:
E.g:
{
"alg": "HS256",
"typ": "JWT"
}
Then, this JSON
by Base64Url
encoded to form JWT
a first portion.
Payload#
The second part is a token 有效负载
which contains statements. Statement is relevant entities (usually the user) statements and other data. There are three types of claims: registered、public、private claims
Registered claims
These are a predefined set of claims is not mandatory but is recommended to provide a useful set forth in claim interoperable. Some of these are:iss
(sender),exp
(maturity),sub
(theme),aud
(the audience) and so on.
Tip: Please note that the statement name only three characters, asJWT
is compact.Public claims
These can be made usingJWT
the definition of casual staff. However, in order to avoid conflicts, you shouldIANA JSON Web
define them tokens in the registry, or define it as containing anti-collision namespaceURI
.Private claims
These are custom declaration, aimed at agreeing the use of shared information between their parties, neither the public nor the registration statement declared.
Payload Example:
{
"sub": "1234567890",
"name": "John Doe",
"admin": true }
Also it needs Base64Url
coding, to form JWT
the second portion.
Signature#
Signature (Signature)
used to authenticate the message does not change throughout the process, and be signed using the private key token, it validates the JWT
sender's true identity is what it says.
For example, if you want to use the HMAC SHA256
algorithm, the signature will be created in the following ways:
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret)
These three parts merge #
Is output from the three .
separated Base64-URL
string, may be HTML
and HTTP
easily pass these strings environments, based on XML
criteria (e.g. SAML
) compared to its more compact.
The following figure shows a JWT
, it has the previous Header
and Payload
are encoded, and with a Signature
.
On this page you can jwt.io Debugger validation and generationJWT
How JWT work #
In authentication, when a user successfully logs in using their credentials, it will return token. Because tokens are credentials, so care must be taken to prevent security issues. Typically, the effective time of the token should not be set too long.
Tip: Due to lack of security, you should not be sensitive session data is stored in the browser stores.
Whenever the user wants to access a protected resource or route, the user agent should normally be Bearer
sent using Authorization header mode JWT
. Header
Content should be as follows:
Authorization: Bearer <token>
In some cases, the interface does not require access authorization identity. Protection by routing servers will Authorization Header
check JWT令牌
whether a valid and effective if there is, it will allow users to access protected resources.
If JWT
contain the necessary data, it can reduce demand for certain operations query the database.
如果令牌是在Authorization Header
中发送的,则跨域资源共享 (CORS) 不会成为问题,因为它不使用cookie
。
- 应用程序或客户端向授权服务器请求授权。生产
JWT令牌
。 - 授予授权后,授权服务器会将访问令牌返回给应用程序。
- 应用程序使用访问令牌来访问受保护的资源(例如API)。
- 服务器检查
JWT令牌
是否有效,返回对应结果给客户端
ps:请注意,使用签名令牌,令牌或令牌中包含的所有信息都会暴露给用户或其他方,即使他们无法更改它。这意味着您不应将机密信息放入令牌中。
为什么需要 JWT#
对比 Simple Web Tokens (SWT) 和Security Assertion Markup Language Tokens (SAML),看看使用JSON Web Tokens (JWT) 有什么好处。
- 由于
JSON
不如XML
冗长,因此在编码时JSON
的大小也较小,从而使JWT
比SAML
更紧凑。这使得JWT
是在HTML
和HTTP环境
中传递的不错的选择。 - 在安全方面,
SWT
只能使用HMAC算法
进行对称签名。但是JWT
和SAML令牌
可以使用X.509证书形式
的公用/专用密钥对进行签名。与签名JSON
的简单性相比,使用XML Digital Signature
签名XML
而不引入模糊的安全漏洞是非常困难的。 JSON
解析器在大多数编程语言中都很常见,因为它们直接映射到对象。相反,XML
没有自然的文档到对象映射。与SAML
断言相比,这使使用JWT
更加容易。- 关于用法,
JWT
是在Internet
规模上使用的。这强调了在多个平台(尤其是移动平台)上对JSON Web令牌
进行客户端处理的简便性。
如果您想了解有关JSON Web令牌的更多信息,甚至开始使用它们在自己的应用程序中执行身份验证,请浏览到 Auth0上的JSON Web令牌登录 页面。
作者: 龙衣
出处:https://www.cnblogs.com/gdragon/p/11878935.html
版权:本站使用「CC BY 4.0」创作共享协议,转载请在文章明显位置注明作者及出处。
JWT#
文章基本是官网内容的翻译,英文不错的同学可点击上面的链接直接看英文文档。
什么是 JWT#
JWT
全称是JSON Web Token(JWT)
是一个开放标准(RFC 7519)
,它定义了一种紧凑且自包含的方式,用于在各方之间作为JSON
对象安全地传输信息。由于此信息是经过数字签名的,因此可以被验证和信任。
可以使用密钥(HMAC算法
)或使用RSA
或ECDSA
的公用/专用密钥对对JWT
进行签名。
什么时候使用 JWT 验证#
- 授权
(Authorization)
这是使用JWT
的最常见情况。一旦用户登录,每个后续请求将包括JWT
,从而允许用户访问该令牌允许的路由,服务和资源。单一登录是当今广泛使用JWT
的一项功能,因为它的开销很小并且可以在不同的域中轻松使用。 - 信息交换
(Information Exchange)
JWT
是在各方之间安全地传输信息的好方法。因为可以对JWT
进行签名(例如,使用公钥/私钥对),所以您可以确保发件人是他们所说的人。另外,由于签名是使用Header
和payload
计算的,因此您还可以验证内容是否未被篡改。
JWT 的结构格式#
由三部分组成,这些部分由点.
分隔,分别是:
Header
Payload
Signature
因此,JWT
通常如下所示。
xxxxx.yyyyy.zzzzz
Header#
通常由两部分组成:
例如:
{
"alg": "HS256",
"typ": "JWT"
}
然后,将此JSON
通过Base64Url
编码以形成JWT
的第一部分。
Payload#
令牌的第二部分是有效负载
,其中包含声明。声明是有关实体(通常是用户)和其他数据的声明。共有三种类型的索赔: registered、public、private claims
Registered claims
这些是一组预定义的权利要求,不是强制性的,而是建议使用的,以提供一组有用的可互操作的权利要求。其中一些是:iss
(发出者),exp
(到期时间),sub
(主题),aud
(受众) 等。
Tip: 请注意,声明名称仅是三个字符,因为JWT
是紧凑的。Public claims
这些可以由使用JWT
的人员随意定义。但是为避免冲突,应在IANA JSON Web
令牌注册表中定义它们,或将其定义为包含抗冲突名称空间的URI
。Private claims
这些是自定义声明,旨在在同意使用它们的各方之间共享信息,既不是注册声明也不是公共声明。
有效负载示例:
{
"sub": "1234567890",
"name": "John Doe",
"admin": true }
同样需要Base64Url
编码,以形成JWT
的第二部分。
Signature#
签名(Signature)
用于验证消息在整个过程中没有更改,并且对于使用私钥进行签名的令牌,它还可以验证JWT
的发送者是它所说的真实身份。
例如,如果要使用HMAC SHA256
算法,则将通过以下方式创建签名:
HMACSHA256(
base64UrlEncode(header) + "." +
base64UrlEncode(payload),
secret)
将这三部分合并#
输出是三个由.
分隔的Base64-URL
字符串,可以在HTML
和HTTP
环境中轻松传递这些字符串,与基于XML
的标准(例如SAML
)相比,它更紧凑。
下图显示了一个JWT
,它已对先前的Header
和Payload
进行了编码,并用一个Signature
。
可以在这个网页 jwt.io Debugger 验证和生成JWT
JWT 如何工作#
在身份验证中,当用户使用其凭据成功登录时,将返回令牌。由于令牌是凭据,因此必须格外小心以防止安全问题。通常,令牌的有效时间不宜设置过长。
Tip: 由于缺乏安全性,您也不应该将敏感的会话数据存储在浏览器存储中。
每当用户想要访问受保护的路由或资源时,用户代理通常应在Bearer
模式中使用授权头发送JWT
。Header
的内容应如下所示:
Authorization: Bearer <token>
在某些情况下,接口访问并不需要身份授权。服务器的受保护路由将在Authorization Header
中检查JWT令牌
是否有效,如果存在且有效,则将允许用户访问受保护的资源。
如果JWT
包含必要的数据,则可以减少查询数据库中某些操作的需求。
如果令牌是在Authorization Header
中发送的,则跨域资源共享 (CORS) 不会成为问题,因为它不使用cookie
。
- 应用程序或客户端向授权服务器请求授权。生产
JWT令牌
。 - 授予授权后,授权服务器会将访问令牌返回给应用程序。
- 应用程序使用访问令牌来访问受保护的资源(例如API)。
- 服务器检查
JWT令牌
是否有效,返回对应结果给客户端
ps:请注意,使用签名令牌,令牌或令牌中包含的所有信息都会暴露给用户或其他方,即使他们无法更改它。这意味着您不应将机密信息放入令牌中。
为什么需要 JWT#
对比 Simple Web Tokens (SWT) 和Security Assertion Markup Language Tokens (SAML),看看使用JSON Web Tokens (JWT) 有什么好处。
- 由于
JSON
不如XML
冗长,因此在编码时JSON
的大小也较小,从而使JWT
比SAML
更紧凑。这使得JWT
是在HTML
和HTTP环境
中传递的不错的选择。 - 在安全方面,
SWT
只能使用HMAC算法
进行对称签名。但是JWT
和SAML令牌
可以使用X.509证书形式
的公用/专用密钥对进行签名。与签名JSON
的简单性相比,使用XML Digital Signature
签名XML
而不引入模糊的安全漏洞是非常困难的。 JSON
解析器在大多数编程语言中都很常见,因为它们直接映射到对象。相反,XML
没有自然的文档到对象映射。与SAML
断言相比,这使使用JWT
更加容易。- For the uses,
JWT
it is inInternet
use on the scale. This emphasizes the pair on multiple platforms (especially mobile platforms)JSON Web令牌
ease of client-side processing.
If you would like more information about JSON Web Token, and even started using them for authentication in your application, please browse to the JSON Web Token to log on Auth0 page.