【问题标题】:Stateless Silhouette CookieAuthenticator can't find / deletes cookieStateless Silhouette Cookie Authenticator 找不到/已删除 cookie
【发布时间】:2017-08-26 19:48:04
【问题描述】:

我有一个 Play 应用程序,它允许用户使用社交服务提供商登录,并设置了与 Play-Silhouette-Slick 种子示例相同的身份验证设置。下面的代码可能没问题,但我还是包含了它。

def authenticate(provider: String): Action[AnyContent] = Action.async { implicit request =>
(socialProviderRegistry.get[SocialProvider](provider) match {
  case Some(provider: SocialProvider with CommonSocialProfileBuilder) =>
    provider.authenticate().flatMap {
      case Left(result) => Future.successful(result) // Redirect user to social provider
      case Right(authInfo) => for {
        profile <- provider.retrieveProfile(authInfo)
        user <- userService.save(profile)
        authInfo <- authInfoRepository.save(profile.loginInfo, authInfo)
        authenticator <- silhouette.env.authenticatorService.create(profile.loginInfo)
        cookie <- silhouette.env.authenticatorService.init(authenticator)
        result <- silhouette.env.authenticatorService.embed(cookie, Redirect(routes.EateriesController.eaterySelection()))
      } yield {
        silhouette.env.eventBus.publish(LoginEvent(user, request))
        println("Just to verify that everything went well")
        result
      }
    }
  case _ => Future.failed(new ProviderException(s"Cannot authenticate with unexpected social provider $provider"))
}).recover {
  case e: ProviderException =>
    logger.error("Unexpected provider error", e)
    Redirect(routes.SignInController.index()).flashing("error" -> Messages("could.not.authenticate"))
  }
}

我的问题是用户登录后,我的应用程序的端点无法检测到用户已登录。当我在登录后立即被重定向到页面时,我可以在 Firefox 中验证是否设置了身份验证器 cookie,但是一旦我导航到我的应用程序中的另一个页面,cookie 就不再存在了。

我猜我的应用程序认为cookie无效或其他什么,然后将其删除,但我目前不知道。是否有其他原因导致这种情况发生/我应该如何记录我的应用程序以缩小问题范围?

【问题讨论】:

    标签: scala playframework silhouette


    【解决方案1】:

    我建议您的 cookie 已过期。

    您可以在 CookieAuthenticatorSettings 中进行配置,其中 cookieMaxAge 设置为 None,这意味着生成的 cookie 是临时的。

    【讨论】:

    • 不幸的是,事实并非如此。 :(
    • Cookie 路径呢?
    • 我将 cookie 路径设置为“/”,应该是这样。
    【解决方案2】:

    我的依赖注入设置错误。我没有删除绑定本地时区时钟作为时钟实例的线,我猜是导致剪影模块使用不正确的时钟。我现在有这一行:

    bind[Clock].toInstance(Clock())
    

    并删除了这一行:

    bind(classOf[Clock]).toInstance(Clock.systemDefaultZone)
    

    【讨论】:

      猜你喜欢
      • 2011-10-08
      • 1970-01-01
      • 2012-04-29
      • 2014-10-26
      • 2010-10-15
      • 2020-09-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多