Java Record“toString()”屏蔽敏感字段

问题描述 投票:0回答:1

有没有办法屏蔽 Java Records 中自动生成的“toString()”方法上的敏感字段而不覆盖它? (即注释)

public record SomeRecord(..., String apiKey, String password, ...) { } 

请不要“使用龙目岛!”答案:)

java record
1个回答
0
投票

我认为你可以通过对象包装敏感信息:

record SensitiveInfo(String info) {
   public String toString() {
      // add your masking func here
    }
   // other necessary methods to use with info field
}

所以在你的 SomeRecord 中:

record SomeRecord(..., SensitiveInfo apiKey, SensitiveInfo password)
© www.soinside.com 2019 - 2024. All rights reserved.