2016-03-14 28 views
5

以下鏈接指出您可以使用CookieAuthenticator作爲無狀態或有狀態。如何使用Play Silhouette實現無狀態cookie認證?

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

但我看不到任何選項做出選擇,而在下面的鏈接。

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

我抄了下面的例子執行。這是無國籍的還是有狀態的?如果它的狀態如何實現無狀態身份驗證?

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

回答

4

這一切都是正確的。

窺視CookieAuthenticator.scala源在github上: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是無狀態的。