如何使用 RSA 256 私钥在 JMETER 上生成 JWT 令牌所需的库或 jar 文件?

时间:2021-04-06 06:50:34

标签: jmeter jwt rsa jmeter-plugins

目前,我正在对 API 进行性能测试,需要 RSA 256 私钥的动态 JWT 令牌。我没有找到任何解决方案。谁能给我提供任何 JWT jar 文件和凹槽代码

1 个答案:

答案 0 :(得分:0)

我相信您需要为此编写 Groovy 脚本

  1. 获取 JWT client library,例如 this guy 将是一个不错的选择并将其放到 JMeter Classpath(确保包括所有 dependencies

  2. 重启 JMeter 以获取 .jar

  3. JSR223 Sampler 添加到您的测试计划

  4. 示例代码类似于:

    def keyPayr = io.jsonwebtoken.security.Keys.keyPairFor(io.jsonwebtoken.SignatureAlgorithm.RS256)
    
    def now = java.time.Instant.now()
    
    def clientId = 'foo'
    
    def jwt = io.jsonwebtoken.Jwts.builder()
            .setAudience('https://example.com')
            .setIssuedAt(Date.from(now))
            .setExpiration(Date.from(now.plus(5L, java.time.temporal.ChronoUnit.MINUTES)))
            .setIssuer(clientId)
            .setSubject(clientId)
            .setId(UUID.randomUUID().toString())
            .signWith(keyPayr.private)
            .compact()
    
    log.info('Token: ' + jwt)
    

演示:

enter image description here