無任何工程的tomcat在阿里云的服務(wù)器上啟動罐旗。
通過觀察日志,查到原來是因?yàn)閟essionid的產(chǎn)生是需要生成隨機(jī)數(shù)。
因?yàn)閘inux下隨機(jī)數(shù)產(chǎn)生都依賴/dev/random和/dev/urandom兩個設(shè)備搁料。
他們產(chǎn)生隨機(jī)數(shù)的原理是利用當(dāng)前系統(tǒng)的熵池來計算出固定一定數(shù)量的隨機(jī)比特庐杨。
所謂熵池
Linux內(nèi)核采用熵來描述數(shù)據(jù)的隨機(jī)性莺葫。熵(entropy)是描述系統(tǒng)混亂無序程度的物理量匪凉,一個系統(tǒng)的熵越大則說明該系統(tǒng)的有序性越差,即不確定性越大捺檬。在信息學(xué)中再层,熵被用來表征一個符號或系統(tǒng)的不確定性,熵越大堡纬,表明系統(tǒng)所含有用信息量越少聂受,不確定度越大。
計算機(jī)本身是可預(yù)測的系統(tǒng)烤镐,因此饺饭,用計算機(jī)算法不可能產(chǎn)生真正的隨機(jī)數(shù)。但是機(jī)器的環(huán)境中充滿了各種各樣的噪聲职车,如硬件設(shè)備發(fā)生中斷的時間,用戶點(diǎn)擊鼠標(biāo)的時間間隔等是完全隨機(jī)的鹊杖,事先無法預(yù)測悴灵。Linux內(nèi)核實(shí)現(xiàn)的隨機(jī)數(shù)產(chǎn)生器正是利用系統(tǒng)中的這些隨機(jī)噪聲來產(chǎn)生高質(zhì)量隨機(jī)數(shù)序列。
這兩個設(shè)備的差異在于:/dev/random在不能產(chǎn)生新的隨機(jī)數(shù)時會阻塞程序骂蓖,而/dev/urandom不會(ublock)积瞒,當(dāng)然產(chǎn)生的隨機(jī)數(shù)效果就不太好了,這對加密解密這樣的應(yīng)用來說就不是一種很好的選擇登下。/dev/random會阻塞當(dāng)前的程序茫孔,直到根據(jù)熵池產(chǎn)生新的隨機(jī)字節(jié)之后才返回,所以使用/dev/random比使用/dev/urandom產(chǎn)生大量隨機(jī)數(shù)的速度要慢被芳。
下面是一個簡單的測試
那如何解決這個問題
tomcat的session id的生成主要通過java.security.SecureRandom生成隨機(jī)數(shù)來實(shí)現(xiàn)缰贝。
所以修改jdk目錄/jre/lib/security/java.security
#
# Select the source of seed data for SecureRandom. By default an
# attempt is made to use the entropy gathering device specified by
# the securerandom.source property. If an exception occurs when
# accessing the URL then the traditional system/thread activity
# algorithm is used.
#
# On Solaris and Linux systems, if file:/dev/urandom is specified and it
# exists, a special SecureRandom implementation is activated by default.
# This "NativePRNG" reads random bytes directly from /dev/urandom.
#
# On Windows systems, the URLs file:/dev/random and file:/dev/urandom
# enables use of the Microsoft CryptoAPI seed functionality.
#
securerandom.source=file:/dev/urandom