Scala 从Play框架向WS-client中的WSRequest添加头
我有一个需要通过头进行身份验证的服务。我有一个为我生成值的现有java客户机。我正在尝试使用WSRequestHeaderFilter将头应用于ws请求 当我设置断点时,代码似乎运行良好,并且可以看到应用了头。但是在我的测试服务器(使用PlaySird路由器)中,似乎没有应用头 如何使用筛选器或此类方法使所需的标题显示在请求中 请参阅下面的代码: 过滤器:Scala 从Play框架向WS-client中的WSRequest添加头,scala,playframework,webservice-client,Scala,Playframework,Webservice Client,我有一个需要通过头进行身份验证的服务。我有一个为我生成值的现有java客户机。我正在尝试使用WSRequestHeaderFilter将头应用于ws请求 当我设置断点时,代码似乎运行良好,并且可以看到应用了头。但是在我的测试服务器(使用PlaySird路由器)中,似乎没有应用头 如何使用筛选器或此类方法使所需的标题显示在请求中 请参阅下面的代码: 过滤器: class AuthenticatingFilter @Inject() (authHeaderGenerator: AuthHeaderG
class AuthenticatingFilter @Inject() (authHeaderGenerator: AuthHeaderGenerator) extends WSRequestFilter {
def apply(executor: WSRequestExecutor): WSRequestExecutor = {
new WSRequestExecutor {
override def execute(request: WSRequest): Future[WSResponse] = {
val headers = authHeaderGenerator.generateRequestHeaders(request.method, request.uri.toString, null).asScala.toList
executor.execute(request.withHeaders(headers:_*))
}
}
}
}
在客户端中的用法:
//code omitted for brevity
def getStuff() = ws.url(s"${baseUrl}/authenticatedEndpoint").withRequestFilter(filter).get()
测试:
谢谢
本事实证明,这是一个正在起作用的错误。我对此进行了修补,更改被合并到主分支()中,因此如果您有类似的问题,应该通过升级到play 2.5.3(如果可用)来修复
// code omitted for brevity
Server.withRouter() {
case GET(p"/authenticatedEndpoint") => Action {
request =>
request.headers.get(authHeader) match {
case Some(authHeaderValue) => Results.Ok(expectedResult)
case _ => Results.Forbidden
}
}
} {
implicit port =>
implicit val materializer = Play.current.materializer
WsTestClient.withClient {
client =>
val authenticatedClient: AuthenticatedClient = new AuthenticatedClient(client,filter)
val result: String = Await.result(authenticatedClient.getStuff(), Duration.Inf)
result must beEqualTo(expectedResult)
}
}
}