我正在尝试创建一个嵌套的JSON JWT,按照此处提供的示例进行操作:http://connect2id.com/products/nimbus-jose-jwt/examples/signed-and-encrypted-jwt
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import com.nimbusds.jose.*;
import com.nimbusds.jose.crypto.*;
import com.nimbusds.jwt.*;
// Generate 256-bit AES key for HMAC as well as encryption
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(256);
SecretKey secretKey = keyGen.generateKey();
// Create HMAC signer
JWSSigner signer = new MACSigner(secretKey.getEncoded());
// Prepare JWT with claims set
JWTClaimsSet claimsSet = new JWTClaimsSet();
claimsSet.setSubject("alice");
claimsSet.setIssueTime(new Date());
claimsSet.setIssuer("https://c2id.com");
SignedJWT signedJWT = new SignedJWT(new JWSHeader(JWSAlgorithm.HS256), claimsSet);
// Apply the HMAC
signedJWT.sign(signer);
// Create JWE object with signed JWT as payload
JWEObject jweObject = new JWEObject(
new JWEHeader.Builder(JWEAlgorithm.DIR, EncryptionMethod.A256GCM)
.contentType("JWT") // required to signal nested JWT
.build(),
new Payload(signedJWT));
// Perform encryption
jweObject.encrypt(new DirectEncrypter(secretKey.getEncoded()));
// Serialise to JWE compact form
String jweString = jweObject.serialize();
我遇到的问题是生成的 jweString 不正确。
eyJhbGc.
.6Ne
.tw8z
我得到了4个部分而不是3个部分,其中一个部分为空。
eyJhbGc.
.6Ne