Akka HTTP Scala JWT 身份验证错误“没有方法解码的重载替代方案”

问题描述 投票:0回答:1

我正在尝试将 JWT 身份验证添加到我的 Akka HTTP Scala 应用程序 API。

我正在使用 Maven 存储库中的 Maven 依赖模块

jwt-circe
jwt-core

但是当前的代码给了我

Type errors

case Some(jwtToken) if Jwt.isValid(jwtToken, secretKey, Seq(JwtAlgorithm.HS256))

Type Error: None of the overloaded alternatives of method isValid in trait JwtCore with types
.

onComplete(Jwt.decode(jwtToken, secretKey, Seq(JwtAlgorithm.HS256)))

Type Error: None of the overloaded alternatives of method decode in trait JwtCore with types
.

我再次安装了依赖模块

jwt-circe
jwt-core
(检查
sbt
代码)。

您能帮我找出问题所在吗?

提前致谢!

我当前的代码如下所示:

案例类:


final case class Account(name: String, age: Int, country: String, roles: List[String])

object JsonFormats {
import DefaultJsonProtocol._

implicit val accountFormat: RootJsonFormat[Account] = jsonFormat4(Account.apply)

JWT 代码:


import pdi.jwt.{ Jwt, JwtAlgorithm, JwtClaim, JwtCirce }

private val expiresIn = 1 * 24 * 60 * 60
implicit val clock: Clock = Clock.systemUTC
private val secretKey = "vp-akka-http-jwt"

def authenticated: Directive1[Account] =
  optionalHeaderValueByName("Authorization").flatMap {
    case Some(jwtToken) if Jwt.isValid(jwtToken, secretKey, Seq(JwtAlgorithm.HS256)) =>
         getClaims(jwtToken) match {
           case Some(account) => provide(account)
           case None => reject(AuthorizationFailedRejection).toDirective[Tuple1[Account]]
         }
    case t => println(t.get)
           complete(StatusCodes.Unauthorized)
}
      
private def getClaims(jwtToken: String): Option[Account] = {
    onComplete(Jwt.decode(jwtToken, secretKey, Seq(JwtAlgorithm.HS256))){
       case Success(value) => 
            Some(value.content.parseJson.convertTo[Account])
       case Failure(ex) => None
    }            
}

SBT 配置:


lazy val akkaHttpVersion = "10.6.3"
lazy val akkaVersion    = "2.9.4"

resolvers += "Akka library repository".at("https://repo.akka.io/maven")

fork := true

lazy val root = (project in file(".")).
  settings(
    inThisBuild(List(
      organization    := "com.example",
      scalaVersion    := "3.3.3"
    )),
    name := "eskimi-bidding-agent",
    libraryDependencies ++= Seq(
      // ....
      ("com.github.jwt-scala"     %% "jwt-circe"             % "10.0.1")
         .exclude("io.circe", "circe-parser_3")
         .exclude("io.circe", "circe-core_3")
         .exclude("org.typelevel", "cats-kernel_3")
         .exclude("org.typelevel", "cats-core_3")
         .exclude("io.circe", "circe-numbers_3")
         .exclude("org.typelevel", "jawn-parser_3")
         .exclude("io.circe", "circe-jawn_3"),
      "com.github.jwt-scala"     %% "jwt-core"             % "10.0.1",
      // ....
    )
  )


完整代码错误:

这是

Jwt.isValid
错误。它与
Jwt.decode
错误非常相似。

Type Error:
case Some(jwtToken) if Jwt.isValid(jwtToken, secretKey, Seq(JwtAlgorithm.HS256)) 
None of the overloaded alternatives of method isValid in trait JwtCore with types

(token: String, key: java.security.PublicKey): Boolean
(token: String, key: java.security.PublicKey, options: pdi.jwt.JwtOptions): Boolean
(token: String, key: java.security.PublicKey, algorithms: Seq[pdi.jwt.algorithms.JwtAsymmetricAlgorithm]): Boolean
(token: String, key: java.security.PublicKey, algorithms: Seq[pdi.jwt.algorithms.JwtAsymmetricAlgorithm], options: pdi.jwt.JwtOptions): Boolean
(token: String, key: javax.crypto.SecretKey): Boolean
(token: String, key: javax.crypto.SecretKey, options: pdi.jwt.JwtOptions): Boolean
(token: String, key: javax.crypto.SecretKey, algorithms: Seq[pdi.jwt.algorithms.JwtHmacAlgorithm]): Boolean
(token: String, key: javax.crypto.SecretKey, algorithms: Seq[pdi.jwt.algorithms.JwtHmacAlgorithm], options: pdi.jwt.JwtOptions): Boolean
(token: String, key: String, algorithms: => Seq[pdi.jwt.algorithms.JwtAsymmetricAlgorithm]): Boolean
(token: String, key: String, algorithms: => Seq[pdi.jwt.algorithms.JwtAsymmetricAlgorithm], options: pdi.jwt.JwtOptions): Boolean
(token: String, key: String, algorithms: Seq[pdi.jwt.algorithms.JwtHmacAlgorithm]): Boolean
(token: String, key: String, algorithms: Seq[pdi.jwt.algorithms.JwtHmacAlgorithm], options: pdi.jwt.JwtOptions): Boolean
(token: String): Boolean
(token: String, options: pdi.jwt.JwtOptions): Boolean 
match arguments ((jwtToken : String), (com.example.WebServer.secretKey : String), scala.collection.Seq[pdi.jwt.JwtAlgorithm.HS256.type])

scala akka akka-http
1个回答
0
投票

只需将函数

JwtAlgorithm
Jwt.isValid()
Jwt.decode()
参数更改为不可变代码,如下所示:
scala.collection.immutable.Seq(JwtAlgorithm.HS256)

所以最终的代码如下所示:

Import pdi.jwt.{ Jwt, JwtAlgorithm, JwtClaim, JwtCirce }

Private val expiresIn = 1 * 24 * 60 * 60
Implicit val clock: Clock = Clock.systemUTC
Private val secretKey = “vp-akka-http-jwt”

Def authenticated: Directive1[Account] =
  optionalHeaderValueByName(“Authorization”).flatMap {
    case Some(jwtToken) if Jwt.isValid(jwtToken, secretKey, scala.collection.immutable.Seq(JwtAlgorithm.HS256)) =>
         getClaims(jwtToken) match {
           case Some(account) => provide(account)
           case None => reject(AuthorizationFailedRejection).toDirective[Tuple1[Account]]
         }
    Case t => println(t.get)
           Complete(StatusCodes.Unauthorized)
}
      
Private def getClaims(jwtToken: String): Option[Account] = {
    onComplete(Jwt.decode(jwtToken, secretKey, scala.collection.immutable.Seq(JwtAlgorithm.HS256))){
       case Success(value) => 
            Some(value.content.parseJson.convertTo[Account])
       Case Failure(ex) => None
    }            
}
© www.soinside.com 2019 - 2024. All rights reserved.