Full stack items | small bookcase | server development -JWT explain the full stack items | small bookcase | server development -JWT Comments

Excerpt: https://www.cnblogs.com/gdragon/p/11878935.html

Full stack items | small bookcase | server development -JWT Comments

 

JWT#

Official Description: Introduction to JSON Web Tokens

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 #

JWTFull name is JSON Web Token(JWT)a 开放标准(RFC 7519), which defines a compact and self-contained manner, as between the parties for JSONthe 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 RSAor ECDSApublic / private key pair JWTfor signing.

When to use JWT verify #

  • Authorization (Authorization)
    This is done using JWTthe most common situations. Once the user logs on, will include every subsequent request JWT, thereby allowing the user to access the token allows routing, services and resources. Single Sign-On is widely used today JWTa feature, because it's low overhead and can easily be used in different domains.
  • Information exchange (Information Exchange)
    JWTis a good way to transfer information securely between the parties. Because you can to JWTsign (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 use Headerand payloadcalculation, 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, JWTas shown generally as follows.

Copy
xxxxx.yyyyy.zzzzz

Header#

Normally it consists of two parts:

  • Type of the token (i.e., JWT)
  • Signature algorithm used, such as: HMAC  SHA256 or RSA .

E.g:

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

Then, this JSONby Base64Urlencoded to form JWTa 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, as JWTis compact.
  • Public claims
    These can be made using JWTthe definition of casual staff. However, in order to avoid conflicts, you should IANA JSON Webdefine them tokens in the registry, or define it as containing anti-collision namespace URI.
  • 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:

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

Also it needs Base64Urlcoding, to form JWTthe 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 JWTsender's true identity is what it says.

For example, if you want to use the HMAC SHA256algorithm, the signature will be created in the following ways:

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

These three parts merge #

Is output from the three .separated Base64-URLstring, may be HTMLand HTTPeasily pass these strings environments, based on XMLcriteria (e.g. SAML) compared to its more compact.

The following figure shows a JWT, it has the previous Headerand Payloadare encoded, and with a Signature.
Here Insert Picture Description

On this page you can  jwt.io Debugger  validation and generationJWT
Here Insert Picture Description

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 Bearersent using Authorization header mode JWT. HeaderContent should be as follows:

Copy
Authorization: Bearer <token>

In some cases, the interface does not require access authorization identity. Protection by routing servers will Authorization Headercheck JWT令牌whether a valid and effective if there is, it will allow users to access protected resources.

If JWTcontain the necessary data, it can reduce demand for certain operations query the database.

如果令牌是在Authorization Header中发送的,则跨域资源共享 (CORS) 不会成为问题,因为它不使用cookie

下图显示了如何获取JWT并将其用于访问API或资源
Here Insert Picture Description

  1. 应用程序或客户端向授权服务器请求授权。生产JWT令牌
  2. 授予授权后,授权服务器会将访问令牌返回给应用程序。
  3. 应用程序使用访问令牌来访问受保护的资源(例如API)。
  4. 服务器检查JWT令牌是否有效,返回对应结果给客户端

下图详细的流程:
Here Insert Picture Description

ps:请注意,使用签名令牌,令牌或令牌中包含的所有信息都会暴露给用户或其他方,即使他们无法更改它。这意味着您不应将机密信息放入令牌中。

为什么需要 JWT#

对比 Simple Web Tokens (SWT) 和Security Assertion Markup Language Tokens (SAML),看看使用JSON Web Tokens (JWT) 有什么好处。

  • 由于JSON不如XML冗长,因此在编码时JSON的大小也较小,从而使JWTSAML更紧凑。这使得JWT是在HTMLHTTP环境中传递的不错的选择。
  • 在安全方面,SWT只能使用HMAC算法进行对称签名。但是JWTSAML令牌可以使用X.509证书形式的公用/专用密钥对进行签名。与签名JSON的简单性相比,使用XML Digital Signature签名XML而不引入模糊的安全漏洞是非常困难的。
  • JSON解析器在大多数编程语言中都很常见,因为它们直接映射到对象。相反,XML没有自然的文档到对象映射。与SAML断言相比,这使使用JWT更加容易。
  • 关于用法,JWT是在Internet规模上使用的。这强调了在多个平台(尤其是移动平台)上对JSON Web令牌进行客户端处理的简便性。

如果您想了解有关JSON Web令牌的更多信息,甚至开始使用它们在自己的应用程序中执行身份验证,请浏览到 Auth0上的JSON Web令牌登录 页面。


咨询请加微信:轻撩即可。
Here Insert Picture Description

作者: 龙衣

出处:https://www.cnblogs.com/gdragon/p/11878935.html

版权:本站使用「CC BY 4.0」创作共享协议,转载请在文章明显位置注明作者及出处。

 
标签:  JWTNodeJS
 

JWT#

官方简介:Introduction to JSON Web Tokens

文章基本是官网内容的翻译,英文不错的同学可点击上面的链接直接看英文文档。

什么是 JWT#

JWT全称是JSON Web Token(JWT)是一个开放标准(RFC 7519),它定义了一种紧凑且自包含的方式,用于在各方之间作为JSON对象安全地传输信息。由于此信息是经过数字签名的,因此可以被验证和信任。

可以使用密钥(HMAC算法)或使用RSAECDSA的公用/专用密钥对对JWT进行签名。

什么时候使用 JWT 验证#

  • 授权(Authorization)
    这是使用JWT的最常见情况。一旦用户登录,每个后续请求将包括JWT,从而允许用户访问该令牌允许的路由,服务和资源。单一登录是当今广泛使用JWT的一项功能,因为它的开销很小并且可以在不同的域中轻松使用。
  • 信息交换(Information Exchange)
    JWT是在各方之间安全地传输信息的好方法。因为可以对JWT进行签名(例如,使用公钥/私钥对),所以您可以确保发件人是他们所说的人。另外,由于签名是使用Headerpayload计算的,因此您还可以验证内容是否未被篡改。

JWT 的结构格式#

由三部分组成,这些部分由点.分隔,分别是:

  • Header
  • Payload
  • Signature

因此,JWT通常如下所示。

Copy
xxxxx.yyyyy.zzzzz

Header#

通常由两部分组成:

  • 令牌的类型(即JWT
  • 所使用的签名算法,例如:HMAC SHA256RSA

例如:

Copy
{
  "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
    这些是自定义声明,旨在在同意使用它们的各方之间共享信息,既不是注册声明也不是公共声明。

有效负载示例:

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

同样需要Base64Url编码,以形成JWT的第二部分。

Signature#

签名(Signature)用于验证消息在整个过程中没有更改,并且对于使用私钥进行签名的令牌,它还可以验证JWT的发送者是它所说的真实身份。

例如,如果要使用HMAC SHA256算法,则将通过以下方式创建签名:

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

将这三部分合并#

输出是三个由.分隔的Base64-URL字符串,可以在HTMLHTTP环境中轻松传递这些字符串,与基于XML的标准(例如SAML)相比,它更紧凑。

下图显示了一个JWT,它已对先前的HeaderPayload进行了编码,并用一个Signature
Here Insert Picture Description

可以在这个网页 jwt.io Debugger 验证和生成JWT
Here Insert Picture Description

JWT 如何工作#

在身份验证中,当用户使用其凭据成功登录时,将返回令牌。由于令牌是凭据,因此必须格外小心以防止安全问题。通常,令牌的有效时间不宜设置过长。

Tip: 由于缺乏安全性,您也不应该将敏感的会话数据存储在浏览器存储中。

每当用户想要访问受保护的路由或资源时,用户代理通常应在Bearer模式中使用授权头发送JWTHeader的内容应如下所示:

Copy
Authorization: Bearer <token>

在某些情况下,接口访问并不需要身份授权。服务器的受保护路由将在Authorization Header中检查JWT令牌是否有效,如果存在且有效,则将允许用户访问受保护的资源。

如果JWT包含必要的数据,则可以减少查询数据库中某些操作的需求。

如果令牌是在Authorization Header中发送的,则跨域资源共享 (CORS) 不会成为问题,因为它不使用cookie

下图显示了如何获取JWT并将其用于访问API或资源
Here Insert Picture Description

  1. 应用程序或客户端向授权服务器请求授权。生产JWT令牌
  2. 授予授权后,授权服务器会将访问令牌返回给应用程序。
  3. 应用程序使用访问令牌来访问受保护的资源(例如API)。
  4. 服务器检查JWT令牌是否有效,返回对应结果给客户端

下图详细的流程:
Here Insert Picture Description

ps:请注意,使用签名令牌,令牌或令牌中包含的所有信息都会暴露给用户或其他方,即使他们无法更改它。这意味着您不应将机密信息放入令牌中。

为什么需要 JWT#

对比 Simple Web Tokens (SWT) 和Security Assertion Markup Language Tokens (SAML),看看使用JSON Web Tokens (JWT) 有什么好处。

  • 由于JSON不如XML冗长,因此在编码时JSON的大小也较小,从而使JWTSAML更紧凑。这使得JWT是在HTMLHTTP环境中传递的不错的选择。
  • 在安全方面,SWT只能使用HMAC算法进行对称签名。但是JWTSAML令牌可以使用X.509证书形式的公用/专用密钥对进行签名。与签名JSON的简单性相比,使用XML Digital Signature签名XML而不引入模糊的安全漏洞是非常困难的。
  • JSON解析器在大多数编程语言中都很常见,因为它们直接映射到对象。相反,XML没有自然的文档到对象映射。与SAML断言相比,这使使用JWT更加容易。
  • For the uses, JWTit is in Internetuse 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.


Advice please add micro letter: Light to tease.
Here Insert Picture Description

Guess you like

Origin www.cnblogs.com/xichji/p/11884246.html