【问题标题】:How to implement stateless cookie authentication using Play Silhouette?如何使用 Play Silhouette 实现无状态 cookie 身份验证?
【发布时间】:2016-06-29 10:40:12
【问题描述】:

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

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

但我在以下链接中看不到任何可供选择的选项。

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

我已经复制了以下示例的实现。这是无状态的还是有状态的?如果它是有状态的,我该如何实现无状态身份验证?

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

【问题讨论】:

    标签: scala playframework silhouette


    【解决方案1】:

    都对。

    查看 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 是无状态的。

    【讨论】:

      猜你喜欢
      • 2020-09-25
      • 2014-07-09
      • 1970-01-01
      • 2018-10-23
      • 2014-01-02
      • 1970-01-01
      • 2017-05-06
      • 1970-01-01
      • 2010-09-26
      相关资源
      最近更新 更多