akka-http:如何 consume/hide 提取值?
akka-http: How can I consume/hide an extracted value?
我正在尝试编写拒绝 non-ajax 请求的指令。下面的代码显然不起作用:
import akka.http.scaladsl.model.HttpHeader
import akka.http.scaladsl.server.Directive0
import akka.http.scaladsl.server.directives.BasicDirectives
import akka.http.scaladsl.server.directives.HeaderDirectives
import akka.http.scaladsl.server.directives.RouteDirectives
trait AjaxDirectives extends BasicDirectives with HeaderDirectives with RouteDirectives {
private val valid = "XMLHttpRequest"
def ajax(): Directive0 = {
headerValueByName("X-Requested-With") { header ⇒
if (header == valid) {
pass
} else {
reject
}
}
}
}
(这里有2个问题:pass
是Directive0
&headerValueByName
是Directive1
,headerValueByName
是Directive1
&ajax
是 Directive0
。所以它不编译)
我的问题是:我能以某种方式获得局部范围的提取吗? 就像,header
不会转义 ajax
.
我知道我可以访问拉出 header 的请求,而无需使用 headerValue*
,所以请不要回答。
阅读SecurityDirectives.authorizeAsync
给了我答案:
import akka.http.scaladsl.model.HttpHeader
import akka.http.scaladsl.server.Directive0
import akka.http.scaladsl.server.directives.BasicDirectives
import akka.http.scaladsl.server.directives.HeaderDirectives
import akka.http.scaladsl.server.directives.RouteDirectives
trait AjaxDirectives extends BasicDirectives with HeaderDirectives with RouteDirectives {
private val headerName = "X-Requested-With"
private val valid = "XMLHttpRequest"
def ajax(): Directive0 = {
headerValueByName(headerName).flatMap { header ⇒
if (header == valid) {
pass
} else {
reject
}
}
}
}
我正在尝试编写拒绝 non-ajax 请求的指令。下面的代码显然不起作用:
import akka.http.scaladsl.model.HttpHeader
import akka.http.scaladsl.server.Directive0
import akka.http.scaladsl.server.directives.BasicDirectives
import akka.http.scaladsl.server.directives.HeaderDirectives
import akka.http.scaladsl.server.directives.RouteDirectives
trait AjaxDirectives extends BasicDirectives with HeaderDirectives with RouteDirectives {
private val valid = "XMLHttpRequest"
def ajax(): Directive0 = {
headerValueByName("X-Requested-With") { header ⇒
if (header == valid) {
pass
} else {
reject
}
}
}
}
(这里有2个问题:pass
是Directive0
&headerValueByName
是Directive1
,headerValueByName
是Directive1
&ajax
是 Directive0
。所以它不编译)
我的问题是:我能以某种方式获得局部范围的提取吗? 就像,header
不会转义 ajax
.
我知道我可以访问拉出 header 的请求,而无需使用 headerValue*
,所以请不要回答。
阅读SecurityDirectives.authorizeAsync
给了我答案:
import akka.http.scaladsl.model.HttpHeader
import akka.http.scaladsl.server.Directive0
import akka.http.scaladsl.server.directives.BasicDirectives
import akka.http.scaladsl.server.directives.HeaderDirectives
import akka.http.scaladsl.server.directives.RouteDirectives
trait AjaxDirectives extends BasicDirectives with HeaderDirectives with RouteDirectives {
private val headerName = "X-Requested-With"
private val valid = "XMLHttpRequest"
def ajax(): Directive0 = {
headerValueByName(headerName).flatMap { header ⇒
if (header == valid) {
pass
} else {
reject
}
}
}
}