is this Singleton resistant to both Serialization and Reflection Attacks?

后端 未结 5 441
死守一世寂寞
死守一世寂寞 2021-02-04 22:25

Is the following code resistant to both Serialization and Reflection Attacks?

public class Example{
  private static Example instance=new Example();

  private E         


        
5条回答
  •  面向向阳花
    2021-02-04 22:38

    No, it is not. There is a better technique.

    Try something like this:

    public enum Elvis {
        INSTANCE;
        public static boolean isThereOnlyOneElvis() {
            return true;
        }
    }
    
    // In your code:
    if ( !Elvis.INSTANCE.isThereOnlyOneElvis() ) {
        System.err.println("Liar !!!");
    }
    

提交回复
热议问题