【问题标题】:How to mask values in complex json structure using dataweave?如何使用 dataweave 屏蔽复杂 json 结构中的值?
【发布时间】:2018-03-20 08:06:18
【问题描述】:

我想屏蔽 json 中的元素。下面的 json 中的 descID 元素应该被屏蔽。能否请教一下。

{
    "status": "ok",
    "statusCode": "19x9s011",
    "statusDescription": "Service: XYZ IOP ; country: india ; Locale:en-US ; SourceId:KOP; ApiUid: 644e1dd7-2a7f-18fb-b8ed-ed78c3f899s2c2b; Description: The NMK profile call was successful.",
    "details": {
        "descID": "11840000000012698",
        "Code": "XX",
        "languageCode": "en",
        "profile": {
            "base": {
                "username": "abc",
                "firstName": "xc",
                "middleName": "test",
                "lastName": "123",
                "shortName": "xc",
                "displayName": "D",
                "suffix": "T",
                "prefix": "E"
            }
        }
    }
}

【问题讨论】:

    标签: mule-studio mule-el dataweave


    【解决方案1】:

    我可能会使用类似的东西来屏蔽数据:

    %dw 1.0
    %input payload application/json
    %output application/json
    
    
    %var keyToEncrypt = ['descID']
    
    %function encrypt(val) "*****"
    
    %function needsEncrypt(key) (sizeOf (keyToEncrypt find key)) > 0
    
    
    %function maskSensitiveData(value) value mapObject ({
    ($$) : maskSensitiveData($) when $ is :object otherwise $
    } unless needsEncrypt($$ as :string) otherwise {
    ($$) : encrypt($)
    })
    
    ---
    
    maskSensitiveData(payload)
    

    本文逐字引用自https://blogs.mulesoft.com/dev/training-dev/encrypt-specific-xml-tags-with-the-power-of-dataweave/

    如果您需要完全删除该字段,那么我可能会使用类似:

    %dw 1.0
    %input payload application/json
    %output application/json
    
    
    %var keyToEncrypt = ['descID']
    
    %function encrypt(val) "*****"
    
    %function needsEncrypt(key) (sizeOf (keyToEncrypt find key)) > 0
    
    
    %function maskSensitiveData(value) value mapObject ({
    ($$) : maskSensitiveData($) when $ is :object otherwise $
    } unless needsEncrypt($$ as :string) otherwise {})
    
    ---
    
    maskSensitiveData(payload)
    

    【讨论】:

    • 嗨乍得,感谢您的回复。我的要求是它本身不应该存在的输出 descID 元素。如果它像状态代码一样直接位于第一级,我可以像 paylaod - staus 那样做。但是如果要移除的元素在多个层级下,具体如何处理呢?
    【解决方案2】:

    1.将payload转化为object

    <json:json-to-object-transformer doc:name="JSON to Object" returnClass="java.util.HashMap"/>
    

    2.覆盖payload中的descID

    <expression-component doc:name="Expression"><![CDATA[payload.details.descID=flowVars.maskedvalue;]]></expression-component>
    

    3.转回json

    <json:object-to-json-transformer doc:name="Object to JSON"/>
    

    【讨论】:

      猜你喜欢
      • 2021-12-05
      • 2021-06-25
      • 2022-08-18
      • 2011-05-08
      • 1970-01-01
      • 1970-01-01
      • 2011-08-26
      • 2018-12-12
      • 1970-01-01
      相关资源
      最近更新 更多