验证用户登录的一段代码是:
val loginRoute = path("login") {
post {
parameter('next ?) {
(next) =>
entity(as[FormData]) {
params =>
implicit ctx => {
var headers = List[HttpHeader]()
val user = params.fields.find(_._1 == "username").get._2
val pass = params.fields.find(_._1 == "password").get._2
val remember = params.fields.find(_._1 == "remember") match {
case Some(rem) => rem._2
case None => "off"
}
LdapAuthenticationProvider.authenticate(user, pass) match {
case false =>
sendResponse(StatusCodes.Forbidden.intValue, "Authentication Failed")
redirect("login", StatusCodes.Found)
case true =>
if ("on".equalsIgnoreCase(remember)) {
val hash = calculateHash(Map(Const.USERNAME -> user))
headers = List(HttpHeaders.`Set-Cookie`(HttpCookie(Const.COOKIE_REMEMBER_ME_KEY, user)),
HttpHeaders.`Set-Cookie`(HttpCookie(Const.COOKIE_AUTH_HASH, hash)))
}
val url = next match {
case Some(path) => path
case None => "/"
}
complete {
HttpResponse(
status = StatusCodes.Found,
headers = Location(url) :: headers,
entity = StatusCodes.Found.htmlTemplate match {
case "" ⇒ HttpEntity.Empty
case template ⇒ HttpEntity(`text/html`, template format url)
}
)
}
}
}
}
}
}
}
但是我无法设置请求内容类型。我不应该自动为我做这个吗?我的要求是
Content-Type: application/x-www-form-urlencoded
我在POST请求标头中设置了哪个。 但是我仍然得到:
There was a problem with the requests Content-Type:
Expected 'application/json'
请帮帮我吧!提前谢谢!
答案 0 :(得分:0)
Jrudolf建议的问题是我的JSON支持特性试图反序列化数据。标题被忽略了。事实上,它甚至没有到达那里。解决方案就像扩展现有的编组人一样简单。
class LoginServlet(context: akka.actor.ActorRefFactory) extends BaseServlet with FormDataUnmarshallers {
}