JWT认证原理

简介:

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 or ECDSA.

是一种小巧的、自包含的JSON对象,用于两个端系统之间的安全传输,它是一种数字标签,它可以使用了一种严格的算法进行签名,或者使用公钥和私钥的形式进行签名

JWT的使用场景

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

1、授权: 比如在web应用当中我们通常需要使用到授权,来给授权用户进行访问相应的路由、服务和资源

2、信息的交换:因为JWT是被签名的,所以能够非常安全的在端系统之间传输消息,通过JWT你可以确认发送者和内容是否被篡改

JWT的结构

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

Header

The header typically consists of two parts: the type of the token, which is JWT, and the signing 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 data. There are three types of claims: registered, public, 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.

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

An example 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 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.

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.

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

Encoded JWT

JWT是如何工作的

In authentication, when the user successfully logs in using their credentials, a JSON Web Token will be returned. Since tokens are credentials, great care must be taken to prevent security issues. In general, you should not keep tokens longer than required.

一般就是用户成功获取到资格后,服务器需要返回一个JWT,用户进行保存,当时注意JWT的保存时间,需要在特定时间进行删除,若用户需要再次获取,经过上一个步骤

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:↳

Authorization: Bearer <token>

当我们请求受保护的路由和资源的时候需要再请求中携带JWT,典型的就是在请求头中设置Authorization字段,如上

This can be, in certain cases, a stateless authorization mechanism. 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. If the JWT contains the necessary data, the need to query the database for certain operations may be reduced, though this may not always be the case.

Note that if you send JWT tokens through HTTP headers, you should try to prevent them from getting too big. Some servers don't accept more than 8 KB in headers. If you are trying to embed too much information in a JWT token, like by including all the user's permissions, you may need an alternative solution, like Auth0 Fine-Grained Authorization.

如果你携带了JWT,服务器的保护路由将会检查你头部的Authorization是否携带了一个真确的JWT,如果有就会放行,如果你JWT包括了一些必要的数据,比如userid 或者 username 对于某些操作,你就可以不需要去进行数据库的查询了,当是不能够让Http请求的头部较大,大部分的服务器接收的请求头不能操作8KB

If the token is sent in the Authorization header, Cross-Origin Resource Sharing (CORS) won't be an issue as it doesn't use cookies.

此处它指出,JWT并不需要使用cookies,所以能解决跨域问题,更适用于前后端分离的项目当中

The following diagram shows how a JWT is obtained and used to access APIs or resources:

How does a JSON Web Token work

  1. The application or client requests authorization to the authorization server. This is performed through one of the different authorization flows. For example, a typical OpenID Connect compliant web application will go through the /oauth/authorize endpoint using the authorization code flow.

  2. When the authorization is granted, the authorization server returns an access token to the application.

  3. The application uses the access token to access a protected resource (like an API).

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.

JWT官网

我之前使用Node.js去实现过,后续会将实现代码进行一个补充

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/775981.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

【信号处理】基于DGGAN的单通道脑电信号增强和情绪检测(tensorflow)

关于 情绪检测&#xff0c;是脑科学研究中的一个常见和热门的方向。在进行情绪检测的分类中&#xff0c;真实数据不足&#xff0c;经常导致情绪检测模型的性能不佳。因此&#xff0c;对数据进行增强&#xff0c;成为了一个提升下游任务的重要的手段。本项目通过DCGAN模型实现脑…

基于STC12C5A60S2系列1T 8051单片机的按键单击长按实现互不干扰增加减少数值应用

基于STC12C5A60S2系列1T 8051单片机的按键单击长按实现互不干扰增加减少数值应用 STC12C5A60S2系列1T 8051单片机管脚图STC12C5A60S2系列1T 8051单片机I/O口各种不同工作模式及配置STC12C5A60S2系列1T 8051单片机I/O口各种不同工作模式介绍基于STC12C5A60S2系列1T 8051单片机的…

第六届蓝桥杯大赛软件赛省赛Java 大学C组题解

文章目录 A 隔行变色思路解题方法复杂度Code B 立方尾不变思路解题方法复杂度Code C 无穷分数思路解题方法复杂度Code D 奇妙的数字思路解题方法复杂度Code E 移动距离思路解题方法复杂度Code F 垒骰子思路解题方法复杂度Code A 隔行变色 思路 这是一个简单的计数问题。我们需…

<chrono>, clock_gettime(), gettimeofday()对比

精度&#xff08;Precision&#xff09;&#xff1a; <chrono>: 提供了纳秒级别的精度&#xff0c;可以满足大多数应用的需求。clock_gettime(): 提供了纳秒级别的精度&#xff0c;与 <chrono> 相当。gettimeofday(): 提供了微秒级别的精度&#xff0c;相对于前两者…

ruoyi-ui(前端文件夹)

bin文件夹&#xff08;批处理文件&#xff0c;打包、运行&#xff09; build文件夹&#xff08;构建相关&#xff09; public文件夹&#xff08;公共文件&#xff0c;图标、html模板&#xff09; src文件夹&#xff08;前端相关源码&#xff09; api文件夹&#xff08;…

iscsi网络协议(连接硬件设备)

iscsi概念 iscsi是一种互联网协议&#xff0c;用于将存储设备&#xff08;如硬盘驱动器或磁带驱动器&#xff09;通过网络连接到计算机。它是一种存储区域网络&#xff08;SAN&#xff09;技术&#xff0c;允许服务器通过网络连接到存储设备&#xff0c;就像它们是本地设备一样…

区块链技术与大数据结合的商业模式探索

