本文整理汇总了Scala中com.mohiva.play.silhouette.api.actions.SecuredErrorHandler类的典型用法代码示例。如果您正苦于以下问题:Scala SecuredErrorHandler类的具体用法?Scala SecuredErrorHandler怎么用?Scala SecuredErrorHandler使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SecuredErrorHandler类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: RestApi
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import scala.concurrent.Future
import com.mohiva.play.silhouette.api.Silhouette
import play.api.libs.json._
import play.api.mvc._
import play.api.i18n.{I18nSupport, Messages, MessagesApi}
import models.User
import User._
import com.mohiva.play.silhouette.api.actions.SecuredErrorHandler
import utils.auth.CookieEnv
class RestApi @Inject() (
val messagesApi: MessagesApi,
val silhouette: Silhouette[CookieEnv]) extends Controller with I18nSupport {
import silhouette._
def profile = SecuredAction.async { implicit request =>
val json = Json.toJson(request.identity.profileFor(request.authenticator.loginInfo).get)
val prunedJson = json.transform(
(__ \ 'loginInfo).json.prune andThen
(__ \ 'passordInfo).json.prune andThen
(__ \ 'oauth1Info).json.prune)
prunedJson.fold(
_ => Future.successful(InternalServerError(Json.obj("error" -> Messages("error.profileError")))),
js => Future.successful(Ok(js))
)
}
val errorHandler = new SecuredErrorHandler {
override def onNotAuthenticated(implicit request: RequestHeader) = {
Future.successful(Unauthorized(Json.obj("error" -> Messages("error.profileUnauth"))))
}
override def onNotAuthorized(implicit request: RequestHeader) = {
Future.successful(Forbidden("local.not.authorized"))
}
}
}
示例2: ErrorHandler
//设置package包名称以及导入依赖的类
package utils
import javax.inject.{Inject, Provider}
import com.mohiva.play.silhouette.api.actions.SecuredErrorHandler
import play.api.http.DefaultHttpErrorHandler
import play.api.i18n.{I18nSupport, Messages, MessagesApi}
import play.api.mvc.Results._
import play.api.mvc.{RequestHeader, Result}
import play.api.routing.Router
import play.api.{Configuration, OptionalSourceMapper}
import scala.concurrent.Future
import controllers.{WebJarAssets, routes}
class ErrorHandler @Inject() (
val messagesApi: MessagesApi,
env: play.api.Environment,
config: Configuration,
sourceMapper: OptionalSourceMapper,
router: javax.inject.Provider[Router],
p: Provider[WebJarAssets])
extends DefaultHttpErrorHandler(env, config, sourceMapper, router)
with SecuredErrorHandler with I18nSupport {
// https://www.playframework.com/documentation/2.5.x/Migration25#Handling-legacy-components
implicit lazy val webJarAssets = p.get()
override def onNotAuthenticated(implicit request: RequestHeader): Future[Result] =
Future.successful(Redirect(routes.Auth.signIn()))
override def onNotAuthorized(implicit request: RequestHeader): Future[Result] =
Future.successful(Redirect(routes.Auth.signIn()).flashing("error" -> Messages("error.accessDenied")))
override def onNotFound(request: RequestHeader, message: String): Future[Result] =
Future.successful(Ok(views.html.errors.notFound(request)))
override def onServerError(request:RequestHeader, exception:Throwable):Future[Result] =
Future.successful(Ok(views.html.errors.serverError(request, exception)))
}
示例3: FallbackToAuthSecuredErrorHandler
//设置package包名称以及导入依赖的类
package silhouette
import com.mohiva.play.silhouette.api.actions.SecuredErrorHandler
import controllers.routes
import play.api.mvc.RequestHeader
import play.api.mvc.Results._
import scala.concurrent.Future
class FallbackToAuthSecuredErrorHandler extends SecuredErrorHandler {
private def fallbackToLogin = {
Future.successful(Redirect(routes.HomeController.login()))
}
override def onNotAuthenticated(implicit request: RequestHeader) = {
println("Not authenticated" + request)
fallbackToLogin
}
override def onNotAuthorized(implicit request: RequestHeader) = {
println("Not authorized" + request)
fallbackToLogin
}
}