【问题标题】:Extract a key from a Base64 decoded string从 Base64 解码的字符串中提取密钥
【发布时间】:2021-12-12 22:40:37
【问题描述】:

我正在尝试使用 BeanSheel PostProcessor 解码 JMeter 中的 base 64 格式字符串,并从字符串中提取 nameid,我将在发送另一个请求时进一步使用它(nameid)。

我尝试了以下代码,但它不起作用。

import org.apache.commons.codec.binary.Base64;

String decoded_response = new String(Base64.decodeBase64(data));
String memberId = decoded_response.nameid;
log.info("memberid", memberId)

它给我的错误是:

ERROR o.a.j.u.BeanShellInterpreter: Error invoking bsh method: eval Sourced file: inline evaluation of: ``import org.apache.commons.codec.binary.Base64;  String decoded_response = new St . . . '' : Typed variable declaration : Cannot access field: nameid, on object: iDzÏí¢G§{"typ":"JWT","alg":"HS256"}{"nameid":"1862","unique_name":"user1@mywebsite.com","iss":"http://mywebsite-app.azurewebsites.net/","aud":"414e1927a3884f68abc79f7283837fd1","exp":1635435731,"nbf":1635349331}$S‘­vC‡öRé™v„2xÉr}ïK…u®KŸäpÏÆ¯-¢G§þÜ©y·š­êÞÆ˜«zÏâŸÎ·÷Ö¬rXžžßâvx

基本上在这里,我想解码令牌并获取一些 id 字段以用于进一步的请求。这样做的好方法是什么?是否有任何替代方法可以将 nameid 存储在另一个变量中并在另一个请求中进一步使用它?

【问题讨论】:

    标签: jmeter beanshell


    【解决方案1】:
    1. 你需要解码Base64URL,而不是Base64
    2. 如果成功,您将获得需要解析的 JSON
    3. Since JMeter 3.1 you're supposed to use JSR223 Test Elements and Groovy language 用于编写脚本

    所以我认为您需要将代码更改为:

    def response = prev.getResponseDataAsString()
    
    def parts = response.split('\\.')
    
    def part2 = org.codehaus.groovy.runtime.EncodingGroovyMethods.decodeBase64Url(parts[1])
    
    def nameid = new groovy.json.JsonSlurper().parse(part2).nameid
    

    更多信息:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-11-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多