如何防止單例模式被JAVA反射攻擊
單例模式相信大家都知道,用過的人不在少數。之前寫過一篇博文《singleton模式四種安全執行緒的實現》(參見:http://blog.csdn.net/u013256816/article/details/50427061),講訴了單例模式的四種寫法,並指出預留位置模式的寫法比較ok,詳見如下:
package com.effective.singleton;public class Elvis{ private static boolean flag = false; private Elvis(){ } private static class SingletonHolder{ private static final Elvis INSTANCE = new Elvis(); } public static Elvis getInstance() { return SingletonHolder.INSTANCE; } public void doSomethingElse() { }}
但這都是基於一個條件:確保不會通過反射機制調用私人的構造器。
這裡舉個例子,通過JAVA的反射機制來“攻擊”單例模式:
package com.effective.singleton;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;public class ElvisReflectAttack{ public static void main(String[] args) throws InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, NoSuchMethodException, SecurityException { Class classType = Elvis.class; Constructor c = classType.getDeclaredConstructor(null); c.setAccessible(true); Elvis e1 = (Elvis)c.newInstance(); Elvis e2 = Elvis.getInstance(); System.out.println(e1==e2); }}
運行結果:false
可以看到,通過反射擷取建構函式,然後調用setAccessible(true)就可以調用私人的建構函式,所有e1和e2是兩個不同的對象。
如果要抵禦這種攻擊,可以修改構造器,讓它在被要求建立第二個執行個體的時候拋出異常。
經修改後:
package com.effective.singleton;public class ElvisModified{ private static boolean flag = false; private ElvisModified(){ synchronized(ElvisModified.class) { if(flag == false) { flag = !flag; } else { throw new RuntimeException("單例模式被侵犯!"); } } } private static class SingletonHolder{ private static final ElvisModified INSTANCE = new ElvisModified(); } public static ElvisModified getInstance() { return SingletonHolder.INSTANCE; } public void doSomethingElse() { }}
測試代碼:
package com.effective.singleton;import java.lang.reflect.Constructor;public class ElvisModifiedReflectAttack{ public static void main(String[] args) { try { Class classType = ElvisModified.class; Constructor c = classType.getDeclaredConstructor(null); c.setAccessible(true); ElvisModified e1 = (ElvisModified)c.newInstance(); ElvisModified e2 = ElvisModified.getInstance(); System.out.println(e1==e2); } catch (Exception e) { e.printStackTrace(); } }}
運行結果:
Exception in thread "main" java.lang.ExceptionInInitializerError at com.effective.singleton.ElvisModified.getInstance(ElvisModified.java:27) at com.effective.singleton.ElvisModifiedReflectAttack.main(ElvisModifiedReflectAttack.java:17)Caused by: java.lang.RuntimeException: 單例模式被侵犯! at com.effective.singleton.ElvisModified.(ElvisModified.java:16) at com.effective.singleton.ElvisModified.(ElvisModified.java:7) at com.effective.singleton.ElvisModified$SingletonHolder.(ElvisModified.java:22) ... 2 more
可以看到,成功的阻止了單例模式被破壞。
從JDK1.5開始,實現Singleton還有新的寫法,只需編寫一個包含單個元素的枚舉類型。推薦寫法:
package com.effective.singleton;public enum SingletonClass{ INSTANCE; public void test() { System.out.println("The Test!"); }}
測試代碼:
package com.effective;import java.lang.reflect.Constructor;import java.lang.reflect.InvocationTargetException;import com.effective.singleton.SingletonClass;public class TestMain{ public static void main(String[] args) throws NoSuchMethodException, SecurityException, InstantiationException, IllegalAccessException, IllegalArgumentException, InvocationTargetException { Class classType = SingletonClass.class; Constructor c = (Constructor) classType.getDeclaredConstructor(); c.setAccessible(true); c.newInstance(); }}
運行結果:
Exception in thread "main" java.lang.NoSuchMethodException: com.effective.singleton.SingletonClass.() at java.lang.Class.getConstructor0(Unknown Source) at java.lang.Class.getDeclaredConstructor(Unknown Source) at com.effective.TestMain.main(TestMain.java:22)
由此可見這種寫法也可以防止單例模式被“攻擊”。
而且這種寫法也可以防止序列化破壞單例模式,具體不在舉例了,有關序列化以及單例模式被序列化破壞可以參考博文《JAVA序列化》。
單元素的枚舉類型已經成為實現Singleton模式的最佳方法。