Shirisha
Shirisha

Reputation: 71

Not able to decode jwt token in JMeter using PostProcessor

I am new to JMeter.I want to perfrom some load testing on one of my application. I want to decode JWT token and from there need to get Session id and it has to be appended to JMeter variable.

Below is my code

import com.auth0.jwt.*

log.info("Hello")
def jwt = JWT.decode(vars.get('idToken'))
log.info(jwt)

vars.put('sessionId', jwt.getClaim('sessionId').asString())
log.info(jwt.getClaim('sessionId').asString())

It is producing some errors while executing this code

Below is the exception message

javax.script.ScriptException: groovy.lang.MissingPropertyException: No such property: JWT for class: Script11
    at org.codehaus.groovy.jsr223.GroovyScriptEngineImpl.eval(GroovyScriptEngineImpl.java:320) ~[groovy-jsr223-3.0.7.jar:3.0.7]
    at org.codehaus.groovy.jsr223.GroovyCompiledScript.eval(GroovyCompiledScript.java:71) ~[groovy-jsr223-3.0.7.jar:3.0.7]
    at javax.script.CompiledScript.eval(CompiledScript.java:93) ~[java.scripting:?]
    at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:217) ~[ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.extractor.JSR223PostProcessor.process(JSR223PostProcessor.java:45) [ApacheJMeter_components.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.runPostProcessors(JMeterThread.java:955) [ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:573) [ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) [ApacheJMeter_core.jar:5.4.1]
    at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) [ApacheJMeter_core.jar:5.4.1]
    at java.lang.Thread.run(Thread.java:831) [?:?]

Need suggestion to resolve this issue.

JMeter Version : 5.4

Upvotes: 1

Views: 1448

Answers (1)

Dmitri T
Dmitri T

Reputation: 168122

You need to have java-jwt-x.xx.x.jar along with its dependencies in JMeter Classpath before running the script.

For the latest stable version 3.18.1 you will need the following .jars:

  • jackson-annotations-2.11.0.jar
  • jackson-core-2.11.0.jar
  • jackson-databind-2.11.0.jar
  • java-jwt-3.18.1.jar

If you do have them in JMeter Classpath already you will have to restart JMeter to pick the libraries up

Once done you should be able to use the code you copied and pasted from somewhere:

Demo using JWT token eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiIxMjM0NTY3ODkwIiwibmFtZSI6IkpvaG4gRG9lIiwiaWF0IjoxNTE2MjM5MDIyfQ.SflKxwRJSMeKKF2QT4fwpMeJf36POk6yJV_adQssw5c from https://jwt.io/ web page:

enter image description here

More information on Groovy scripting in JMeter: Apache Groovy - Why and How You Should Use It

Upvotes: 2

Related Questions