我不能正确地转义,以便将变量与字符串的其余部分连接起来 . 我检查了新字符串的输出,看起来都很好 . 请参阅下面的代码 .
import javax.crypto.Mac
import javax.crypto.spec.SecretKeySpec
import java.security.InvalidKeyException
def emailAddress = '"test@test.com"'
def hmac_sha256(String secretKey, String data) {
try {
Mac mac = Mac.getInstance("HmacSHA256")
SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getBytes(), "HmacSHA256")
mac.init(secretKeySpec)
byte[] digest = mac.doFinal(data.getBytes())
return digest
} catch (InvalidKeyException e) {
throw new RuntimeException("Invalid key exception while converting to HMac SHA256")
}
}
secret='1234'
// this will fail
def hash = hmac_sha256(secret, '/sf/v3/Accounts/GetByUser{"username":' + emailAddress + ',"employeesonly":false,"singleplane":false,"clientId":"someID"}')
// this will pass
def hash = hmac_sha256(secret, '/sf/v3/Accounts/GetByUser{"username":"test@test.com","employeesonly":false,"singleplane":false,"clientId":"someID"}')
encodedData = hash.encodeHex().toString()
println encodedData
我发现一个失败的错误说:
Caught:java.lang.ClassFormatError:类文件中的非法类名“test-groovy-temp $ hmac_sha256”test-groovy-temp $ hmac_sha256 java.lang.ClassFormatError:非法类名“test-groovy-temp $ hmac_sha256”in test-groovy-temp.run上的类文件test-groovy-temp $ hmac_sha256(test-groovy-temp.groovy:21)
这只是在我尝试使用连接时 .
编辑删除拼写错误 .
1 回答
事实证明我的IDE / Compliler有问题 . 我正在为这个项目使用IntelliJ IDEA,也许我没有正确配置它 . 正如饶提到的那样,我也看到它不是在其他地方重复,所以我应该做得好 .
除非有人知道我的IDE /编译器有什么问题!