我正在 SAP CPI 环境中编写 Groovy 脚本,这意味着该脚本在某种程度固定的 JVM 中运行。
我上传了
jjwt-api-0.11.2
、jjwt-impl-0.11.2
和 jjwt-jackson-0.11.2
jar,以便构建 JWT。
部署后的第一次运行脚本运行得很好,但之后每次运行都会出现以下错误:
com.sap.it.rt.adapter.http.api.exception.HttpResponseException: An internal server error occured: java.lang.ClassCastException: io.jsonwebtoken.impl.DefaultJwtBuilder (loaded by com.sap.gateway.ip.core.customdev.classloader.MergeClassLoaders@1625616) cannot be cast to io.jsonwebtoken.JwtBuilder (loaded by com.sap.gateway.ip.core.customdev.classloader.MergeClassLoaders@1624982) (found matching interface io.jsonwebtoken.JwtBuilder loaded by com.sap.gateway.ip.core.customdev.classloader.MergeClassLoaders@1625616, but needed loader com.sap.gateway.ip.core.customdev.classloader.MergeClassLoaders@1624982)@ line 70 in script1.groovy.
第 70 行显示:
JwtBuilder jwtBuilder = Jwts.builder().setClaims(claims).setIssuedAt(nowDate).setExpiration(expDate).setHeader((Map<String, Object>) header);
即使我将其更改为:
,错误仍然存在DefaultJwtBuilder jwtBuilder = Jwts.builder().setClaims(claims).setIssuedAt(nowDate).setExpiration(expDate).setHeader((Map<String, Object>) header);
当我重新部署时,它第一次再次工作,然后每次后续调用都会返回错误。
作为调试辅助工具,我转储了
System.getenv()
,但它返回的数据在成功和错误的运行中都是相同的:
{HC_GLOBAL_HOST=br1.hana.ondemand.com, HC_COMPONENT=web, HC_REGION=BR_1, HC_APPLICATION=someid, HC_HOST_INT=int.br1.hana.ondemand.com, replicate_ebs_volumes=false, replicate_ebs_with_delete=false, HC_PROCESS_ID=f6576be76bd88828e305602dbbca9d13890c, ENABLE_CAM=false, HC_LANDSCAPE=production, DISABLE_ANTIVIRUS=true, HC_APPLICATION_URL=https://someid-n9e08ec00.int.br1.hana.ondemand.com, HC_LOCAL_HTTP_PORT=9001, HC_HOST_SCP=sapcp.br1.hana.ondemand.com, HC_HOST_SVC=svc.br1.hana.ondemand.com, HC_HOST_CERT=cert.br1.hana.ondemand.com, HC_LANDSCAPE_TYPE=single_AZ, HC_ACCOUNT=someotherid, HC_OP_HTTP_PROXY_HOST=localhost, LDAP_SEARCH_BASE=dc=spa3,dc=od,dc=sap,dc=biz, HC_HOST=br1.hana.ondemand.com, HC_OP_HTTP_PROXY_PORT=20003, HOME=/usr/sap/ljs/home, HC_AVAILABILITY_ZONE=NLB1FSA02}
其余相关代码为:
import com.sap.gateway.ip.core.customdev.util.Message;
import com.sap.it.api.ITApiFactory;
import com.sap.it.api.keystore.KeystoreService;
import java.security.cert.Certificate;
import java.security.Key;
import java.util.HashMap;
import java.io.File;
import java.io.IOException;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.RSAPrivateKeySpec;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Base64;
import java.util.Date;
import java.util.Map;
import java.util.Scanner;
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.JwtBuilder;
import io.jsonwebtoken.impl.DefaultJwtBuilder;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.impl.DefaultHeader;
def Message processData(Message message) {
def messageLog = messageLogFactory.getMessageLog(message);
messageLog.addAttachmentAsString("dump_env", System.getenv().toString(), "text/plain");
Map<String, Object> header = new HashMap<String, Object>();
header.put("typ", "JWT");
header.put("alg", "RS256");
// Claims claims = Jwts.claims();
Map<String, Object> claims = new HashMap<String, Object>();
claims.put("scope", "https://www.googleapis.com/auth/devstorage.full_control");
claims.put("aud", tokenUrl);
claims.put("iss", "[email protected]");
/* Create JWT json with JwtBuilder */
DefaultJwtBuilder jwtBuilder = Jwts.builder().setClaims(claims).setIssuedAt(nowDate).setExpiration(expDate)
.setHeader((Map<String, Object>) header);
// ^^^^^^^^----> Crashing line
// ...jwt signing and whatnot
/* Set body and URL encode the grant_type string. */
body = "grant_type=" + URLEncoder.encode("urn:ietf:params:oauth:grant-type:jwt-bearer") + "&assertion=" + assertion
message.setHeader("Content-Type", "application/x-www-form-urlencoded")
message.setBody(body);
return message;
以前我使用的是 Headers 和 Claims 类,但它们也表现出同样的问题(它们第一次工作正常,但后来抛出了一个类转换错误,该错误并没有通过更改变量类而消失)
我怀疑这是一个类加载错误,但我正在学习 Java,因为我正在研究这个问题,但我仍然没有掌握 JVM 和 Java 世界的特性。
弗拉基米尔,你解决这个问题了吗? 我有同样的问题,但是使用 JasperReports .jar。
问候。