在 Akka HTTP 的路由中,您可以使用“authenticateBasic”指令来执行基本身份验证。以下是一个示例:
import akka.http.scaladsl.server.Directives._
val routes = authenticateBasic("realm", myUserPassAuthenticator) { user =>
path("secure") {
get {
complete("This is secure")
}
}
}
在上面的示例中,“authenticateBasic”指令执行基本身份验证,并调用名为“myUserPassAuthenticator”的方法来验证用户凭据。如果身份验证成功,该路由将返回“This is secure”字符串。如果未经身份验证的用户尝试访问此路由,“authenticateBasic”将返回一个 401 响应,指示用户未经身份验证。
使用“authenticateBasic”指令时,请确保正确实现您的“myUserPassAuthenticator”身份验证方法,以便在通过“authenticateBasic”进行身份验证之前对用户进行身份验证。
上一篇:AkkaHttpSwagger设置-引用其他文件时出现406application/octet-stream错误。
下一篇:akkahttp中不允许混合版本。如何通过添加显式依赖项mavenakka-http-jackson解决该问题?