【发布时间】:2016-07-30 22:15:59
【问题描述】:
我正在开发一个项目并使用Silhouette 身份验证框架。 对于来自浏览器 UI 的常规请求,我使用 CookieAuthenticator,对于 REST API 请求,我使用 JWTAuthenticator。 这是 Silhouette 源代码的一部分和文档,这让我觉得我不完全理解这件事的真正工作原理:
/**
* The service that handles the JWT authenticator.
*
* If the authenticator DAO is deactivated then a stateless approach will be used. But note
* that you will loose the possibility to invalidate a JWT.
*
* @param settings The authenticator settings.
* @param dao The DAO to store the authenticator. Set it to None to use a stateless approach.
* @param idGenerator The ID generator used to create the authenticator ID.
* @param clock The clock implementation.
* @param executionContext The execution context to handle the asynchronous operations.
*/
class JWTAuthenticatorService(
settings: JWTAuthenticatorSettings,
dao: Option[AuthenticatorDAO[JWTAuthenticator]],
idGenerator: IDGenerator,
clock: Clock)(implicit val executionContext: ExecutionContext)
extends AuthenticatorService[JWTAuthenticator]
with Logger {
请注意文档的这一部分
如果验证器 DAO 被停用,那么无状态方法将 使用。但请注意*,您将失去无效的可能性 一个智威汤逊。
所以它就像他们说的那样工作。当我将None 作为dao 参数的值传递时,即使我关闭了应用程序,生成的令牌也会保持有效。但是如果没有后备存储,这些令牌如何保持有效?当我再次启动应用程序并使用相同的令牌时,它会对用户进行身份验证。我不知道它是怎么做到的。你能解释一下吗?
【问题讨论】:
-
这很简单。您使用已知的盐和算法组合对令牌的内容进行编码。 JWT 令牌具有已知结构,使用 HMAC 或 RSA 编码。服务器可以以无状态方式解密令牌,只要它们知道编码密钥(HMAC 的秘密密钥)和 RSA 的密钥对。
-
是的,如果我想得更努力一点,我会猜想显然令牌应该包含有关链接帐户的信息。服务器可以轻松解密令牌的事实对我来说并不简单。您能否将您的评论作为答案,以便我接受?
标签: scala playframework silhouette