文本将介绍一种在Web应用中安全传递信息的方式,称为JWT。

本文内容是对JWT官网介绍说明的英文翻译而来,由于本文英文水平有限,如有错误,还请指出,谢谢。

What is JSON Web Token?

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.

JSON Web Token(JWT)是一个开放规范(RFC7519),该规范为安全的信息传递定义了一个紧凑自包含的方式。由于信息是被数字签名的,所以信息是可以被认证和信任的。JWT可以通过一个秘钥也可以通过公钥/私钥对的方式进行签名。

Although JWTs can be encrypted to also provide secrecy between parties, we will focus on signed tokens. Signed tokens can verify the integrity of the claims contained within it, while encrypted tokens hide those claims from other parties. When tokens are signed using public/private key pairs, the signature also certifies that only the party holding the private key is the one that signed it.

尽管JWTs可以被加密从而提供安全性,但是我们重心还是放在这些被签名的token上。签名令牌可以验证包含在其中的信息的完整性,与此同时加密令牌则隐藏了来自其他方的相关声明。当令牌被通过公钥/私钥对进行签名,签名还要求只有拥有私钥的一方是签名方。

Let's explain some concepts further.

  • Compact: Because of their smaller size, JWTs can be sent through a URL, POST parameter, or inside an HTTP header. Additionally, the smaller size means transmission is fast.

  • Self-contained: The payload contains all the required information about the user, avoiding the need to query the database more than once.

让我来解释一下后面的一些概念。

  • 紧凑: 由于他们信息传递内容之小,JWTs可以通过URL、Post参数或是HTTP头的方式进行发送。另外,传递的信息小意味着传输快。

  • 自包含: 载荷包含了所有关于用户所需要的信息,避免了查询数据库超过一次的需要。

When should you use JSON Web Tokens?

Here are some scenarios where JSON Web Tokens are useful:

  • Authentication: This is the most common scenario for using JWT. Once the user is logged in, each subsequent request will include the JWT, allowing the user to access routes, services, and resources that are permitted with that token. Single Sign On is a feature that widely uses JWT nowadays, because of its small overhead and its ability to be easily used across different domains.

  • Information Exchange: JSON Web Tokens are a good way of securely transmitting information between parties. Because JWTs can be signed—for example, using public/private key pairs—you can be sure the senders are who they say they are. Additionally, as the signature is calculated using the header and the payload, you can also verify that the content hasn't been tampered with.

这里是JWTs有用的一些场景:

  • 认证: 这是使用JWT最常用的应用场景。只要用户已登录,后续的每次请求都将包含JWT,从而使得用户能够获取只有令牌才被允许的服务和资源。单点登录是目前广泛使用JWT的一个功能,因为它的开销很小,并且具有很容易跨领域使用的能力。

  • 信息交换: JWTs是双方之间安全信息传递的一种良好方式。由于JWTs可以被签名,比如使用公钥/私钥对,你可以确保发送者就是他所说的那个人。另外,由于签名是使用头部和载荷通过计算得到的,所以通过比较签名可以验证内容没有被篡改。

What is the JSON Web Token structure?

In its compact form, JSON Web Tokens consist of three parts separated by dots (.), which are:

  • Header
  • Payload
  • Signature

Therefore, a JWT typically looks like the following.

xxxxx.yyyyy.zzzzz

在它的紧凑格式中,JWTs由以下三个部分通过点号.拼接而成,这三个部分为:

  • 头部
  • 载荷
  • 签名

因此,一个JWT看起来如下所示:

xxxxx.yyyyy.zzzzz

Let's break down the different parts.

让我们分解这三个不同的部分来看下。

Header

The header typically consists of two parts: the type of the token, which is JWT, and the hashing algorithm being used, such as HMAC SHA256 or RSA.

头部一般包含两个部分:令牌的类型(这里是JWT)和所使用的哈希算法,比如HMAC、SHA256或者RSA。

