Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin Ktor中的表单认证_Kotlin_Ktor - Fatal编程技术网

Kotlin Ktor中的表单认证

Kotlin Ktor中的表单认证,kotlin,ktor,Kotlin,Ktor,我是新来的Kotlin和Ktor尝试查看身份验证部分,所以我得到了下面的代码 路由“/”和“/再见”工作正常,但路由“登录”给出空白页 package blog import kotlinx.html.* import kotlinx.html.stream.* // for createHTML import org.jetbrains.ktor.application.* import org.jetbrains.ktor.auth.* import org.jetbrains.kt

我是新来的
Kotlin
Ktor
尝试查看身份验证部分,所以我得到了下面的代码

路由“/”和“/再见”工作正常,但路由“登录”给出空白页

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)
                }
            }
        }
    }
}

您不仅得到一个空白页面,还得到一个HTTP状态代码
401(未经授权)
。这是因为
formAuthentication
有四个参数,其中三个是默认值。您只实现了最后一个(
验证
,无默认值):

当您到达
/login
路线时,如果没有正确的凭据,您将获得
质询
的默认值,即
FormAuthChallenge.Unauthorized
,这是一个
401
响应

您可以使用
格式的AuthChallenge.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)
    }
}

它被赋予
状态302
,并不断重新加载
\login
页面,而不管输入的密码是什么:(当我使用
进行身份验证(“用户”、“通过”时,它工作得很好,…
非常感谢。
userParamName: String = "user",
passwordParamName: String = "password",
challenge: FormAuthChallenge = FormAuthChallenge.Unauthorized,
validate: (UserPasswordCredential) -> Principal?
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)
    }
}
        authentication {
            formAuthentication("user", "pass",
                    challenge = FormAuthChallenge.Redirect({ _, _ -> "/login" })){
                credential: UserPasswordCredential ->
                when {
                credential.password == "secret" -> UserIdPrincipal(credential.name)
                else -> null
                }
            }
        }