这是我的代码:
package com.example.ktordemo
import org.jetbrains.ktor.application.Application
import org.jetbrains.ktor.application.install
import org.jetbrains.ktor.application.log
import org.jetbrains.ktor.auth.UserHashedTableAuth
import org.jetbrains.ktor.features.CallLogging
import org.jetbrains.ktor.features.ConditionalHeaders
import org.jetbrains.ktor.features.DefaultHeaders
import org.jetbrains.ktor.features.PartialContentSupport
import org.jetbrains.ktor.locations.*
import org.jetbrains.ktor.response.*
import org.jetbrains.ktor.routing.*
import org.jetbrains.ktor.util.decodeBase64
import org.slf4j.Logger
@location("/login")
data class Login(val userId: String = "", val password: String = "", val error: String = "")
@location("/userTable") class SimpleUserTable
val hashedUserTable = UserHashedTableAuth(table = mapOf(
"test" to decodeBase64("VltM4nfheqcJSyH887H+4NEOm2tDuKCl83p5axYXlF0=")
))
fun Application.basicAuth() {
install(DefaultHeaders)
install(CallLogging)
install(ConditionalHeaders)
install(PartialContentSupport)
install(Locations)
install(Routing) {
manual(log)
}
}
fun Route.manual(log: Logger) {
post<Login> {
log.info(it.toString())
call.respond(it.userId) // get nothing
}
get { login: Login ->
call.respond("login page")
}
}
我使用 insomnia 测试请求,结果如下:
> POST /login HTTP/1.1
> Host: localhost:8080
> User-Agent: insomnia/5.6.3
> Accept: */*
> Accept-Encoding: deflate, gzip
> Content-Type: application/x-www-form-urlencoded
> Content-Length: 33
| userId=username&password=password
< HTTP/1.1 200 OK
< Date: Sun, 27 Aug 2017 16:52:20 GMT
< Server: ktor-core/0.4.0 ktor-core/0.4.0
< Content-Type: text/plain; charset=UTF-8
< Content-Length: 0
任何人都可以帮助我吗?