hello宝子们...我们是艾斯视觉擅长ui设计和前端开发10年经验&#xff01;希望我的分享能帮助到您&#xff01;如需帮助可以评论关注私信我们一起探讨&#xff01;致敬感谢感恩&#xff01; 随着区块链技术和大数据技术的不断发展&#xff0c;两者的结合为企业带来了新的商业模式…

探索K最近邻算法:从理论到实践

引言&#xff1a; 在机器学习领域中&#xff0c;有许多经典的算法被用于解决各种问题。其中之一就是K最近邻&#xff08;KNN&#xff09;算法。KNN是一种简单而强大的非参数化学习方法&#xff0c;被广泛用于分类和回归问题。本文将深入探讨KNN算法的原理、应用场景以及如何在实…

科东软件联手英特尔,用工业AI智能机器人赋能工业升级

AI浪潮已经冲击到各行各业中&#xff0c;它能够帮助人们提高思考和生产效率。在创作中&#xff0c;AI能够帮助人们释放创意&#xff0c;那在工业中&#xff0c;AI能够为产业带来什么呢&#xff1f; 科东软件是国内专注于操作系统开发的企业。当前&#xff0c;科东开发的Intewe…

机器学习——贝叶斯分类器(基础理论+编程)

目录 一、理论 1、初步引入 2、做简化 3、拉普拉斯修正 二、实战 1、计算P(c) 2、计算P(x|c) 3、实战结果 1、数据集展示 2、相关信息打印 一、理论 1、初步引入 在所有相关概率都已知的理想情形下&#xff0c;贝叶斯决策论考虑如何基于这些概率和误判损失来选择最…

Jenkins升级中的小问题

文章目录 使用固定版本安装根据jenkins页面下载war包升级jenkins重启jenkins报错问题解决 K8s部署过程中的一些小问题 ##### Jenkins版本小插曲 ​ 在Jenkins环境进行插件安装时全部清一色飘红&#xff0c;发现是因为Jenkins版本过低导致&#xff0c;报错的位置可以找到更新je…

巨控GRM560工业物联网的升级后的功能

巨控GRM560&#xff1a;工业自动化领域的革命者 标签:#工业自动化 #PLC #远程控制 #OPCUA #MQTT 随着工业4.0时代的到来&#xff0c;智能制造已经成为了发展的大势所趋。在这样的背景下&#xff0c;自动化控制系统的核心——可编程逻辑控制器&#xff08;PLC&#xff09;的作用…

JS学习之旅第七天

今天是学习JS的第七天&#xff0c;今天学习了数组&#xff0c;话不多说开始今天的讲解。 一、认识数组 数组是指一组数据的集合&#xff0c;其中的每个数据被称作元素&#xff0c;在数组中可以存放任意类型的元素。数组是一种将一组数据存储在单个变量名下的优雅方式。 1.1创…

shell脚本发布docker-nginx vue2 项目示例

docker、git、node.js安装略过。 使git pull或者git push不需要输入密码操作方法 nginx安装在docker容器里面&#xff0c;参见&#xff1a;https://blog.csdn.net/HSJ0170/article/details/128631155 姊妹篇&#xff08;宿主机nginx&#xff0c;非docker-nginx&#xff09;&am…

Vue计算属性computed深度解析,告别只会使用,迎来全面理解

一、基础示例 模板中的表达式虽然方便&#xff0c;但也只能用来做简单的操作。如果在模板中写太多逻辑&#xff0c;会让模板变得臃肿&#xff0c;难以维护。比如说&#xff0c;有这样一个包含嵌套数组的对象&#xff1a; const author reactive({name: John Doe,books: [Vue 2…

基于java+SpringBoot+Vue的数码论坛系统设计与实现

基于javaSpringBootVue的数码论坛系统设计与实现 开发语言: Java 数据库: MySQL技术: SpringBoot MyBatis工具: IDEA/Eclipse、Navicat、Maven 系统展示 前台展示 后台展示 系统简介 整体功能包含&#xff1a; 数码论坛系统是一个基于互联网的数码产品讨论和信息分享平台…

深度学习语义分割篇——DeepLabV2原理详解篇

&#x1f34a;作者简介&#xff1a;秃头小苏&#xff0c;致力于用最通俗的语言描述问题 &#x1f34a;专栏推荐&#xff1a;深度学习网络原理与实战 &#x1f34a;近期目标&#xff1a;写好专栏的每一篇文章 &#x1f34a;支持小苏&#xff1a;点赞&#x1f44d;&#x1f3fc;、…

小狐狸JSON-RPC:wallet_watchAsset(向钱包中新增资产代币)

wallet_watchAsset 请求用户在 MetaMask 中添加新的资产。返回一个布尔值&#xff0c;是否已成功添加。 var res await window.ethereum.request({ "method": "wallet_watchAsset","params": {"type": "ERC20","opti…

盘点库存怎么做账

库存的盘点是企业中非常重要的一步&#xff0c;也是仓管经常要做的工作&#xff0c;盘点通俗点说就是点一下实物与账面上的数据是否一至&#xff0c;来判断我们平时的货物管理是否与账面上的业务往来符合&#xff0c;盘点库存怎么做账&#xff1f; 按目前的情况来看&#xff0c…

什么是软件工程?如何应用软件工程原则?

什么是软件工程&#xff1f; 软件工程是应用工程原则来设计、开发、维护、测试和评估计算机软件的过程。它涵盖了软件开发的整个生命周期&#xff0c;包括需求收集和分析、系统设计、编码、集成和测试、部署以及维护和支持。软件工程的目的是为了确保软件系统的可靠性、效率、…