0

私は Scala でガトリング テストを行っており、デコードされた JWT トークンのいくつかのフィールドを検証したいと考えています。私はそれをデコードする方法を知っていますが、結果のJSONをJavaで行ったようにJacksonを使用してエンティティにマップし、値や存在を確認することはできません/非常に遅いです。

次のような HTTP リクエストを実行し、JSON で JWT トークンを取得します。

{"id_token":"xxxxxxx...."}

トークンは JWT です。それをデコードして、別の JSON を取得できます。

JWSObject jwsObject = JWSObject.parse(authorizeToken); // from com.nimbusds.jose.JWTObject
log.info("Decoded JWS object: {}", jwsObject.getPayload().toString());

それは私を得る:

{
    "sub": "c3f0d627-4820-4397-af20-1de71b208b15",
    "birthdate": "1942-11-08",
    "last_purchase_time": 1542286200,
    "gender": "M",
    "auth_level": "trusted",
    "iss": "http:\/\/somehost.com",
    "preferred_username": "test6@app.com",
    "given_name": "test6",
    "middle_name": "test6",
    "nonce": "random_string",
    "prv_member_id": 146794237,
    "aud": "some_issuer",
    "nbf": 1546869709,
    "is_premium": true,
    "updated_at": 1540812517,
    "registered_time": 1527677605,
    "name": "test6 test6 test6",
    "nickname": "test6",
    "exp": 1546870708,
    "iat": 1546869709,
    "family_name": "test6",
    "jti": "838bdd3f-1add-46f5-b3a1-cb220d3547a6"
}

Java で DTO を定義し、この JSON を DTO のインスタンスに変換して、各フィールドの値をAssert.assertEquals()or でチェックします。

しかし、ガトリングでは、それは不可能です:

  • Jackson との変換は非常に遅く、永遠にかかります。
  • 呼び出しは連鎖しており、のcheck()ようには機能しませんorg.junit.Assert

といる:

  http(...).exec(...)
    .check(
      header(HttpHeaderNames.ContentType).is("application/json;charset=UTF-8"),
      jsonPath("$..id_token") exists,
      jsonPath("$..id_token").saveAs("id_token"),
      status.is(200),
    )
  )
  .exitHereIfFailed
  .exec(session => {
    val token = session("id_token").as[String]
    log.debug("Token: " + token)
    val decodedToken:String = JWSObject.parse(token).getPayload.toString()
    val dto:JWTPayloadDTO = JsonUtil.fromJson(decodedToken)  // this is very slow

    // here verification

    log.info("JWT payload: " + dto)
    session
  }

それで、なにかお手伝いできますか?check()一部動作しなくなりsession => {}ます。

JsonUtil.fromJson():

package xxx.helpers

import com.fasterxml.jackson.databind.{DeserializationFeature, ObjectMapper}
import com.fasterxml.jackson.module.scala.experimental.ScalaObjectMapper
import com.fasterxml.jackson.module.scala.DefaultScalaModule

import scala.collection.mutable.ListBuffer

object JsonUtil {
  val mapper = new ObjectMapper() with ScalaObjectMapper
  mapper.registerModule(DefaultScalaModule)
  mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false)

  def fromJson[T](json: String)(implicit m : Manifest[T]): T = {
    mapper.readValue[T](json)
  }


}

DTO:

package xxx.dto

import com.fasterxml.jackson.databind.PropertyNamingStrategy
import com.fasterxml.jackson.databind.annotation.JsonNaming

@JsonNaming(classOf[PropertyNamingStrategy.SnakeCaseStrategy])
case class JWTPayloadDTO(
  aud:                String,
  iss:                String,
  exp:                Long,
  nbf:                Long,
  iat:                Long,
  sub:                String,
  authLevel:          String,
  jti:                String,
  nonce:              String,

  preferredUsername:  String,
  name:               String,
  givenName:          String,
  familyName:         String,
  middleName:         String,
  nickname:           String,
  profile:            String,
  picture:            String,
  website:            String,
  email:              String,
  emailVerified:      Boolean,
  gender:             String,
  birthdate:          String,
  zoneInfo:           String,
  locale:             String,
  phoneNumber:        String,
  phoneNumberVerified:Boolean,
  mobileNumber:       String,
  updatedAt:          Long,
  registeredTime:     Long,
  prvMemberId:        Long,
  fbUid:              String,
  lastPurchaseTime:   Long,
  isPremium:          Boolean,
  isStaff:            Boolean
)
4

1 に答える 1

0

リポジトリのreadmeが示唆するように、最初は依存関係を解決するためにSonartypeに頼っています。

https://github.com/FasterXML/jackson-module-scala

sonatype.sbt:

resolvers += "Sonatype OSS Snapshots" at "https://oss.sonatype.org/content/repositories/snapshots"

そして、依存関係を追加しbuild.sbtます。

次に、モジュールの wiki ページに入り、Maven (削除済みsonatype.sbt)に変更します。

https://github.com/FasterXML/jackson-module-scala/wiki

のみbuild.sbt:

libraryDependencies += "com.fasterxml.jackson.module" % "jackson-module-scala_2.12" % "2.9.8" // latest

今、それは働き始めます。

于 2019-01-08T10:51:59.003 に答える