如何在 Gatling 中对密码进行编码?

How to encode the password in Gatling?

我的项目要求对登录请求中发送的密码进行编码。最好的方法是什么?

您可以将凭据作为键值对存储在文件中。读取向量中的文件,然后解码值。该值可以存储在会话中,并可以在您的 Gatling 请求中使用,如下所示:

import scala.concurrent.duration._
import io.gatling.core.Predef._
import io.gatling.core.structure.ChainBuilder
import io.gatling.http.Predef._
import io.gatling.jdbc.Predef._
import io.gatling.jsonpath._
import Headers._
import scala.collection._
import java.time.format.DateTimeFormatter
import java.time.LocalDateTime
import java.time.LocalDate

import io.gatling.core.feeder._
import java.util.Base64
import java.nio.charset.StandardCharsets
import scala.util.matching.Regex

object TestClass {

//Now you can use below code to read the credential file,

  val recordsByEnv: Map[String, Seq[Record[Any]]] =
    csv("/User/sandy/data/userdetails.csv").readRecords.groupBy { record =>
      record("env").toString
    }
  val passwordByEnv: Map[String, Seq[Any]] = recordsByEnv.mapValues { records =>
    records.map { record =>
      record("password")
    }
  }
  val password_v = (passwordByEnv.get("env_name").toString)
  val password_encoded = password_v.substring(12, (password_v.length - 2))
  val credentials = new String(Base64.getDecoder.decode(password_encoded))
  //Store the decoded value in session variable
    .exec(session => session.set("password", credentials))
    .exec(
      http("Authorization")
        .post("https://XXXX.xxxxxx.com")
        .header("Content-Type", "application/x-www-form-urlencoded")
        .formParam("password", "${password}")
        .formParam("username", "USERNAME")
    )
//Below is user details file look like below
//env_name,encoded_password

}