やろうとしたこと&エラー
サーバーから開発用PCにフォワードを設定していた。そこでcookieに secure = true を入れたかった。
val call: ApplicationCall by lazy { TODO() }
call.response.cookies.append(
name = "key",
value = "value",
maxAge = 10.seconds.inWholeSeconds,
domain = ServerEnv.domain,
path = ".",
secure = true,
)
以下のエラーが出た。
Exception while fetching data (/path/to/target) : You should set secure cookie only via secure transport (HTTPS)
解決
まずはサーバーのnginxに以下を追加した。
server {
proxy_set_header X-Forwarded-SSL $https;
}
依存を追加(Gradle)
implementation("io.ktor:ktor-server-forwarded-header:$ktorVersion")
installする。
fun Application.myApplicationModule() {
install(XForwardedHeaders)
}
一言
以下に書いてあるコメントに同意します。 secure=true はブラウザが判別するべきもので、サーバーが設定できないようにするのはどうなのかと思いました。
https://github.com/ktorio/ktor/issues/311#issuecomment-1422346111