【问题标题】:'Mandatory fields are missing' response from APIGEE APIAPIGEE API 的“缺少必填字段”响应
【发布时间】:2021-10-03 01:11:27
【问题描述】:

我在 Android 中使用 nimbus-jose-jwt 库生成加密的 JWT (JWE),并将其发送到 APIGEE API 的主体中。

我已使用以下代码生成加密的 JWT:

public class EncryptedJWTGenerator {
    String jweString;
    Map<String, Object> map = new HashMap<>();

    @RequiresApi(api = Build.VERSION_CODES.O)
    public EncryptedJWTGenerator() throws NoSuchAlgorithmException, JOSEException, InvalidKeySpecException {

        String publicKey = <my_public_key>;

        try {
            // create Gson instance
            Gson gson = new Gson();

            URL url = getClass().getResource("Payload.json"); //JSON file having the Payload

            // create a reader
            Reader reader = Files.newBufferedReader(Paths.get(url.toURI().getPath()));

            // convert JSON file to map
            map = gson.fromJson(reader, Map.class);

            // print map entries
            for (Map.Entry<?, ?> entry : map.entrySet()) {
                System.out.println(entry.getKey() + "=" + entry.getValue());
            }

            // close reader
            reader.close();

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        JWEAlgorithm alg = JWEAlgorithm.RSA_OAEP_256;
        EncryptionMethod enc = EncryptionMethod.A256GCM;


        byte[] publicBytes = Base64.decodeBase64(publicKey);
        X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicBytes);
        KeyFactory keyFactory = KeyFactory.getInstance("RSA");
        PublicKey pubKey = keyFactory.generatePublic(keySpec);

        // Generate the preset Content Encryption (CEK) key
        KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
        keyGenerator.init(EncryptionMethod.A256GCM.cekBitLength());

        SecretKey cek = keyGenerator.generateKey();

        JOSEObjectType joseObjectType = new JOSEObjectType("JWT");
        JWEHeader jweHeader = new JWEHeader(alg, enc, joseObjectType, null, null, null, null, null, null, null, null, null, null,
                null, null, null, null, 0, null, null, null, null);

        // Encrypt the JWE with the RSA public key + specified AES CEK
        JWEObject jweObject = new JWEObject(new JWEHeader(jweHeader), new Payload(map));

        jweObject.encrypt(new RSAEncrypter((RSAPublicKey) pubKey, cek));

        jweString = jweObject.serialize();
    }
}

但是当我在 API 主体中使用这个 JWE 时,我得到以下响应:

{
    "Code": 0,
    "Response": {},
    "Message": "Mandatory Fields are missing"
}

但是当我使用这个工具 -> https://dinochiesa.github.io/jwt/ 生成 JWE 并将它与 APIGEE API 一起使用时,它工作正常。

我的 JWE 生成代码中是否缺少某些内容?

【问题讨论】:

  • 你为什么不比较结果看看缺少什么?

标签: android encryption jwt jwe nimbus-jose-jwt


【解决方案1】:

通过将我的 JSON 作为字符串传递给 Payload 解决了这个问题,而不是从文件中读取并转换为映射并将其传递给 Payload。

JWEObject jweObject = new JWEObject(new JWEHeader(jweHeader), new Payload(map)); 替换为:

 JWEObject jweObject = new JWEObject(new JWEHeader(jweHeader), new Payload(<my_json_string>));

【讨论】:

    猜你喜欢
    • 2016-06-09
    • 1970-01-01
    • 2018-07-19
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-05-30
    相关资源
    最近更新 更多