将用户凭据存储在JWT中可以吗 [英] Is it ok to store user credentials in the JWT

查看:746
本文介绍了将用户凭据存储在JWT中可以吗的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以在JWT中存储用户凭据(用户名/密码)(因此sign它和verify随后生成的令牌)?

Is it ok to store user credentials (username / password) in the JWT (so sign it and verify the resulted token later)?

听说

否,在JWT中发送密码并不安全.这是因为 JWT声明仅经过编码,任何人都可以轻松地对其进行解码 看到他们.将任何敏感信息存储在计算机中并不安全. 返回用户的JWT

No, it is not secure to send a password in a JWT. This is because the JWT claims are simply encoded and can easily be decoded by anyone that sees them. It is not secure to store any sensitive information in a JWT that returned to a user

但是我不知道为什么JWT网站推荐然后将其用于身份验证:

but I don't know why does the JWT website recommends using it for authentication purposes then:

何时应使用JSON Web令牌?

When should you use JSON Web Tokens?

在某些情况下,JSON Web令牌很有用:

Here are some scenarios where JSON Web Tokens are useful:

身份验证:这是使用JWT的最常见方案.一次 用户登录后,随后的每个请求都将包含JWT, 允许用户访问的路由,服务和资源 该令牌允许的.单一登录是一项广泛的功能 现在使用JWT,是因为它的开销很小且具有 易于在不同域中使用

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

推荐答案

JWT是身份验证的结果.例如

The JWT is the result of the authentication. For example

  1. 用户将其凭据(例如用户名/密码)发送到身份验证服务.它可能是第三方内部的一个或一个,也可能是您自己的专用于身份验证的微服务.
  2. 该服务验证用户名密码.如果身份验证成功,则它将返回一个JWT,该JWT表示用户已通过身份验证,换句话说,他是声称自己是谁的用户.这个JWT可能包含没有敏感信息的有效负载(请不要在此处存储密码).
  3. 用户使用JWT向服务业务发送另一个请求.如果JWT未过期且未损坏(符号仍然有效),则服务可以信任其JWT.也许此任务将委派给授权服务.

JWT令牌内有什么?

What is inside the JWT token?

最简单的JWT包含有关标志的信息(由于我不是安全专家,因此我无法在此详细输入),可以在接收到JWT的请求时检查标志是否已损坏

Well, the simplest JWT contains information about the sign (I can't enter in much detail here because I'm not a security expert) that allows to check if the sign has been corrupted when a request with the JWT is received.

此信息可以进行验证和信任,因为它是经过数字签名的

This information can be verified and trusted because it is digitally signed

除此之外,JWT允许发送有效载荷.

Besides that, the JWT allows to send a payload.

更正式地说,JWT由以下人员组成:

More formally, the JWT is composed by:

  • 标题:正在使用的令牌+哈希算法的类型
  • 有效负载:声明是有关实体(通常是用户)和其他元数据的声明.
  • 签名:签名用于验证JWT的发件人是谁,并确保消息在此过程中未被更改.

例如,如果我以我的凭据用户名:password为gabriel:giussi发送请求到身份验证服务,它将检查这些凭据,如果可以,则可以创建以下JWT:

For example, if I send a request to a authentication service with my credentials username:password being gabriel:giussi, it will check these credentials and if they're OK it could create the following JWT:

然后,对于每个请求,我都将包含我的用户名和服务的已编码JWT

Then with every request I will then the encoded JWT that contains my username and the service will

  • 如果JWT标志有效,则执行授权(授权百利(Gabriel)做什么?).
  • 如果JWT已过期,请问我再次登录
  • 如果标志已损坏,则返回身份验证错误.

这篇关于将用户凭据存储在JWT中可以吗的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