Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java Record "toString()" mask sensitive fields

Tags:

java

record

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 :)

like image 624
JWT Avatar asked Apr 27 '26 02:04

JWT


1 Answers

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)
like image 168
NickNgn Avatar answered Apr 28 '26 17:04

NickNgn