gpt4 book ai didi

kotlin - Ktor 中的表单认证

转载 作者:行者123 更新时间:2023-12-02 12:25:08 26 4
gpt4 key购买 nike

我是 KotlinKtor 的新手,想看看身份验证部分,所以我得到了以下代码。

路由“/”和“/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)
}
}
}
}
}

当我禁用下面的认证部分时,路由'/login'显示了所需的形式,这意味着错误很可能在这部分或调用它的方式?我猜。

            authentication {
formAuthentication { up: UserPasswordCredential ->
when {
up.password == "ppp" -> UserIdPrincipal(up.name)
else -> null
}
}
}

最佳答案

您得到的不仅仅是一个空白页面,您还会得到 401 (UNAUTHORIZED) 的 HTTP 状态代码。那是因为 formAuthentication 有四个参数,其中三个有默认值。你只实现了最后一个(validate,没有默认):

userParamName: String = "user",
passwordParamName: String = "password",
challenge: FormAuthChallenge = FormAuthChallenge.Unauthorized,
validate: (UserPasswordCredential) -> Principal?

每当您到达 /login 路由而没有正确的凭据时,您将获得 challenge 的默认值,即 FormAuthChallenge.Unauthorized,这是一个 401 响应。

您可以使用 FormAuthChallenge.Redirect,而不是使用默认的 challenge。一个需要两条路线的简短示例:

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

更新

如果以上方法效果不佳,请在 form 中清楚地定义 userid-parameterpassword-parameter执行 POST,如下所示:

        authentication {
formAuthentication("user", "pass",
challenge = FormAuthChallenge.Redirect({ _, _ -> "/login" })){
credential: UserPasswordCredential ->
when {
credential.password == "secret" -> UserIdPrincipal(credential.name)
else -> null
}
}
}

关于kotlin - Ktor 中的表单认证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46500030/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com