For example:

  1.  
    {
  2.  
    "alg": "HS256",
  3.  
    "typ": "JWT"
  4.  
    }

Then, this JSON is Base64Url encoded to form the first part of the JWT.

紧接着,这个JSON被BASE64编码,并作为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: registeredpublic, 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.

    Notice that the claim names are only three characters long as JWT is meant to be compact.

  • 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.

令牌的第二部分是载荷,载荷包含了一些信息声明.声明的信息包括一个实体和一些额外的元数据。声明包含三个部分:注册声明、公共声明和私有声明。

  • 注册声明: 这里有一组预定义的声明,这些声明不是强制的,而是推荐的,用于提供一组有用的可互操作的声明。其中一些是:iss(发行者),exp(过期时间),sub (subject), aud(观众),以及其他。

    注意,声明的名称都是三个字符长度大小,因为JWT要求紧凑。

  • 公共声明: 这些声明可以由使用JWTs的人进行定义. 但是为了避免冲突,这些声明应该在IANA JSON Web Token Registry中定义过,或者被定义为包含一个抗冲突名称空间的URI。

  • 私有声明: 这些是自定义的声明,用于在双方同意使用它们的情况下共享信息,而不是注册或公开声明。

An example payload could be:

一个载荷的例子可能如下:

  1.  
    {
  2.  
    "sub": "1234567890",
  3.  
    "name": "John Doe",
  4.  
    "admin": true
  5.  
    }

The payload is then Base64Url encoded to form the second part of the JSON Web Token.

载荷部分也会被BASE64编码,并且作为JWT的第二部分。

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:

举个例子,如果你想使用HMAC SHA256算法,那么签名将通过如下的方式计算获得:

  1.  
    HMACSHA256(
  2.  
    base64UrlEncode(header) + "." +
  3.  
    base64UrlEncode(payload),
  4.  
    secret)

The signature is used to verify the message wasn't changed along the way, and, in the case of tokens signed with a private key, it can also verify that the sender of the JWT is who it says it is.

签名被用于校验信息是否在中途被篡改过,在用私钥签名的情况下,它还可以验证JWT的发送者就是他说的自己。

Putting all together

The output is three Base64-URL strings separated by dots that can be easily passed in HTML and HTTP environments, while being more compact when compared to XML-based standards such as SAML.

输出是三个使用点号(.)分隔的Base64-URL字符串,这些字符串由可以在HTML和HTTP环境中轻松传递,当与像SAML这样的基于xml的标准相比,它更紧凑。

The following shows a JWT that has the previous header and payload encoded, and it is signed with a secret.

下面展示了一个JWT,包含了前面的头部和载荷的编码,并且使用了一个秘钥进行了签名。(红色为头部编码、紫色为载荷编码、蓝色为签名部分)

If you want to play with JWT and put these concepts into practice, you can use jwt.io Debugger to decode, verify, and generate JWTs.

如果您想要使用JWT并将这些概念付诸实践,您可以使用jwt.io Debugger进行解码、验证和生成JWTs。

How do JSON Web Tokens work?

JSON Web Tokens是如何工作的?

In authentication, when the user successfully logs in using their credentials, a JSON Web Token will be returned and must be saved locally (typically in local storage, but cookies can be also used), instead of the traditional approach of creating a session in the server and returning a cookie.

在认证中,当用户使用它们的凭证成功登录时,将会返回一个JWT令牌,并且这个令牌将会被保存在本地(通常在本地存储,也可以使用cookies保存),而不是像传统途径一样在服务器上创建一个session并且返回一个cookie。

There are security considerations that must be taken into account with regards to the way tokens are stored. These are enumerated in Where to Store Tokens.

在令牌以什么样的方式存储这个问题上,有一些安全性的问题需要纳入到考虑范围。这些问题在Where to Store Tokens进行了说明。

