问题
I am trying to write a sample app with Play 2.5 using Silhouette 4.0 for user/password authentication.
Source code available at : https://github.com/pariksheet/dribble
I am stuck at setting requestProvider in Environment object
Environment[JWTEnv](
identityService,
authenticatorService,
Seq(),
eventBus
)
As I am not able to figure out the solution, I am injecting Credential Provider in my all controller classes.
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
...
...
}
}
My Module.scala as below -
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)
}
Can you please help me over here?
Thanks Pari
回答1:
You mix two different things here. The credentials provider is not a request provider. A request provider is a special type of provider, which can be directly hooked into the request. A detailed description can be found in the documentation. Silhouette provides a Basic Authentication provider which is an implementation of a request provider.
The credentials provider should directly injected into your controller as implemented in the seed template.
回答2:
The request providers are set in your Module.scala, but they are set to an empty Sequence:
@Provides
def provideEnvironment(
identityService: IdentityService[User],
authenticatorService: AuthenticatorService[JWTAuthenticator],
eventBus: EventBus): Environment[JWTEnv] = {
Environment[JWTEnv](
identityService,
authenticatorService,
Seq(), // Here the request providers are set
eventBus
)
}
Ps: Keep in mind, that the foreach
will always "return none", since its type is unit
来源:https://stackoverflow.com/questions/38949447/play-2-5-with-silhouette-4-0-credential-provider