本文整理汇总了Scala中com.mohiva.play.silhouette.impl.exceptions.IdentityNotFoundException类的典型用法代码示例。如果您正苦于以下问题:Scala IdentityNotFoundException类的具体用法?Scala IdentityNotFoundException怎么用?Scala IdentityNotFoundException使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了IdentityNotFoundException类的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: authenticate
//设置package包名称以及导入依赖的类
package controllers.security.rest
import scala.concurrent.Future
import play.api.mvc._
import play.api.libs.json._
import play.api.libs.concurrent.Execution.Implicits._
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.api.exceptions.ConfigurationException
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import com.mohiva.play.silhouette.impl.authenticators.JWTAuthenticator
import com.mohiva.play.silhouette.impl.exceptions.IdentityNotFoundException
import com.mohiva.play.silhouette.api.util.Credentials
import modules.cake.HeaderEnvironmentModule
import security.models._
import models.users.User
def authenticate = Action.async(parse.json[Credentials]) { implicit request =>
(env.providers.get(CredentialsProvider.ID) match {
case Some(p: CredentialsProvider) => p.authenticate(request.body)
case _ => Future.failed(new ConfigurationException(s"Cannot find credentials provider"))
}).flatMap { loginInfo =>
userService.retrieve(loginInfo).flatMap {
case Some(user) => env.authenticatorService.create(user.loginInfo).flatMap { authenticator =>
env.eventBus.publish(LoginEvent(user, request, request2lang))
env.authenticatorService.init(authenticator).flatMap { token =>
env.authenticatorService.embed(token, Future.successful {
Ok(Json.toJson(Token(token = token, expiresOn = authenticator.expirationDate)))
})
}
}
case None =>
Future.failed(new IdentityNotFoundException("Couldn't find user"))
}
}.recoverWith(exceptionHandler)
}
}
object RestCredentialsAuthController extends RestCredentialsAuthController
示例2: Authentication
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.api.exceptions.ProviderException
import com.mohiva.play.silhouette.api.util.Credentials
import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator
import com.mohiva.play.silhouette.impl.exceptions.IdentityNotFoundException
import com.mohiva.play.silhouette.impl.providers._
import forms.SignInForm
import models.User
import models.daos.UserDAO
import play.api.i18n.{Messages, MessagesApi}
import play.api.libs.concurrent.Execution.Implicits._
import play.api.mvc.Action
import scala.concurrent.Future
class Authentication @Inject()(val messagesApi: MessagesApi,
val env: Environment[User, SessionAuthenticator],
userDAO: UserDAO,
credentialsProvider: CredentialsProvider)
extends Silhouette[User, SessionAuthenticator] {
def authenticate = Action.async { implicit request =>
SignInForm.form.bindFromRequest.fold(
form => Future.successful(BadRequest(views.html.signIn(form))),
data => {
val credentials = Credentials(data.email, data.password)
credentialsProvider.authenticate(credentials).flatMap { loginInfo =>
val result = Redirect(routes.Application.dashboard())
userDAO.retrieve(loginInfo).flatMap {
case Some(user) =>
env.authenticatorService.create(loginInfo).flatMap { authenticator =>
env.eventBus.publish(LoginEvent(user, request, request2Messages))
env.authenticatorService.init(authenticator).flatMap { v =>
env.authenticatorService.embed(v, result)
}
}
case None => Future.failed(new IdentityNotFoundException(Messages("user.not_found")))
}
}.recover {
case _: ProviderException =>
Redirect(routes.Application.signIn()).flashing("error" -> Messages("invalid.credentials"))
}
}
)
}
}