博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
JSON Web Token
阅读量:6683 次
发布时间:2019-06-25

本文共 4141 字,大约阅读时间需要 13 分钟。

What is JSON Web Token?

JSON Web Token (JWT) is an open standard () 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.

Let's explain some concepts of this definition further.

  • Compact: Because of its smaller size, JWTs can be sent through an 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.

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 as they can be signed, for example using public/private key pairs, you can be sure that 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.

What is the JSON Web Token structure?

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

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.

For example:

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

Then, this JSON is Base64Url encoded to form the first part of the 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: reservedpublic, and private claims.

  • Reserved claims: These is 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.

An example of payload could be:

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

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

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:

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

The signature is used to verify that the sender of the JWT is who it says it is and to ensure that the message wasn't changed along the way.

Putting all together

The output is three Base64 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.

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

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

https://jwt.io/introduction/

 

转载地址:http://pzoao.baihongyu.com/

你可能感兴趣的文章
exchange2010赋予普通用户新建和删除邮箱的权限
查看>>
CRS-0215: Could not start resource 'ora.DB.DB2.inst'
查看>>
图片里的英语
查看>>
iscsi客户端配置
查看>>
用Windows Server实现软件定义存储之存储空间直连
查看>>
花(cnm加强)
查看>>
有用J2ee学习资料下载地址
查看>>
如何对Redis设置密码,提高安全性
查看>>
cognos如何制作维表左关联事实表的报表
查看>>
Android基础(五) – Service生命周期及启动方式
查看>>
Nginx反向代理后端多节点下故障节点的排除思路
查看>>
error: No curses/termcap library found
查看>>
android之shape使用
查看>>
Java八大基本数据类型
查看>>
yum安装JDK
查看>>
C#中Dictionary的用法及用途
查看>>
BizTalkServer 如何接收 EDI 消息(7)
查看>>
我的友情链接
查看>>
我的友情链接
查看>>
初出茅庐
查看>>