Scala/Pac4j/Play框架新手遇类型不匹配编译错误,寻求排查修复方案
Hey Priyanka, let's break down what's going on here and walk through how to fix it!
The core issue here is almost certainly a dependency version mismatch between your original Activator setup and the sbt run command you're using now. Here's why:
- Activator is just a wrapper around sbt, but it often pins specific versions of sbt, Play, and related libraries (like pac4j) to ensure compatibility. When you switched to raw
sbt run, you might be using a different sbt version that pulls in incompatible versions of pac4j or Play. - The type mismatch error (
AuthenticatedRequestvsList[CommonProfile]) is a dead giveaway: pac4j's Play integration changed how it exposes user profiles across versions. Older versions might have passed the profile list directly to your action handlers, while newer versions wrap profiles in anAuthenticatedRequestobject. Since your code was working with Activator, it's tuned to the old dependency set, butsbt runis pulling in a newer (or older) incompatible version.
Let's tackle this step by step:
Lock your sbt version to match Activator's
Activator uses a specific sbt version under the hood. To replicate that, create or update theproject/build.propertiesfile in your project with the exact sbt version your original Activator setup used. For example:sbt.version=1.5.5(If you don't remember the version, check the original workspace's
project/build.propertiesor look up the Activator version you used—each Activator release maps to a specific sbt version.)Pin compatible pac4j-play and Play versions
pac4j and Play have strict version compatibility rules. Make sure yourbuild.sbtexplicitly specifies versions that work together. For example:- If you're using Play 2.8.x, use pac4j-play 11.x:
libraryDependencies += "org.pac4j" %% "pac4j-play" % "11.0.0" - For Play 2.7.x, use pac4j-play 10.x:
libraryDependencies += "org.pac4j" %% "pac4j-play" % "10.0.0"
Double-check the pac4j version compatibility matrix to match your original working setup.
- If you're using Play 2.8.x, use pac4j-play 11.x:
Clean and refresh dependencies
Old cached dependencies might be causing conflicts. Run these commands in your project root to start fresh:sbt clean sbt reload sbt updateThis will clear old build artifacts, reload your sbt configuration, and download the exact versions you specified.
Verify your action code (if needed)
If after fixing dependencies you still see issues, double-check how you're handling authenticated requests. For newer pac4j-play versions, you'll access profiles from the request object instead of a direct list:// Example of correct usage for newer pac4j versions def protectedAction = Secure("your-client-name") { Action { request: AuthenticatedRequest[AnyContent] => val profile = request.userProfile[CommonProfile] Ok(s"Hello, ${profile.getDisplayName}!") } }Since your code worked with Activator, this step might not be necessary—but it's a good check if dependency fixes alone don't resolve the error.
内容的提问来源于stack exchange,提问作者priyanka goel

