2014-11-25 78 views
0

我试图理解字符串不变性如何提高安全性。我搜查了许多案件,但没有给出实际的例子。字符串不变性的安全性

下面是这样一个例子 -

boolean connect(string s){ 
    if (!isSecure(s)) { 
     throw new SecurityException(); 
    } 
    //here will cause problem, if s is changed before this by using other references.  
    causeProblem(s); 
} 

在上述情况下,连接方法可以与任何有效的String

被称为对于离: - 连接( “DB2”)或连接(“ORACLE “)并且该方法将相应地执行。

有人能详细说明安全性如何增强吗?

如果它更基本的问题,请原谅。

+2

是什么让你觉得'String'不变性增加了安全性? – 2014-11-25 02:45:54

+0

这是一个这样的博客: - http://java.dzone.com/articles/why-string-immutable-java – user1050619 2014-11-25 02:48:46

+0

这里是另一个在SO - http://stackoverflow.com/questions/15274874/how-does -java-string-being-immutable-increase-security – user1050619 2014-11-25 02:50:04

回答

0

也有相反的注意事项,例如某些API避免使用String作为密码,更喜欢char[](可以在使用后立即在内存中擦除,而String可能会在相当长的时间内停留)。