游戏框架中的基本身份验证

Basic Authentication in play framework

如何使用 play 框架实现 web 套接字的基本身份验证。

我正在使用 play 框架创建一个网络套接字。 我想进行基本身份验证并在身份验证失败时发送 401。 以下是我的代码,我无法发送“{code=401, message=unauthorized access}”作为响应

def ChatServer(): WebSocket = WebSocket.accept[String, String] { request =>
    if (Util.doBasicAuthentication(request.headers)) {
      ActorFlow.actorRef { out =>
        ChatActor.props(out)
      }
    } else throw new RuntimeException("Unauthorized Access")
  }

每当身份验证失败时,我都无法将响应作为 "unauthorized access" 发回,而是以异常结束

Play documentation, use WebSocket.acceptOrResult所述:

def socket = WebSocket.acceptOrResult[String, String] { request =>
  Future.successful {
    if (Util.doBasicAuthentication(request.headers)) {
      Right(ActorFlow.actorRef { out =>
        ChatActor.props(out)
      })
    } else {
      Left(Unauthorized)
    }
  }
}