[英]spray-http redirect to other subdomain
I need to redirect all traffic without a subdomain to the www
subdomain (eg, foo.com
-> www.foo.com
). 我需要将没有子域的所有流量重定向到www
子域(例如, foo.com
- > www.foo.com
)。
The code below is working, but i know redirecting code can be brittle and might bring security flaws. 下面的代码是有效的,但我知道重定向代码可能很脆弱,可能会带来安全漏洞。
Is this a proper way of achieving the above, or is there perhaps another set of directives i should use? 这是实现上述目标的正确方法,还是我应该使用另一套指令?
host("^(?!www).+".r) { h =>
unmatchedPath { p =>
schemeName { s =>
redirect(s"$s://www.$h$p", StatusCodes.MovedPermanently)
}
}
}
Edit: changed StatusCodes.SeeOther
to StatusCodes.MovedPermanently
编辑:更改了StatusCodes.SeeOther
到StatusCodes.MovedPermanently
MY first error was using the wrong status code ( StatusCodes.SeeOther
instead of StatusCodes.MovedPermanently
), the second was only using the path part of the url 我的第一个错误是使用了错误的状态代码( StatusCodes.SeeOther
而不是StatusCodes.MovedPermanently
),第二个错误只是使用了url的路径部分
The version below passing my set of tests (see further below): 下面的版本通过我的测试集(见下文):
host("^(?!www).+".r) { h =>
unmatchedPath { p =>
requestUri { s =>
val r = s.withHost(s"www.$h")
redirect(r, StatusCodes.MovedPermanently)
}
}
}
This passes the test below 这通过了下面的测试
"Redirect correctly" in {
"request" | "redirect" |
u("http://acme.com") ! u("http://www.acme.com") |
u("http://foo.com") ! u("http://www.foo.com") |
u("http://acme.com/search?a=1") ! u("http://www.acme.com/search?a=1") |
u("http://acme.com/search#abc") ! u("http://www.acme.com/search#abc") |
u("http://acme.com/service/") ! u("http://www.acme.com/service/") |
u("https://acme.com") ! u("https://www.acme.com") |> { (a :java.net.URI, b :java.net.URI) => {
Get(a.toString) ~> wholeRoute ~> check {
status === MovedPermanently
header("Location").get.value == b.toString
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.