【问题标题】:GSON password serialization policyGSON 密码序列化策略
【发布时间】:2023-04-07 05:35:01
【问题描述】:

我想创建序列化策略,它将解码对象中的字段,这些字段是密码。如果字段名称包含“密码”,我希望 GSON 使用我定义的加密算法。我尝试扩展 TypeAdapter 并将其插入 GsonBuilder,但随后我失去了 TreeTypeAdapter 的所有功能。有什么想法吗?

我有类似的东西,但我需要自己完成 TreeTypeAdapter 的所有工作:

public class PasswordStringAdapter extends TypeAdapter<String> {


    @Override
    public void write(JsonWriter out, String value) throws IOException {

        String keyName = getKeyName(out);

    }

    private String getKeyName(JsonWriter writer) {

        try {
            Field name = writer.getClass().getDeclaredField("deferredName");
            name.setAccessible(true);
            String fieldName = (String) name.get(writer);

            //if field name contains password then encode it

        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
      return null;
    }

} 

【问题讨论】:

    标签: java serialization gson


    【解决方案1】:

    事实证明,在我解密密码后,我只需要这样做:

    out.value(value);
    

    如果您有更好的想法,请发表评论。

    【讨论】:

      猜你喜欢
      • 2021-05-26
      • 1970-01-01
      • 1970-01-01
      • 2013-02-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多