Whenever the user wants to access a protected route or resource, the user agent should send the JWT, typically in the Authorization header using the Bearer schema. The content of the header should look like the following:

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

Authorization: Bearer <token>

This is a stateless authentication mechanism as the user state is never saved in server memory.The server's protected routes will check for a valid JWT in the Authorization header, and if it's present, the user will be allowed to access protected resources. As JWTs are self-contained, all the necessary information is there, reducing the need to query the database multiple times.

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

This allows you to fully rely on data APIs that are stateless and even make requests to downstream services. It doesn't matter which domains are serving your APIs, so Cross-Origin Resource Sharing (CORS) won't be an issue as it doesn't use cookies.

这允许您完全依赖无状态的数据api,甚至可以向下游服务发出请求。哪个域名为您的api提供服务并不重要,所以跨源资源共享(CORS)不会成为一个问题,因为它不使用cookie。

The following diagram shows this process:

下图显示了这个过程:

Do note that with signed tokens, all the information contained within the token is exposed to users or other parties, even though they are unable to change it. This means you should not put secret information within the token.

请注意,在带有签名的标记的情况下,令牌中包含的所有信息都将暴露给用户或其他参与方,即使他们无法更改它。这意味着您不应该在令牌中放置机密信息。

Why should we use JSON Web Tokens?

我们为什么要用JSON Web Tokens?

Let's talk about the benefits of JSON Web Tokens (JWT) when compared to Simple Web Tokens (SWT) and Security Assertion Markup Language Tokens (SAML).

与简单的Web令牌(SWT)和安全断言标记语言令牌(SAML)相比,让我们讨论一下JSON Web令牌(JWT)的好处。

As JSON is less verbose than XML, when it is encoded its size is also smaller, making JWT more compact than SAML. This makes JWT a good choice to be passed in HTML and HTTP environments.

由于JSON比XML更小,当它被编码时,它的大小也更小,使得JWT比SAML更紧凑。这使得在HTML和HTTP环境中使用JWT进行传递是个更好的选择。

Security-wise, SWT can only be symmetrically signed by a shared secret using the HMAC algorithm. However, JWT and SAML tokens can use a public/private key pair in the form of a X.509 certificate for signing. Signing XML with XML Digital Signature without introducing obscure security holes is very difficult when compared to the simplicity of signing JSON.

安全方面,SWT只能通过使用HMAC算法的共享密钥来对称地签名。但是,JWT和SAML令牌可以使用一个公共/私有密匙对,以X.509证书的形式签名。与简单的签名JSON相比,在不引入晦涩的安全漏洞的情况下使用XML数字签名来签署XML是非常困难的。

JSON parsers are common in most programming languages because they map directly to objects. Conversely, XML doesn't have a natural document-to-object mapping. This makes it easier to work with JWT than SAML assertions.

JSON解析器在大多数编程语言中都很常见,因为它们直接映射到对象。相反,XML没有自然的文档到对象的映射。这使得使用JWT比SAML断言更容易。

Regarding usage, JWT is used at Internet scale. This highlights the ease of client-side processing of the JSON Web token on multiple platforms, especially mobile.

关于使用,JWT在互联网规模使用。这突出显示了在多个平台上的JSON Web标记的客户端处理的易用性,尤其是移动端。

Comparison of the length of an encoded JWT and an encoded SAML

If you want to read more about JSON Web Tokens and even start using them to perform authentication in your own applications, browse to the JSON Web Token landing page at Auth0.

如果您想阅读更多关于JSON Web令牌的内容,甚至开始使用它们在您自己的应用程序中执行身份验证,请浏览到Auth0的JSON Web Token landing page页面。

