Kotlin Ktor 활용한 간단한 Login 페이지 만들기
30 Dec 2019 | Kotlin ktorLogin 기능 구현하기
login.ftl
resources/templates/login.ftl
파일을 생성합니다.
<html> <head> <link rel="stylesheet" href="/static/styles.css"> </head> <body> <#if error??> <p style="color:red;">${error}</p> </#if> <form action="/login" method="post" enctype="application/x-www-form-urlencoded"> <div>User:</div> <div><input type="text" name="username" /></div> <div>Password:</div> <div><input type="password" name="password" /></div> <div><input type="submit" value="Login" /></div> </form> </body> </html>
Main.kt
package com.snowdeer import freemarker.cache.ClassTemplateLoader import io.ktor.application.Application import io.ktor.application.call import io.ktor.application.install import io.ktor.freemarker.FreeMarker import io.ktor.freemarker.FreeMarkerContent import io.ktor.http.content.resources import io.ktor.http.content.static import io.ktor.request.receiveParameters import io.ktor.response.respond import io.ktor.response.respondText import io.ktor.routing.get import io.ktor.routing.post import io.ktor.routing.route import io.ktor.routing.routing fun Application.main() { install(FreeMarker) { templateLoader = ClassTemplateLoader(this::class.java.classLoader, "templates") } routing { static("/static") { resources("static") } route("/login") { get { call.respond(FreeMarkerContent("login.ftl", null)) } post { val post = call.receiveParameters() val username = post["username"] println("[snowdeer] username: $username") if (username != null && post["password"] != null) { call.respondText("OK") } else { call.respond(FreeMarkerContent("login.ftl", mapOf("error" to "Invalid login"))) } } } } }
만약 리다이렉션(Redirection)을 하고 싶으면 다음과 같은 코드를 사용하면 됩니다.
call.respondRedirect("/", permanent = false)