当前位置:网站首页>实战模拟│JWT 登录认证
实战模拟│JWT 登录认证
2022-07-05 12:35:00 【51CTO】
目录
Token 认证流程
- 作为目前最流行的跨域认证解决方案,
JWT(JSON Web Token)深受开发者的喜爱,主要流程如下: - 客户端发送账号和密码请求登录
- 服务端收到请求,验证账号密码是否通过
- 验证成功后,服务端会生成唯一的
token,并将其返回给客户端 - 客户端接受到
token,将其存储在cookie或者localStroge中 - 之后每一次客户端向服务端发送请求,都会通过
cookie或者header携带该token - 服务端验证
token的有效性,通过才返回响应的数据

Token 认证优点
- 支持跨域访问:
Cookie是不允许跨域访问的,这一点对Token机制是不存在的,前提是传输的用户认证信息通过HTTP头传输 - 无状态:
Token机制在服务端不需要存储session信息,因为Token自身包含了所有登录用户的信息,只需要在客户端的cookie或本地介质存储状态信息 - 适用性更广: 只要是支持
http协议的客户端,就可以使用token认证。 - 无需考虑CSRF: 由于不再依赖
cookie,所以采用token认证方式不会发生CSRF,所以也就无需考虑CSRF的防御
JWT 结构
- 一个
JWT实际上就是一个字符串,它由三部分组成:头部、载荷与签名。中间用点.分隔成三个部分。注意JWT内部是没有换行的。

- 头部 / header
header由两部分组成:token的类型JWT和算法名称:HMAC、SHA256、RSA
- 载荷 / Payload
Payload部分也是一个JSON对象,用来存放实际需要传递的数据。JWT指定七个默认字段供选择。- 除了默认字段之外,你完全可以添加自己想要的任何字段,一般用户登录成功后,就将用户信息存放在这里
- 签名 / Signature
- 签名部分是对上面的 头部、载荷 两部分数据进行的数据签名
- 为了保证数据不被篡改,则需要指定一个密钥,而这个密钥一般只有你知道,并且存放在服务端
- 生成签名的代码一般如下:
JWT 基本使用
- 客户端收到服务器返回的
JWT,可以储存在Cookie里面, 也可以储存在localStorage - 然后 客户端每次与服务器通信,都要带上这个
JWT - 把
JWT保存在Cookie里面发送请求,这样不能跨域 - 更好的做法是放在
HTTP请求的头信息Authorization字段里面
实战:使用 JWT 登录认证
这里使用
ThinkPHP6整合JWT登录认证进行实战模拟安装 JWT 扩展
- 封装生成 JWT 和解密方法
- 用户登录后,生成 JWT 标识
- 中间件验证用户是否登录
- 在
middleware.php注册中间件
- 注册中间件后,在权限验证中间件中完善验证逻辑
边栏推荐
- ZABBIX agent2 monitors mongodb nodes, clusters and templates (official blog)
- Kotlin流程控制、循环
- Sqoop import and export operation
- ZABBIX agent2 monitors mongodb templates and configuration operations
- Third party payment interface design
- Differences between IPv6 and IPv4 three departments including the office of network information technology promote IPv6 scale deployment
- Iterator details in list... Interview pits
- ZABBIX 5.0 - LNMP environment compilation and installation
- A few years ago, I outsourced for four years. Qiu Zhao felt that life was like this
- JDBC exercise - query data encapsulated into object return & simple login demo
猜你喜欢

Pytoch uses torchnet Classerrormeter in meter

激动人心!2022开放原子全球开源峰会报名火热开启!

ActiveMQ installation and deployment simple configuration (personal test)

Tips and tricks of image segmentation summarized from 39 Kabul competitions

Volatile instruction rearrangement and why instruction rearrangement is prohibited

Knowledge representation (KR)

About cache exceptions: solutions for cache avalanche, breakdown, and penetration

How to connect the API interface of Taobao open platform (super detailed)

Kotlin变量

Iterator details in list... Interview pits
随机推荐
Oppo Xiaobu launched Obert, a large pre training model, and promoted to the top of kgclue
Detailed steps for upgrading window mysql5.5 to 5.7.36
Third party payment interface design
ZABBIX monitors mongodb (template and deployment operations)
Conversion du format de données GPS [facile à comprendre]
Pytoch monolayer bidirectional_ LSTM implements MNIST and fashionmnist data classification
Redis's memory elimination mechanism, read this article is enough.
在家庭智能照明中应用的测距传感芯片4530A
Take you hand in hand to develop a service monitoring component
NLP engineer learning summary and index
Neural network of PRML reading notes (1)
Full text search of MySQL
Transactions from January 6 to October 2022
Kotlin变量
GPS data format conversion [easy to understand]
Solve the problem of cache and database double write data consistency
[superhard core] is the core technology of redis
Get data from the database when using JMeter for database assertion
Distributed solution - distributed session consistency problem
Docker configures redis and redis clusters