---
title: "how to defend against serialization and reflection attack for eager singleton"
author: "dracularking"
date: "2012-08-08T09:18:25Z"
tags: []
categories: ["编程语言"]
slug: "how-to-defend-against-serialization-and-reflection-attack-for-eager-singleton"
draft: false
lastmod: "2012-08-08T09:22:17Z"
id: 1624740
url: "https://dracularking.iteye.com/blog/1624740"
---

//为什么要定义构造方法

因为不定义它也会有默认构造方法，而且是public的，不符合单例的设计模式要求

这种eager singleton是线程安全的，因为JVM保证了静态变量只由classloader初始化一次，也因此意味着所有调用getInstance的线程只能得到同一个变量实例

但是这种方法不足以保护其免遭reflection attack，因为反射可以改变私有变量的访问控制符  
AccessibleObject.setAccessible(true)，应对这种情况就需要这样：

 

```
public class JavaSingleton {
  private static final JavaSingleton INSTANCE = new JavaSingleton();
  private JavaSingleton() {
    if (INSTANCE != null) {
      throw new IllegalStateException("Inside JavaSingleton(): JavaSingleton " +
                                                        "instance already created.");
    }
    System.out.println("Inside JavaSingleton(): Singleton instance is being created.");
  }
  public static final JavaSingleton getInstance() {
    return INSTANCE;
  }
}
```

 

同时也不能免遭serialization attack，应对法：  
定义所有的实例域为transient，再提供一个readResolve方法，返回现有实例，来保证singleton

见：

[http://technonstop.com/java-singleton-reflection-and-lazy-initialization](http://technonstop.com/java-singleton-reflection-and-lazy-initialization)  
[http://www.chiaocheng.com/blog/2009/07/java-singleton/](http://www.chiaocheng.com/blog/2009/07/java-singleton/)  
[http://stackoverflow.com/questions/5735797/is-this-singleton-resistant-to-both-serialization-and-reflection-attacks](http://stackoverflow.com/questions/5735797/is-this-singleton-resistant-to-both-serialization-and-reflection-attacks)