scala akka 不重定向
scala akka does not redirect
我有以下后端。如果转到“localhost:8080”,则通过从“/”重定向到“login”来加载登录页面。登录页面已加载。在提交登录表单时,将调用“执行登录”。但是,由于某种原因,没有重定向到“存储”页面。为什么?
P.S。如果手动请求页面“存储”,则会加载它。问题在于从“登录”页面到“存储”页面的重定向。可能与设置 cookie 有关,因为此命令也有 return 类型 Route.
Scala 版本:2.13.6,
Akka HTTP 版本:10.2.6
object Backend {
def main(args: Array[String]) = {
implicit val system = ActorSystem(Behaviors.empty, "lowlevel")
// needed for the future map/flatmap in the end
implicit val executionContext: ExecutionContext = system.executionContext
val topLevelRoute: Route =
concat(
pathSingleSlash {
redirect("login", StatusCodes.PermanentRedirect)
},
path("login") {
complete("my login page")
},
path("storage") {
cookie("token") { tokenCookie =>
println("you managed to login, token:" + tokenCookie.value + "ENDLINE")
complete("my storage page")
}
},
path("perform-login") {
formFields("Username", "Password") { (username, password) =>
var isAbleToLogin = database.isUserLoggedIn(username, password)
if (isAbleToLogin == true) {
setCookie(HttpCookie("token", value="ThisIsMyStrongAccessToken")) {
//TODO: debug why does not redirect to main page
redirect("storage", StatusCodes.PermanentRedirect)
}
}
else {
reject(ValidationRejection("bad credentials"))
}
}
},
path(Remaining) { pathRest =>
reject(ValidationRejection("topLevelRoute, unknown path:" + pathRest + "ENDLINE"))
}
)
val binding = Http().newServerAt("localhost", 8080).bind(topLevelRoute)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
binding
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
解决方法:
当我导航到“/”页面时,由于“文档/重定向”请求类型(如果分析网络),重定向到“/登录”页面。
但是,在从“/login”页面重定向到“/storage”页面的情况下,请求的类型为“xhr /redirect”,无法在服务器端完成,即我必须添加 $(location).attr('href', 'storage')
到我的jQuery 让它工作的脚本。
我有以下后端。如果转到“localhost:8080”,则通过从“/”重定向到“login”来加载登录页面。登录页面已加载。在提交登录表单时,将调用“执行登录”。但是,由于某种原因,没有重定向到“存储”页面。为什么?
P.S。如果手动请求页面“存储”,则会加载它。问题在于从“登录”页面到“存储”页面的重定向。可能与设置 cookie 有关,因为此命令也有 return 类型 Route.
Scala 版本:2.13.6, Akka HTTP 版本:10.2.6
object Backend {
def main(args: Array[String]) = {
implicit val system = ActorSystem(Behaviors.empty, "lowlevel")
// needed for the future map/flatmap in the end
implicit val executionContext: ExecutionContext = system.executionContext
val topLevelRoute: Route =
concat(
pathSingleSlash {
redirect("login", StatusCodes.PermanentRedirect)
},
path("login") {
complete("my login page")
},
path("storage") {
cookie("token") { tokenCookie =>
println("you managed to login, token:" + tokenCookie.value + "ENDLINE")
complete("my storage page")
}
},
path("perform-login") {
formFields("Username", "Password") { (username, password) =>
var isAbleToLogin = database.isUserLoggedIn(username, password)
if (isAbleToLogin == true) {
setCookie(HttpCookie("token", value="ThisIsMyStrongAccessToken")) {
//TODO: debug why does not redirect to main page
redirect("storage", StatusCodes.PermanentRedirect)
}
}
else {
reject(ValidationRejection("bad credentials"))
}
}
},
path(Remaining) { pathRest =>
reject(ValidationRejection("topLevelRoute, unknown path:" + pathRest + "ENDLINE"))
}
)
val binding = Http().newServerAt("localhost", 8080).bind(topLevelRoute)
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
StdIn.readLine() // let it run until user presses return
binding
.flatMap(_.unbind()) // trigger unbinding from the port
.onComplete(_ => system.terminate()) // and shutdown when done
}
解决方法:
当我导航到“/”页面时,由于“文档/重定向”请求类型(如果分析网络),重定向到“/登录”页面。
但是,在从“/login”页面重定向到“/storage”页面的情况下,请求的类型为“xhr /redirect”,无法在服务器端完成,即我必须添加 $(location).attr('href', 'storage')
到我的jQuery 让它工作的脚本。