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)
}
}