You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

升级Scala与play-authenticate后遇方法不存在错误求助

解决play-authenticate 0.9.0升级后isLoggedIn/getUser方法不存在的问题

问题背景

将SBT项目从Scala 2.11.2升级至2.12.4,同步升级play-authenticate依赖:

  • 旧依赖:"com.feth" % "play-authenticate_2.11" % "0.6.9"
  • 新依赖:"com.feth" %% "play-authenticate" % "0.9.0"
    同时升级Play框架至兼容的2.6.0版本后,出现编译错误:
value isLoggedIn is not a member of object com.feth.play.module.pa.PlayAuthenticate
value getUser is not a member of object com.feth.play.module.pa.PlayAuthenticate

原因分析

play-authenticate 0.9.0针对Play 2.6+做了API重构,原PlayAuthenticate对象的静态方法改为依赖注入模式,不再支持通过PlayAuthenticate.isLoggedIn()或PlayAuthenticate.getUser()的方式调用。

解决方案

1. 依赖注入方式调用方法

在控制器或需要使用这些方法的类中,通过构造函数注入PlayAuthenticate实例,再调用实例方法:

Scala 示例

import com.feth.play.module.pa.PlayAuthenticate
import javax.inject.Inject
import play.api.mvc._

class HomeController @Inject()(playAuth: PlayAuthenticate, val controllerComponents: ControllerComponents) extends BaseController {
  def index = Action { request =>
    // 替代原PlayAuthenticate.isLoggedIn(request)
    if (playAuth.isLoggedIn(request)) {
      // 替代原PlayAuthenticate.getUser(request)
      val user = playAuth.getUser(request)
      Ok(s"当前登录用户:${user.id}")
    } else {
      Ok("未登录")
    }
  }
}

Java 示例

import com.feth.play.module.pa.PlayAuthenticate;
import javax.inject.Inject;
import play.mvc.Controller;
import play.mvc.Result;

public class HomeController extends Controller {
    private final PlayAuthenticate playAuth;

    @Inject
    public HomeController(PlayAuthenticate playAuth) {
        this.playAuth = playAuth;
    }

    public Result index() {
        if (playAuth.isLoggedIn(request())) {
            Object user = playAuth.getUser(request());
            return ok("当前登录用户:" + user.toString());
        } else {
            return ok("未登录");
        }
    }
}

2. 确认模块启用

在conf/application.conf中确保play-authenticate模块已启用:

play.modules.enabled += "com.feth.play.module.pa.PlayAuthenticateModule"

3. 同步其他API变更

项目中其他使用PlayAuthenticate对象静态方法的地方,都需要改为调用注入实例的对应方法,可参考play-authenticate 0.9.0的版本迁移说明调整。


内容的提问来源于stack exchange,提问作者manuelbcr

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.09 07:25:39