2014-11-25 74 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可能會在相當長的時間內停留)。