我不熟悉Kotlin和Ktor试图查看身份验证部分,所以我得到了下面的代码。
路由"/“和"/bye”工作正常,但路由“登录”给定的空白页!
package blog
import kotlinx.html.*
import kotlinx.html.stream.* // for createHTML
import org.jetbrains.ktor.application.*
import org.jetbrains.ktor.auth.*
import org.jetbrains.ktor.features.*
import org.jetbrains.ktor.http.*
import org.jetbrains.ktor.response.*
import org.jetbrains.ktor.routing.*
import org.jetbrains.ktor.request.* // for request.uri
import org.jetbrains.ktor.html.*
import org.jetbrains.ktor.pipeline.*
import org.jetbrains.ktor.host.* // for embededServer
import org.jetbrains.ktor.netty.* // for Netty
fun main(args: Array<String>) {
embeddedServer(Netty, 8080, watchPaths = listOf("BlogAppKt"), module = Application::module).start()
}
fun Application.module() {
install(DefaultHeaders)
install(CallLogging)
intercept(ApplicationCallPipeline.Call) {
if (call.request.uri == "/hi")
call.respondText("Test String")
}
install(Routing) {
get("/") {
call.respondText("""Hello, world!<br><a href="/bye">Say bye?</a>""", ContentType.Text.Html)
}
get("/bye") {
call.respondText("""Good bye! <br><a href="/login">Login?</a> """, ContentType.Text.Html)
}
route("/login") {
authentication {
formAuthentication { up: UserPasswordCredential ->
when {
up.password == "ppp" -> UserIdPrincipal(up.name)
else -> null
}
}
}
handle {
val principal = call.authentication.principal<UserIdPrincipal>()
if (principal != null) {
call.respondText("Hello, ${principal.name}")
} else {
val html = createHTML().html {
body {
form(action = "/login", encType = FormEncType.applicationXWwwFormUrlEncoded, method = FormMethod.post) {
p {
+"user:"
textInput(name = "user") {
value = principal?.name ?: ""
}
}
p {
+"password:"
passwordInput(name = "pass")
}
p {
submitInput() { value = "Login" }
}
}
}
}
call.respondText(html, ContentType.Text.Html)
}
}
}
}
}当我禁用下面的身份验证部分时,路由‘/登录’显示了所需的表单,这意味着错误很可能发生在这个部分或调用它的方式上?我想是的。
authentication {
formAuthentication { up: UserPasswordCredential ->
when {
up.password == "ppp" -> UserIdPrincipal(up.name)
else -> null
}
}
}发布于 2017-11-05 10:34:23
您不仅获得了一个空白页,还得到了401 (UNAUTHORIZED)的HTTP代码。这是因为formAuthentication有四个参数,其中三个参数具有默认值。您只实现了最后一个(validate,没有默认情况):
userParamName: String = "user",
passwordParamName: String = "password",
challenge: FormAuthChallenge = FormAuthChallenge.Unauthorized,
validate: (UserPasswordCredential) -> Principal?无论何时,在没有正确凭证的情况下到达/login路由时,都会得到challenge的缺省值,即FormAuthChallenge.Unauthorized,这是一个401响应。
您可以使用一个challenge,而不是使用默认的FormAuthChallenge.Redirect。一个需要两条路由的简短示例:
get("/login") {
val html = """
<form action="/authenticate" enctype="..."
REST OF YOUR LOGIN FORM
</form>
"""
call.respondText(html, ContentType.Text.Html)
}
route("/authenticate") {
authentication {
formAuthentication(challenge = FormAuthChallenge.Redirect({ _, _ -> "/login" })) {
credential: UserPasswordCredential ->
when {
credential.password == "secret" -> UserIdPrincipal(credential.name)
else -> null
}
}
}
handle {
val principal = call.authentication.principal<UserIdPrincipal>()
val html = "Hello, ${principal?.name}"
call.respondText(html, ContentType.Text.Html)
}
}更新
如果上面的操作不太好,请将userid-parameter和password-parameter定义为在执行POST的form中清楚地定义,如下所示:
authentication {
formAuthentication("user", "pass",
challenge = FormAuthChallenge.Redirect({ _, _ -> "/login" })){
credential: UserPasswordCredential ->
when {
credential.password == "secret" -> UserIdPrincipal(credential.name)
else -> null
}
}
}https://stackoverflow.com/questions/46500030
复制相似问题