Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/scala/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Scala 播放框架:如何为每个响应添加标题_Scala_Playframework_Http Headers_Token - Fatal编程技术网

Scala 播放框架:如何为每个响应添加标题

Scala 播放框架:如何为每个响应添加标题,scala,playframework,http-headers,token,Scala,Playframework,Http Headers,Token,在以下Controller中,Authenticated从请求头中提取令牌,并在且仅当令牌有效时调用给定操作(为清晰起见,代码已简化): authService.token(t)返回的令牌是JWT(JSON Web令牌),只能使用一次。。。因此,我需要在每次请求后返回一个新令牌。其想法是将新令牌放入响应头中。这就是说,有没有一种方法可以将标记添加到每个响应中,而不必在每个操作中调用withHeader。在Java中,它可能看起来像: public class YourActionComposit

在以下
Controller
中,
Authenticated
从请求头中提取令牌,并在且仅当令牌有效时调用给定操作(为清晰起见,代码已简化):


authService.token(t)返回的令牌是JWT(JSON Web令牌),只能使用一次。。。因此,我需要在每次请求后返回一个新令牌。其想法是将新令牌放入响应头中。这就是说,有没有一种方法可以将
标记添加到每个响应中,而不必在每个操作中调用
withHeader
。在Java中,它可能看起来像:

public class YourActionComposition extends Action<YourAnnotation> {

  @With(YourActionComposition.class)
  @Target({ ElementType.TYPE, ElementType.METHOD })
  @Retention(RetentionPolicy.RUNTIME)
  public @interface YourAnnotation {
  }

  public F.Promise<Result> call(Http.Context ctx) throws Throwable {
    Promise<Result> call = delegate.call(ctx);
    // Add something to your headers here
    return call;
  }
}
public类YourActionComposition扩展了Action{
@使用(YourActionComposition.class)
@目标({ElementType.TYPE,ElementType.METHOD})
@保留(RetentionPolicy.RUNTIME)
public@interface注释{
}
public F.Promise调用(Http.Context ctx)抛出可丢弃的{
承诺呼叫=委托呼叫(ctx);
//在这里为你的标题添加一些内容
回电;
}
}

我会使用ActionComposition。在Java中,它可能看起来像:

public class YourActionComposition extends Action<YourAnnotation> {

  @With(YourActionComposition.class)
  @Target({ ElementType.TYPE, ElementType.METHOD })
  @Retention(RetentionPolicy.RUNTIME)
  public @interface YourAnnotation {
  }

  public F.Promise<Result> call(Http.Context ctx) throws Throwable {
    Promise<Result> call = delegate.call(ctx);
    // Add something to your headers here
    return call;
  }
}
public类YourActionComposition扩展了Action{
@使用(YourActionComposition.class)
@目标({ElementType.TYPE,ElementType.METHOD})
@保留(RetentionPolicy.RUNTIME)
public@interface注释{
}
public F.Promise调用(Http.Context ctx)抛出可丢弃的{
承诺呼叫=委托呼叫(ctx);
//在这里为你的标题添加一些内容
回电;
}
}

只需在Global.scala添加WithFilters类中创建一个过滤器即可

import play.api.mvc._

object Global extends WithFilters(TokenFilter) {
  ...
}
这是一个日志的过滤器示例,您可以轻松地更改它以满足您的需要

val loggingFilter = Filter { (next, rh) =>
  val start = System.currentTimeMillis

  def logTime(result: PlainResult): Result = {
    val time = System.currentTimeMillis - start
    Logger.info(s"${rh.method} ${rh.uri} took ${time}ms and returned ${result.header.status}")
    result.withHeaders("Request-Time" -> time.toString)
  }

  next(rh) match {
    case plain: PlainResult => logTime(plain)
    case async: AsyncResult => async.transform(logTime)
  }
}

只需在Global.scala中创建一个过滤器并添加WithFilters类

import play.api.mvc._

object Global extends WithFilters(TokenFilter) {
  ...
}
这是一个日志的过滤器示例,您可以轻松地更改它以满足您的需要

val loggingFilter = Filter { (next, rh) =>
  val start = System.currentTimeMillis

  def logTime(result: PlainResult): Result = {
    val time = System.currentTimeMillis - start
    Logger.info(s"${rh.method} ${rh.uri} took ${time}ms and returned ${result.header.status}")
    result.withHeaders("Request-Time" -> time.toString)
  }

  next(rh) match {
    case plain: PlainResult => logTime(plain)
    case async: AsyncResult => async.transform(logTime)
  }
}