JWT(Json Web Token):一种在Web应用中安全传递信息的规范 转载的更多相关文章

  1. [转] JSON Web Token in ASP.NET Web API 2 using Owin

    本文转自:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/ ...

  2. JSON Web Token in ASP.NET Web API 2 using Owin

    In the previous post Decouple OWIN Authorization Server from Resource Server we saw how we can separ ...

  3. 基于JWT(Json Web Token)的ASP.NET Web API授权方式

    token应用流程 初次登录:用户初次登录,输入用户名密码 密码验证:服务器从数据库取出用户名和密码进行验证 生成JWT:服务器端验证通过,根据从数据库返回的信息,以及预设规则,生成JWT 返还JWT ...

  4. Web Token JWT

    基于Token的WEB后台认证机制 JSON Web Token in ASP.NET Web API 2 using Owin 翻译:Token Authentication in ASP.NET ...

  5. JWT【JSON Web Token】 简述

    draft: http://self-issued.info/docs/draft-ietf-oauth-json-web-token.html http://tools.ietf.org/html/ ...

  6. 什么是JWT(JSON WEB TOKEN)

    转自于:http://www.jianshu.com/p/576dbf44b2ae 什么是JWT Json web token(JWT)是为了网络应用环境间传递声明而执行的一种基于JSON的开发标准( ...

  7. JWT(JSON Web Token) 多网站的单点登录,放弃session

    多个网站之间的登录信息共享, 一种解决方案是基于cookie - session的登录认证方式,这种方式跨域比较复杂. 另一种替代方案是采用基于算法的认证方式, JWT(json web token) ...

  8. 深入浅出JWT(JSON Web Token )

    1. JWT 介绍 JSON Web Token(JWT)是一个开放式标准(RFC 7519),它定义了一种紧凑且自包含的方式,用于在各方之间以JSON对象安全传输信息. 这些信息可以通过数字签名进行 ...

  9. Java JWT: JSON Web Token

    Java JWT: JSON Web Token for Java and Android JJWT aims to be the easiest to use and understand libr ...

随机推荐

  1. C++ class 中的 const 成员函数

    const 修饰的成员函数  表示  不会修改class中的成员变量. const 和 非-const 的成员函数同事存在时, 用户定义 const 类对象,调用 const 成员函数: 定义 非-c ...

  2. leetcode347. 前 K 个高频元素

    题目最终需要返回的是前 kk 个频率最大的元素,可以想到借助堆这种数据结构,对于 kk 频率之后的元素不用再去处理,进一步优化时间复杂度. 具体操作为: 借助 哈希表 来建立数字和其出现次数的映射,遍 ...

  3. 【电脑】win10开启telnet服务

  4. 7.Go-用户信息和系统文件/目录

    7.1.获取操作系统用户信息 (1)os包及子包功能 os/exec包,负责执行外部命令 os/singal对输入信息的访问 os/user通过名次或ID 查询用户账号 (2)在os/user中提供了 ...

  5. CF1178D Prime Graph

    题目链接 题意 构造一张有\(n(3\le n\le 1000)\)个点的无向图(无重边和自环).满足: 边的总数为素数 所有点的度数均为素数 输出方案 solution 如果所有点的度数确定了.那么 ...

  6. USB鼠标抓包数据(转)

    https://blog.csdn.net/zqixiao_09/article/details/53056854

  7. mysql8

    解决navicat不能连接问题: grant all privileges on *.* to ‘root’@’%’;ALTER USER 'root'@'localhost' IDENTIFIED ...

  8. 海边拾贝-B-优秀博客/网站

    记下若干优秀博客,方便后期检索.会不定期更新: 优秀的程序员,从使用Github开始:https://help.github.com/en/github/managing-your-work-on-g ...

  9. Leetcode算法题 7. Reverse Integer2

    7. Reverse Integer 题目描述: Given a 32-bit signed integer, reverse digits of an integer. Example 1: Inp ...

  10. paramiko简介

    一.什么是paramiko 要想明白什么是paramiko,要先明白ssh协议. 二.什么是ssh协议 ssh全称是Secure Shell (翻译:安全的外壳),根据字面意思就可以知道是和安全相关的 ...