会话身份验证是否比基于令牌的身份验证更安全?
Is session authentication more secure than token-based authentication?
我一直在努力了解会话和令牌身份验证之间的真正区别。
到目前为止我收集到的内容:
在令牌认证中,服务器端不存储任何内容。这意味着,实际令牌包括密码和用户名,以及其他可能的信息。而服务器只是解密token,然后检查用户名和密码是否正确。我说得对吗??如果token包含密码和用户名,那么token怎么每次都不一样呢?
在基于会话的身份验证中,会话令牌只是一个随机的(时间上唯一的)id,映射到服务器端的用户。因此,当服务器收到 session_id(例如在 cookie 中)时,它会检查它是否映射到任何用户,如果是,则用户通过身份验证。所以 session_id 不包含任何可以解密的用户相关信息?
在session认证中,服务器会不加密(除非使用https)返回用户相关信息(非密码)。
在token认证中,服务器不会直接返回用户信息,而是返回包含用户信息的token,解密后?
我感觉我还没有真正理解令牌和会话身份验证的工作原理。上面的陈述肯定有问题。
但是,让我们假设这些陈述是正确的。那么基于会话的身份验证不是更安全吗?因为在基于会话的身份验证中,您不会泄露用户密码(例如在浏览器中)。由于它只是一个随机 ID,因此无法从中获取信息。但是Token认证就不是这样了。由于令牌认证包含密码,如果有人设法解密它,他将得到你的密码。那么会话身份验证实际上是否比令牌身份验证更安全,因为它不会泄露密码和用户名信息?
密码等敏感信息或社会安全号码等项目不应存储在令牌中。
令牌签名的一个典型例子是这个
function createToken(user) {
return jwt.sign(_.omit(user, 'password'), config.secret, { expiresIn: 60*60*5 });
}
在这里,我们使用用户的详细信息创建一个签名令牌,但我们省略了密码。
我在这个帖子中提供了非常详细的信息How is JSON Web Token more secure than cookie/session?
看看吧。我希望这个信息帮助!
您的问题没有绝对答案YES/NO。例如,会话 cookie 容易受到 CSRF 攻击,令牌可能会被 XSS 注入窃取。如果您不使用 HTTPS,这两种机制也容易受到 ManInTheMiddle 的攻击。因此,通常每个解决方案都需要额外的安全措施。取决于您的用例。
我猜你是在谈论像 JWT 这样的令牌机制,它是独立的并且可以防止更改,因为你说
In token authentication, nothing is stored in the server side.
但是你混淆了一些概念。我将尝试使用 JWT 令牌作为参考来回答您的其他问题。如果不是,大多数概念也可以应用于不透明令牌
In token authentication, nothing is stored in the server side. What this means is, that the actual token includes the password and username, as well as other possible information. And the server just decrypts the token, and then checks whether the username and password are correct. Am I right about this??
令牌由服务器(而非客户端)颁发,要求用户出示其凭据并使用服务器私钥进行数字签名。该令牌包括 sub
声明中委托人的标识符和其他感兴趣的字段,如到期时间或发行者。从来没有密码
当客户端发送token进行身份验证时,服务端验证签名确定真伪并没有被篡改
If the token includes the password and username, then how can the token still be different everytime?
令牌不包含密码。由于某些变体声明(例如到期时间 exp
或在 iat
发行),令牌将有所不同。计算出的签名也会不同
So the session_id does not contain any user related information, that could be decrypted?
是的,这是一个随机序列。与用户服务器的关系存储在服务器
In token authentication, the server will not send back direct user information, but just the token, which contains the user information, once decrypted?
JWT token包含一些用户信息,但是没有加密,是签名的。如果需要隐藏payload,JWT也允许使用JWE加密
But, let's play along that the statements would be correct. Then wouldn't session-based authentication be more secure? Because in session based authentication, you do not reveal user password (in browser for example). Since it's just a random id, one cannot get information from it. But this is not the case with Token authentication. Since token authentication contains the password, if someone manages to decrypt it, he will get your password. So isn't the session authentication actually more safe than the token authentication, as it doesn't reveal password nor username information?
基本方法是错误的。 密码从不包含在令牌中。如果您不想泄露用户数据,您可以使用不透明令牌或 JWE 加密与 JWT。正确的解决方案取决于您的用例。看我的第一段
我一直在努力了解会话和令牌身份验证之间的真正区别。
到目前为止我收集到的内容:
在令牌认证中,服务器端不存储任何内容。这意味着,实际令牌包括密码和用户名,以及其他可能的信息。而服务器只是解密token,然后检查用户名和密码是否正确。我说得对吗??如果token包含密码和用户名,那么token怎么每次都不一样呢?
在基于会话的身份验证中,会话令牌只是一个随机的(时间上唯一的)id,映射到服务器端的用户。因此,当服务器收到 session_id(例如在 cookie 中)时,它会检查它是否映射到任何用户,如果是,则用户通过身份验证。所以 session_id 不包含任何可以解密的用户相关信息?
在session认证中,服务器会不加密(除非使用https)返回用户相关信息(非密码)。
在token认证中,服务器不会直接返回用户信息,而是返回包含用户信息的token,解密后?
我感觉我还没有真正理解令牌和会话身份验证的工作原理。上面的陈述肯定有问题。
但是,让我们假设这些陈述是正确的。那么基于会话的身份验证不是更安全吗?因为在基于会话的身份验证中,您不会泄露用户密码(例如在浏览器中)。由于它只是一个随机 ID,因此无法从中获取信息。但是Token认证就不是这样了。由于令牌认证包含密码,如果有人设法解密它,他将得到你的密码。那么会话身份验证实际上是否比令牌身份验证更安全,因为它不会泄露密码和用户名信息?
密码等敏感信息或社会安全号码等项目不应存储在令牌中。
令牌签名的一个典型例子是这个
function createToken(user) { return jwt.sign(_.omit(user, 'password'), config.secret, { expiresIn: 60*60*5 }); }
在这里,我们使用用户的详细信息创建一个签名令牌,但我们省略了密码。
我在这个帖子中提供了非常详细的信息How is JSON Web Token more secure than cookie/session?
看看吧。我希望这个信息帮助!
您的问题没有绝对答案YES/NO。例如,会话 cookie 容易受到 CSRF 攻击,令牌可能会被 XSS 注入窃取。如果您不使用 HTTPS,这两种机制也容易受到 ManInTheMiddle 的攻击。因此,通常每个解决方案都需要额外的安全措施。取决于您的用例。
我猜你是在谈论像 JWT 这样的令牌机制,它是独立的并且可以防止更改,因为你说
In token authentication, nothing is stored in the server side.
但是你混淆了一些概念。我将尝试使用 JWT 令牌作为参考来回答您的其他问题。如果不是,大多数概念也可以应用于不透明令牌
In token authentication, nothing is stored in the server side. What this means is, that the actual token includes the password and username, as well as other possible information. And the server just decrypts the token, and then checks whether the username and password are correct. Am I right about this??
令牌由服务器(而非客户端)颁发,要求用户出示其凭据并使用服务器私钥进行数字签名。该令牌包括 sub
声明中委托人的标识符和其他感兴趣的字段,如到期时间或发行者。从来没有密码
当客户端发送token进行身份验证时,服务端验证签名确定真伪并没有被篡改
If the token includes the password and username, then how can the token still be different everytime?
令牌不包含密码。由于某些变体声明(例如到期时间 exp
或在 iat
发行),令牌将有所不同。计算出的签名也会不同
So the session_id does not contain any user related information, that could be decrypted?
是的,这是一个随机序列。与用户服务器的关系存储在服务器
In token authentication, the server will not send back direct user information, but just the token, which contains the user information, once decrypted?
JWT token包含一些用户信息,但是没有加密,是签名的。如果需要隐藏payload,JWT也允许使用JWE加密
But, let's play along that the statements would be correct. Then wouldn't session-based authentication be more secure? Because in session based authentication, you do not reveal user password (in browser for example). Since it's just a random id, one cannot get information from it. But this is not the case with Token authentication. Since token authentication contains the password, if someone manages to decrypt it, he will get your password. So isn't the session authentication actually more safe than the token authentication, as it doesn't reveal password nor username information?
基本方法是错误的。 密码从不包含在令牌中。如果您不想泄露用户数据,您可以使用不透明令牌或 JWE 加密与 JWT。正确的解决方案取决于您的用例。看我的第一段