如何使用Play Silhouette实现无状态cookie身份验证?

时间:2016-03-14 11:49:20

标签: scala playframework silhouette

以下链接表示您可以将CookieAuthenticator用作无状态或有状态。

http://silhouette.mohiva.com/docs/authenticator

但我在下面的链接中找不到任何选择。

http://silhouette.mohiva.com/v3.0/docs/config-authenticators#cookieauthenticator

我复制了以下示例的实现。这是无国籍还是有状态的?如果有状态我如何实现无状态身份验证?

https://github.com/mohiva/play-silhouette-seed

1 个答案:

答案 0 :(得分:4)

一切都正确。

查看github上的CookieAuthenticator.scala来源:https://github.com/mohiva/play-silhouette/blob/master/silhouette/app/com/mohiva/play/silhouette/impl/authenticators/CookieAuthenticator.scala

/**
 * The service that handles the cookie authenticator.
 *
 * @param settings The cookie settings.
 * @param repository The repository to persist the authenticator. Set it to None to use a stateless approach.
 * @param fingerprintGenerator The fingerprint generator implementation.
 * @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 CookieAuthenticatorService(
  settings: CookieAuthenticatorSettings,
  repository: Option[AuthenticatorRepository[CookieAuthenticator]],
  fingerprintGenerator: FingerprintGenerator,
  idGenerator: IDGenerator,
  clock: Clock)(implicit val executionContext: ExecutionContext)
  extends AuthenticatorService[CookieAuthenticator]

所以你只需要在定义了存储库的情况下创建CookieAuthenticatorService

在您的示例中,您可以找到一个字符串

new CookieAuthenticatorService(config, None, fingerprintGenerator, idGenerator, clock)

此处的repository参数为None,因此CookieAuthenticator为无状态。