当前位置: 首页>>代码示例>>Scala>>正文


Scala SecuredErrorHandler类代码示例

本文整理汇总了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"))
    }
  }
} 
开发者ID:tm-sukehiro,项目名称:play-hands-on,代码行数:42,代码来源:RestApi.scala

示例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)))
} 
开发者ID:tm-sukehiro,项目名称:play-hands-on,代码行数:41,代码来源:ErrorHandler.scala

示例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
  }
} 
开发者ID:pequalsnp,项目名称:eve-isk-tracker,代码行数:24,代码来源:FallbackToAuthSecuredErrorHandler.scala


注:本文中的com.mohiva.play.silhouette.api.actions.SecuredErrorHandler类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。