使用私钥生成 JSON Web 令牌 (JWT)

我正在尝试生成 JWT 但收到此错误:


openssl req -x509 -sha256 -nodes -days 365 -newkey rsa:2048 -keyout private.key -out certificate_pub.crt

我正在使用该io.jsonwebtoken.Jwts库和字符串形式的私钥,但出现错误。


Map<String, Object> payload = new HashMap<>();

payload.put("iss", orgId);

payload.put("sub", accountId);

payload.put("exp", expirationTime);

payload.put("aud", new 

StringBuilder("Url").append("/c/").append(apiKey).toString());

payload.put(new StringBuilder("Url").append("/s/entt_sdk").toString(), Boolean.TRUE);

return Jwts.builder().setClaims(payload).**signWith**(SignatureAlgorithm.RS256, privateKeyStr).compact();

java.lang.IllegalArgumentException: Base64-encoded key bytes may only be specified for HMAC signatures.  If using RSA or Elliptic Curve, use the signWith(SignatureAlgorithm, Key) method instead.

我的私钥如下所示:


-----BEGIN PRIVATE KEY-----

sajdkjsadkjsahdkjsadksadkjsadkjs

-----END PRIVATE KEY-----


呼唤远方
浏览 248回答 1
1回答

阿波罗的战车

错误非常简单 - 该方法只能用于 Hmac 算法。对于基于 RSA 的算法,您必须使用signWith(SignatureAlgorithm, Key).我将使用的密钥不是 PEM 格式 - 它将是 base64 DER 编码的字符串 - 为此,我必须删除该密钥的 PEM 格式的开头和结尾 ------BEGIN PRIVATE KEY-----并且-----END PRIVATE KEY-----:MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQC9/xBGBRhpSBa/Xc/5CeAQjjVMcXfIFOqeIc/kq7dnsLz+ntTrvTE3fyz0E8J8MLMTNypK7irt8MB00Gpm/NJHnLSGqgSqIXAvUbUsxCO8ULSTKLTQs4RiDbUtOWZgacvRN0IYRnN2tDWfDWTYAElBSValTt/jvHw72BMcLXd8plGQYYD52l6+w/7ENgLG+hNbVewdM/saJKyLY/jhRl758XWGw5bCmIYk9BCbUVDLc5PN+iiYoBFTQYwP0Y1ln58vNZ/CDBw2mW3qTyploxoAdmG86km4EK2mtlhWBaUshfHORxGkWlCXcwazWDoc5QJ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Ktv6zvKeOZeZUIDIUZDqZgLd3IT7rikVCedIljWNhroXU1wNMssJPkfiQToGaykUMbBcgZKIneU2IuYWaLXBN9oAj1u7/YQ0DpPqk/Sb2FpVX5eKfp4cu8XdyezxNuFFsPVdGBhBxhqJOJuUc/ZKbo5Stc3NXEE=以下是如何读取此密钥并使用它签署 JWT 的示例://create payloadMap<String, Object> payload = new HashMap<>();payload.put("iss", "orgId");payload.put("sub", "orgId");payload.put("exp", "orgId");payload.put("aud", newStringBuilder("Url").append("/c/").append("key").toString());payload.put(new StringBuilder("Url").append("/s/entt_sdk").toString(), Boolean.TRUE);// read keyString privateKeyB64 = Files.lines(Paths.get("src/main/resources/private.key")).collect(Collectors.joining());byte[] privateKeyDecoded = Base64.getDecoder()&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .decode(privateKeyB64);//create key specPKCS8EncodedKeySpec spec =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new PKCS8EncodedKeySpec(privateKeyDecoded);// create key form specKeyFactory keyFactory = KeyFactory.getInstance("RSA");PrivateKey privateKey = keyFactory.generatePrivate(spec);//create signed JWT - JWSString jws = Jwts.builder().setClaims(payload).signWith(SignatureAlgorithm.RS256, privateKey).compact();System.out.println(jws);请注意,我使用的是PKCS8EncodedKeySpec因为您的密钥似乎是 PKCS8 格式。输出是:eyJhbGciOiJSUzI1NiJ9.eyJzdWIiOiJvcmdJZCIsImF1ZCI6IlVybC9jL2tleSIsImlzcyI6Im9yZ0lkIiwiZXhwIjoib3JnSWQiLCJVcmwvcy9lbnR0X3NkayI6dHJ1ZX0.m8ASk4kUNx41csikpd0zALLQTjwG2pc0Ba0D9PGLVbI2NaY0IIXgaVVVJcIERz4ejj_jfq436v6v0_QnxdmvjMAnx88UmHGdrCT0V5MZl008LP4g4LrV-WczNltCUpoJQ-4CW6xkpXD03JIDQAYwaKb-PIOtm-pfLJhPPmxykc8QioueijhI5M__Pq5Nq0JCbkQxfGzxE5m_gJwwq7n290RBGRYH6AHeClaEJhDzLNitIejNvvua4zNNC6S1CHsa4ChaEFfRb9bi-jNEQW27IGhrKRCtuwleFwigl7oTIsyaRWlzuVNYcZHS707Z2o6Mkf9hDo8AGKURUVsJgA8WIg我在 Java 8 上测试了它。对于 Java 11,我收到一条错误,缺少有关 XML 处理的模块。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java