当前位置:网站首页>JWT diagram
JWT diagram
2022-07-25 15:56:00 【51CTO】
JWT brief introduction
JWT(Json Web Token) adopt json form , As web Token in application , Used to securely treat information as between parties json Object creation , In the process of data transmission, data encryption can also be completed 、 Signature and other related processing , The characteristics are as follows :
- Cross language : Support for mainstream languages
- Self contained : Include all necessary information , Such as user information and signature
- Easy to transmit : It's easy to get through HTTP Head pass
Conventional session authentication

There are problems with traditional schemes :
- More users , The more expensive the server is
- cookie It's not safe
- In the case of clusters , Users must be on the same server or through session Sharing mechanism realizes authentication
- Front and rear end separation increases complexity , Such as : agent 、 Routing Gateway
be based on JWT authentication

The certification process
- First , Front end by Web The form sends its user name and password to the backend . This process is generally a HTTP POST request . The way to suggest it is through SSL Encrypted transmission (https agreement ), So as to avoid sensitive information being sniffed
- After the back end checks the user name and password successfully , The user's id And other information as JWT Payload( load ), Separate it from the head Base64 Code stitching and signature , Form an identity xxx.yyy.zzz A string of JWT(Token)
- Backend will JWT The string is returned to the front end as the result of successful login . The front end can save the returned results in localStorage or sessionStorage On , When logging out, the front end will delete the saved JWT that will do
- The front end will JWT Put in HTTP Header Medium Authorization position .( solve XSS and XSRF problem )
- Back end check JWT Whether there is , If it exists, verify its effectiveness . for example , Check that the signature is correct ; Check Token Is it overdue ; Check Token Whether the recipient is himself ( Optional )
- After verification, the back end uses JWT The user information contained in is used for other logical operations , Return the corresponding results
JWT The advantages of
- concise (Compact): Can pass URL,POST Parameters may be in HTTP header Sent in , Small amount of data , Fast transmission speed
- Self contained (Self-contained): The load contains the information that users need , Avoid multiple queries to the database
- because Token In order to JSON The encrypted form is saved on the client , therefore JWT It's Cross Language Of , In principle, any web All forms support
- There is no need to save session information on the server , Especially for distributed microservices
JWT Structure
Token yes String ====> header.payload.signature
1、 token (token) form
- header (header)
- Payload (payload)
- Signature (signature)
2、Header
- The header usually consists of two parts :
Type of token( namely JWT) And so onThe algorithm used - It will use
Base64Code composition JWT The first part of the structure - Be careful :Base64 It's a kind of coding , in other words , He can translate back to the original data , It's not an encryption process
3、payload
- The second part of the token is
Payload, It contains a statement , Life is aboutEntity( It is usually a declaration of users and other data ) - It USES
Base64The code consists of JWT The second part of the structure - Be careful : Don't store sensitive information (eg.password)
4、signature
- The first two parts are Base64 Coded , That is, the front end can unlock the information inside
- signature You need to use the encoded Header and payload And myself
A key provided ( That is, salt value ), Use header The algorithm specified in (HS256) To sign - The function of signature
To ensure the JWT It has not been tampered with
eg.
Verification : header + payload + Salt value == signature The data of
JWT Common abnormal information in
AlgorithmMismatchExceptionAlgorithm mismatch exception ( That is, the algorithm used is inconsistent )SignatureVerificationExceptionSignature inconsistency exception ( That is, the signatures used are inconsistent )InvalidClaimExceptionInvalid payload abnormal ( That is, the data is tampered )TokenExpiredExceptionToken expiration exception
Java Use JWT actual combat
- Import JWT rely on
- To write JWT Tool class
- Write interceptors
- Register interceptors
- Other related configurations ( A little .)

test result :
- Do not log in to visit index data

- The logged in user , obtain token

- Use token visit index

边栏推荐
- Solve the vender-base.66c6fc1c0b393478adf7.js:6 typeerror: cannot read property 'validate' of undefined problem
- User defined annotation verification API parameter phone number
- Idea - click the file code to automatically synchronize with the directory
- Matlab -- CVX optimization kit installation
- How to solve cross domain problems
- Redis分布式锁,没它真不行
- Wechat applet
- The difference between mouseover and mouseenter
- Activity review | July 6 Anyuan AI X machine heart series lecture No. 2 | MIT professor Max tegmark shares "symbiotic evolution of human and AI"
- Cf566a greed + dictionary tree
猜你喜欢

【莎士比亚:保持做人的乐趣】
SQL cultivation manual from scratch - practical part

JVM knowledge brain map sharing

一文入门Redis

Pat grade a 1152 Google recruitment (20 points)

Gary Marcus: 学习语言比你想象的更难

MATLAB optimization tool manopt installation

LeetCode - 380 O(1) 时间插入、删除和获取随机元素 (设计 哈希表+数组)

Pytorch学习笔记-刘二老师RNN高级篇-代码注释及结果

Leetcode - 362 knock counter (Design)
随机推荐
var、let、const之间的区别
兆骑科创海内外高层次创新创业人才服务平台,双创成果转化平台
Idea - click the file code to automatically synchronize with the directory
Leetcode - 622 design cycle queue (Design)
C # fine sorting knowledge points 12 exception handling (recommended Collection)
JVM - classloader and parental delegation model
Pytorch学习笔记--常用函数总结3
MySQL—常用SQL语句整理总结
Gary Marcus: 学习语言比你想象的更难
LeetCode - 359 日志速率限制器 (设计)
Games101 review: linear algebra
MySQL教程66-数据表查询语句
Zhaoqi Kechuang high-quality overseas returnee talent entrepreneurship and innovation service platform, online live broadcast Roadshow
Pytoch learning notes - Teacher Liu Er RNN advanced chapter - code comments and results
Endnote add Chinese gbt7714 style how to quote documents in word
MySQL - user and permission control
ZOJ - 4114 flipping game DP, reasonable state representation
Beyond compare 4 realizes class file comparison [latest]
Baseband simulation system experiment of 4pam in Gaussian channel and Rayleigh channel
BSC smart chain contract mode system development details