使用Silhouette 4.0播放2.5 - 凭据提供者

时间:2016-08-15 04:47:13

标签: scala playframework silhouette

我正在尝试使用Silhouette 4.0编写一个使用Play 2.5的示例应用程序进行用户/密码验证。

源代码:https://github.com/pariksheet/dribble

我被困在环境对象

中设置requestProvider
Environment[JWTEnv](
  identityService,
  authenticatorService,
  Seq(),
  eventBus
)

由于我无法找到解决方案,因此我在所有控制器类中注入了Credential Provider。

    class MyController @Inject() (val messagesApi: MessagesApi,val silhouette: Silhouette[JWTEnv],
  authInfoRepository: AuthInfoRepository,
  credentialsProvider: CredentialsProvider)
    extends Controller {
          def authenticate = Action.async(BodyParsers.parse.json) { implicit request =>
        credentialsProvider.authenticate(Credentials(data.email, data.password)).flatMap { loginInfo => ..}    
         //   silhouette.env.requestProviders.foreach { x => println("====" + x.id) }  --- I am hoping to get credential provider from silhouette object

        ...
        ...
        }
    }

我的Module.scala如下 -

package module

import com.google.inject.AbstractModule
import com.google.inject.Provides
import com.mohiva.play.silhouette.api.Env
import com.mohiva.play.silhouette.api.Environment
import com.mohiva.play.silhouette.api.EventBus
import com.mohiva.play.silhouette.api.Silhouette
import com.mohiva.play.silhouette.api.SilhouetteProvider
import com.mohiva.play.silhouette.api.crypto.Base64AuthenticatorEncoder
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.services.AuthenticatorService
import com.mohiva.play.silhouette.api.services.AvatarService
import com.mohiva.play.silhouette.api.services.IdentityService
import com.mohiva.play.silhouette.api.util.CacheLayer
import com.mohiva.play.silhouette.api.util.Clock
import com.mohiva.play.silhouette.api.util.FingerprintGenerator
import com.mohiva.play.silhouette.api.util.HTTPLayer
import com.mohiva.play.silhouette.api.util.IDGenerator
import com.mohiva.play.silhouette.api.util.PasswordHasher
import com.mohiva.play.silhouette.api.util.PasswordHasherRegistry
import com.mohiva.play.silhouette.api.util.PasswordInfo
import com.mohiva.play.silhouette.api.util.PlayHTTPLayer
import com.mohiva.play.silhouette.impl.authenticators.JWTAuthenticator
import com.mohiva.play.silhouette.impl.authenticators.JWTAuthenticatorService
import com.mohiva.play.silhouette.impl.authenticators.JWTAuthenticatorSettings
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import com.mohiva.play.silhouette.impl.services.GravatarService
import com.mohiva.play.silhouette.impl.util.DefaultFingerprintGenerator
import com.mohiva.play.silhouette.impl.util.PlayCacheLayer
import com.mohiva.play.silhouette.impl.util.SecureRandomIDGenerator
import com.mohiva.play.silhouette.password.BCryptPasswordHasher
import com.mohiva.play.silhouette.persistence.daos.DelegableAuthInfoDAO
import com.mohiva.play.silhouette.persistence.repositories.DelegableAuthInfoRepository

import daos.MongoUserDao
import daos.MongoUserTokenDao
import daos.PasswordInfoDao
import daos.UserDao
import daos.UserTokenDao
import models.User
import net.codingwell.scalaguice.ScalaModule
import play.api.Configuration
import play.api.libs.concurrent.Execution.Implicits.defaultContext
import play.api.libs.ws.WSClient
import services.UserService

trait JWTEnv extends Env {
  type I = User
  type A = JWTAuthenticator
}

class Module extends AbstractModule with ScalaModule {

  def configure() {
    bind[IdentityService[User]].to[UserService]
    bind[UserDao].to[MongoUserDao]
    bind[UserTokenDao].to[MongoUserTokenDao]
    bind[DelegableAuthInfoDAO[PasswordInfo]].to[PasswordInfoDao]
    bind[IDGenerator].toInstance(new SecureRandomIDGenerator())
    bind[PasswordHasher].toInstance(new BCryptPasswordHasher)
    bind[FingerprintGenerator].toInstance(new DefaultFingerprintGenerator(false))
    bind[EventBus].toInstance(EventBus())
    bind[Clock].toInstance(Clock())
    bind[CacheLayer].to[PlayCacheLayer]
    bind[Silhouette[JWTEnv]].to[SilhouetteProvider[JWTEnv]]
  }

  @Provides
  def provideHTTPLayer(client: WSClient): HTTPLayer = new PlayHTTPLayer(client)

  @Provides
  def provideEnvironment(
    identityService: IdentityService[User],
    authenticatorService: AuthenticatorService[JWTAuthenticator],
    eventBus: EventBus): Environment[JWTEnv] = {

    Environment[JWTEnv](
      identityService,
      authenticatorService,
      Seq(),
      eventBus
    )
  }

  @Provides
  def provideAuthenticatorService(
    fingerprintGenerator: FingerprintGenerator,
    idGenerator: IDGenerator,
    configuration: Configuration,
    clock: Clock): AuthenticatorService[JWTAuthenticator] = {
    val settings = JWTAuthenticatorSettings(
      sharedSecret = configuration.getString("application.secret").getOrElse("PARI"))


    new JWTAuthenticatorService(
      settings = settings,
      repository = None,
      authenticatorEncoder = new Base64AuthenticatorEncoder,
      idGenerator = idGenerator,
      clock = Clock())
  }

  @Provides
  def provideAuthInfoRepository(
    passwordInfoDAO: DelegableAuthInfoDAO[PasswordInfo]): AuthInfoRepository = {

    new DelegableAuthInfoRepository(passwordInfoDAO)
  }

  @Provides
  def providePasswordHasherRegistry(passwordHasher: PasswordHasher): PasswordHasherRegistry = {
    new PasswordHasherRegistry(passwordHasher)
  }

  @Provides
  def provideCredentialsProvider(
    authInfoRepository: AuthInfoRepository,
    passwordHasherRegistry: PasswordHasherRegistry): CredentialsProvider = {

    new CredentialsProvider(authInfoRepository, passwordHasherRegistry)
  }


  @Provides
  def provideAvatarService(httpLayer: HTTPLayer): AvatarService = new GravatarService(httpLayer)

}

你能帮帮我吗?

由于 彩

1 个答案:

答案 0 :(得分:0)

你在这里混合了两件不同的东西。凭证提供程序不是请求提供程序。请求提供程序是一种特殊类型的提供程序,可以直接连接到请求中。详细说明可在documentation中找到。 Silhouette提供Basic Authentication provider,它是请求提供者的实现。

凭据提供程序应直接注入您的控制器,如seed template中所实现的那样。