Java Record "toString()" mask sensitive fields

79 views Asked by At

Is there any way to mask sensitive fields on the automatically generated "toString()" method in Java Records without overriding it? (i.e. Annotations)

i.e.

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

Please no "Use Lombok!" answers :)

1

There are 1 answers

3
NickNgn On

I think you can wrap the sensitive info by the object:

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

So in your SomeRecord:

record SomeRecord(..., SensitiveInfo apiKey, SensitiveInfo password)