本文整理汇总了Scala中play.filters.cors.CORSFilter类的典型用法代码示例。如果您正苦于以下问题:Scala CORSFilter类的具体用法?Scala CORSFilter怎么用?Scala CORSFilter使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了CORSFilter类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: CustomCORSFilter
//设置package包名称以及导入依赖的类
package filters
import javax.inject.{Inject, Singleton}
import akka.stream.Materializer
import play.api.http.HeaderNames
import play.api.mvc.{Filter, RequestHeader, Result}
import play.filters.cors.CORSFilter
import scala.concurrent.{ExecutionContext, Future}
@Singleton
class CustomCORSFilter @Inject()(corsFilter: CORSFilter)
(implicit override val mat: Materializer,
exec: ExecutionContext) extends Filter {
override def apply(nextFilter: RequestHeader => Future[Result])
(requestHeader: RequestHeader): Future[Result] = {
requestHeader.headers.get(HeaderNames.ORIGIN) match {
case (Some("null")) =>
val newHeaders = requestHeader.headers
.remove(HeaderNames.ORIGIN)
.add(HeaderNames.ORIGIN -> "http://file.url.local.null")
val mappedOrigin = requestHeader.copy(headers = newHeaders)
corsFilter(nextFilter)(mappedOrigin)
.map { result =>
result.withHeaders(HeaderNames.ACCESS_CONTROL_ALLOW_ORIGIN -> "null")
}
case _ => corsFilter(nextFilter)(requestHeader)
}
}
}
示例2: Filters
//设置package包名称以及导入依赖的类
import javax.inject._
import play.api._
import play.api.http.HttpFilters
import play.api.http.HttpFilters
import play.filters.gzip.GzipFilter
import play.filters.cors.CORSFilter
import play.api.mvc._
import filters.LoggingFilter
@Singleton
class Filters @Inject() (
env: Environment,
gzip: GzipFilter,
log: LoggingFilter,
cors: CORSFilter) extends HttpFilters {
override val filters = {
// Use the example filter if we're running development mode. If
// we're running in production or test mode then don't use any
// filters at all.
if (env.mode == Mode.Dev) Seq(gzip, log, cors) else Seq.empty
}
}
示例3: GriffinApiLoader
//设置package包名称以及导入依赖的类
import com.softwaremill.macwire._
import controllers.Assets
import filters.LoggingFilter
import play.api.ApplicationLoader.Context
import play.api._
import play.api.i18n._
import play.api.libs.ws.ahc.AhcWSComponents
import play.api.mvc.EssentialFilter
import play.api.routing.Router
import play.filters.cors.{CORSConfig, CORSFilter}
import router.Routes
import services.ServicesModule
class GriffinApiLoader extends ApplicationLoader {
def load(context: Context): Application = new GriffinComponents(context).application
}
class GriffinComponents(context: Context)
extends BuiltInComponentsFromContext(context)
with ServicesModule
with I18nComponents
with AhcWSComponents
{
// set up logger
LoggerConfigurator(context.environment.classLoader).foreach {
_.configure(context.environment)
}
lazy val assets: Assets = wire[Assets]
lazy val router: Router = {
// add the prefix string in local scope for the Routes constructor
val prefix: String = "/"
wire[Routes]
}
lazy val corsFilter = CORSFilter(CORSConfig.fromConfiguration(configuration))
lazy val loggingFilter = new LoggingFilter
lazy val filters = wire[Filters]
override lazy val httpFilters: Seq[EssentialFilter] =
Seq(loggingFilter,corsFilter)
}
示例4: Filters
//设置package包名称以及导入依赖的类
import javax.inject._
import play.api._
import play.api.http.HttpFilters
import play.api.mvc._
import filters.ExampleFilter
import play.filters.cors.CORSFilter
@Singleton
class Filters @Inject() (
env: Environment,
exampleFilter: ExampleFilter,
corsFilter: CORSFilter) extends HttpFilters {
override val filters = {
// Use the example filter if we're running development mode. If
// we're running in production or test mode then don't use any
// filters at all.
val seq = Seq(corsFilter)
if (env.mode == Mode.Dev) seq ++ Seq(exampleFilter)
else seq
}
}
示例5: RegExCORSFilter
//设置package包名称以及导入依赖的类
package com.github.dgilling.regexcors
import akka.stream.Materializer
import play.api.Logger
import play.api.http.{DefaultHttpErrorHandler, HttpErrorHandler}
import play.api.mvc.{Result, RequestHeader, Filter}
import play.core.j.JavaHttpErrorHandlerAdapter
import play.filters.cors.{CORSFilter, AbstractCORSPolicy, CORSConfig}
import scala.concurrent.Future
class RegExCORSFilter(
override protected val corsConfig: CORSConfig = CORSConfig(),
override protected val errorHandler: HttpErrorHandler = DefaultHttpErrorHandler,
private val pathPrefixes: Seq[String] = Seq("/"),
private val ignoredPathPrefixes: Seq[String] = Seq.empty[String])(override implicit val mat: Materializer)
extends CORSFilter {
// Java constructor
def this(corsConfig: CORSConfig, errorHandler: play.http.HttpErrorHandler, pathPrefixes: java.util.List[String])(mat: Materializer) = {
this(corsConfig, new JavaHttpErrorHandlerAdapter(errorHandler), Seq(pathPrefixes.toArray.asInstanceOf[Array[String]]: _*))(mat)
}
override protected val logger = Logger(classOf[CORSFilter])
override def apply(f: RequestHeader => Future[Result])(request: RequestHeader): Future[Result] = {
if (pathPrefixes.exists(request.path.startsWith) &&
!ignoredPathPrefixes.exists(request.path.startsWith)) {
filterRequest(f, request)
} else {
f(request)
}
}
}
object RegExCORSFilter {
val RequestTag = "CORS_REQUEST"
def apply(corsConfig: CORSConfig = CORSConfig(), errorHandler: HttpErrorHandler = DefaultHttpErrorHandler,
pathPrefixes: Seq[String] = Seq("/"))(implicit mat: Materializer) =
new CORSFilter(corsConfig, errorHandler, pathPrefixes)
}
示例6: Filters
//设置package包名称以及导入依赖的类
import javax.inject._
import play.api._
import play.api.http.HttpFilters
import play.filters.cors.CORSFilter
import play.api.mvc._
@Singleton
class Filters @Inject() (
env: Environment,
corsFilter: CORSFilter) extends HttpFilters {
override val filters = {
// Use the example filter if we're running development mode. If
// we're running in production or test mode then don't use any
// filters at all.
if (env.mode == Mode.Dev) Seq(corsFilter) else Seq.empty
}
}
示例7: Filters
//设置package包名称以及导入依赖的类
import javax.inject._
import play.api._
import play.api.http.HttpFilters
import play.filters.cors.CORSFilter
@Singleton
class Filters @Inject() (
env: Environment,
corsFilter: CORSFilter
) extends HttpFilters {
override val filters = {
// Use the example filter if we're running development mode. If
// we're running in production or test mode then don't use any
// filters at all.
if (env.mode == Mode.Dev) Seq(corsFilter) else Seq(corsFilter)
}
}
示例8: Filters
//设置package包名称以及导入依赖的类
import javax.inject.Inject
import play.api.http.DefaultHttpFilters
import play.filters.cors.CORSFilter
import play.filters.csrf.CSRFFilter
import play.filters.headers.SecurityHeadersFilter
import play.filters.hosts.AllowedHostsFilter
class Filters @Inject() (
csrfFilter: CSRFFilter,
allowedHostsFilter: AllowedHostsFilter,
securityHeadersFilter: SecurityHeadersFilter,
corsFilter: CORSFilter
) extends DefaultHttpFilters(
csrfFilter,
allowedHostsFilter,
securityHeadersFilter,
corsFilter
)
示例9: LoggingFilter
//设置package包名称以及导入依赖的类
package filters
import javax.inject.Inject
import akka.stream.Materializer
import play.api.Logger
import play.api.http.DefaultHttpFilters
import play.api.mvc.{Filter, RequestHeader, Result}
import play.filters.cors.CORSFilter
import scala.concurrent.{ExecutionContext, Future}
class LoggingFilter @Inject() (implicit val mat: Materializer, ec: ExecutionContext) extends Filter {
val X_REQUEST_ID = "X-Request-Id"
val X_RESPONSE_TIME = "X-Response-Time"
val accessLog = Logger("access")
def apply(nextFilter: RequestHeader => Future[Result])
(requestHeader: RequestHeader): Future[Result] = {
val startTime = System.currentTimeMillis
val requestId = requestHeader.headers.get(X_REQUEST_ID)
val linepre = s"req-${requestHeader.id}: ${requestHeader.method} ${requestHeader.uri}" +
requestId.map(" (" + X_REQUEST_ID + ": " + _ + ")").getOrElse("")
accessLog.info(linepre)
nextFilter(requestHeader).map { result =>
val endTime = System.currentTimeMillis
val requestTime = endTime - startTime
accessLog.info(s"${linepre} returned ${result.header.status} in ${requestTime}ms ")
val responseHeaders = Seq(
X_RESPONSE_TIME -> requestTime.toString
) ++ requestId.map(X_REQUEST_ID -> _)
result.withHeaders(responseHeaders:_*)
}
}
}
class Filters @Inject() (corsFilter: CORSFilter, loggingFilter: LoggingFilter)
extends DefaultHttpFilters(corsFilter,loggingFilter)
示例10: Filters
//设置package包名称以及导入依赖的类
import javax.inject._
import play.api._
import play.api.http.HttpFilters
import play.api.mvc._
import filters.ExampleFilter
import play.filters.cors.CORSFilter
@Singleton
class Filters @Inject() (
env: Environment,
exampleFilter: ExampleFilter,
corsFilter: CORSFilter) extends HttpFilters {
override val filters = {
// Use the example filter if we're running development mode. If
// we're running in production or test mode then don't use any
// filters at all.
// if (env.mode == Mode.Dev) Seq(exampleFilter) else Seq(corsFilter)
Seq.empty
}
}
示例11: CorsWithLoggingFilter
//设置package包名称以及导入依赖的类
package filters
import javax.inject.Inject
import akka.stream.Materializer
import play.api.http.HttpFilters
import play.api.Logger
import play.api.mvc._
import play.filters.cors.CORSFilter
import scala.concurrent.{ExecutionContext, Future}
class CorsWithLoggingFilter @javax.inject.Inject() (corsFilter: CORSFilter, loggingFilter: LoggingFilter) extends HttpFilters {
def filters = Seq(corsFilter, loggingFilter)
}
class LoggingFilter @Inject() (implicit val mat: Materializer, ec: ExecutionContext) extends Filter {
def apply(nextFilter: RequestHeader => Future[Result])
(requestHeader: RequestHeader): Future[Result] = {
val startTime = System.currentTimeMillis
val headerMap = requestHeader.headers.toMap
nextFilter(requestHeader).map { result =>
val endTime = System.currentTimeMillis
val requestTime = endTime - startTime
val line = Seq(
requestHeader.method,
s"${requestHeader.host}${requestHeader.uri}",
result.header.status,
s"${requestTime}ms",
headerMap.getOrElse("User-Agent", Nil).mkString(","),
headerMap.getOrElse("X-Forwarded-For", Nil).mkString(","),
headerMap.getOrElse(
"CF-Connecting-IP",
headerMap.getOrElse("True-Client-IP", Nil)
).mkString(",")
).mkString(" ")
Logger.info(line)
result.withHeaders("Request-Time" -> requestTime.toString)
}
}
}
示例12: ServerModule
//设置package包名称以及导入依赖的类
package uk.co.telegraph.utils.server
import akka.actor.ActorSystem
import com.google.inject.{AbstractModule, Provides}
import com.google.inject.Key.get
import com.google.inject.multibindings.Multibinder.newSetBinder
import play.api.mvc.EssentialFilter
import play.filters.cors.CORSFilter
import uk.co.telegraph.utils.server.filters.{AuthFilter, EventIdFilter}
import java.util.{Set => JSet}
import scala.language.postfixOps
import scala.collection.convert.WrapAsScala._
import uk.co.telegraph.utils.client.GenericClient
import uk.co.telegraph.utils.client.monitor.Monitor
class ServerModule extends AbstractModule {
override def configure(): Unit = {
val filterBinder = newSetBinder(binder(), get(classOf[EssentialFilter]))
filterBinder.addBinding().to(classOf[CORSFilter])
filterBinder.addBinding().to(classOf[EventIdFilter])
filterBinder.addBinding().to(classOf[AuthFilter])
}
@Provides
def monitorProvider(clientsSet:JSet[GenericClient])(implicit system:ActorSystem): Monitor = {
Monitor(clientsSet.toSeq)
}
}
示例13: Filters
//设置package包名称以及导入依赖的类
import com.google.inject.{Inject, Singleton}
import play.api._
import play.api.http.HttpFilters
import play.filters.cors.CORSFilter
@Singleton
class Filters @Inject()(
env: Environment,
corsFilter: CORSFilter) extends HttpFilters {
override val filters = {
// Use the example filter if we're running development mode. If
// we're running in production or test mode then don't use any
// filters at all.
if (env.mode == Mode.Dev) Seq(corsFilter) else Seq.empty
}
}
示例14: Filters
//设置package包名称以及导入依赖的类
import javax.inject._
import play.api._
import play.api.http.{ DefaultHttpFilters, HttpFilters }
import filters.ExampleFilter
import play.filters.cors.CORSFilter
@Singleton
class Filters @Inject() (
env: Environment,
exampleFilter: ExampleFilter,
corsFilter: CORSFilter
) extends HttpFilters {
override val filters = {
// Use the example filter if we're running development mode. If
// we're running in production or test mode then don't use any
// filters at all.
if (env.mode == Mode.Dev) Seq(exampleFilter, corsFilter) else Seq(corsFilter)
}
}
示例15: Filters
//设置package包名称以及导入依赖的类
import javax.inject._
import play.api._
import play.api.http.HttpFilters
import play.api.mvc._
import play.filters.cors.CORSFilter
@Singleton
class Filters @Inject() (
env: Environment,
corsFilter: CORSFilter) extends HttpFilters {
override val filters = {
// Use the example filter if we're running development mode. If
// we're running in production or test mode then don't use any
// filters at all.
if (env.mode == Mode.Dev) Seq(corsFilter) else Seq.empty
}
}