【问题标题】:How to log user credentials when authentication fails with Scala Play Framework and Silhouette使用 Scala Play Framework 和 Silhouette 身份验证失败时如何记录用户凭据
【发布时间】:2020-03-06 21:28:23
【问题描述】:

我有一个使用剪影的安全操作,如下所示:

def data: Action[JsValue] = silhouette.SecuredAction(errorHandler).async(parse.json) { implicit request =>
...

我想在用户身份验证失败时记录消息。最好是他/她用来进行身份验证的用户名。我找不到这样做的方法。

【问题讨论】:

    标签: scala playframework silhouette


    【解决方案1】:

    最后我发现创建自己的 AuthProvider(或者更确切地说是覆盖现有的 BasicAuthProvider)并添加 MarkerContext 是最简单的。像这样:

    class BasicAuthProvider @Inject()(
        override protected val authInfoRepository: AuthInfoRepository,
        override protected val passwordHasherRegistry: PasswordHasherRegistry)(implicit override val executionContext: ExecutionContext)
        extends providers.BasicAuthProvider(authInfoRepository, passwordHasherRegistry)(executionContext) {
    
      /**
        * Authenticates an identity based on credentials sent in a request.
        *
        * @param request The request.
        * @tparam B The type of the body.
        * @return Some login info on successful authentication or None if the authentication was unsuccessful.
        */
      override def authenticate[B](request: Request[B]): Future[Option[LoginInfo]] = {
        getCredentials(request) match {
          case Some(credentials) =>
            val loginInfo = LoginInfo(id, credentials.identifier)
            val marker: org.slf4j.Marker =
              MarkerFactory.getMarker(loginInfo.toString)
            implicit val mc: MarkerContext = MarkerContext(marker)
    
            authenticate(loginInfo, credentials.password).map {
              case Authenticated => Some(loginInfo)
              case InvalidPassword(error) =>
                logger.warn(error)
                None
              case UnsupportedHasher(error) => throw new ConfigurationException(error)
              case NotFound(error) =>
                logger.warn(error)
                None
            }
          case None => Future.successful(None)
        }
      }
    }
    

    【讨论】:

      猜你喜欢
      • 2020-09-25
      • 2014-06-29
      • 2016-06-29
      • 2018-10-23
      • 1970-01-01
      • 2016-02-08
      • 1970-01-01
      • 1970-01-01
      • 2018-11-02
      相关资源
      最近更新 更多