[tomcat]-tomcat8启动时SessionIdGeneratorBase.createSecureRandom耗时
https://blog.csdn.net/xujiamin0022016/article/details/88142973
tomcat8啟動(dòng)時(shí)SessionIdGeneratorBase.createSecureRandom耗時(shí)
14-Jul-2016 04:14:22.900 INFO [localhost-startStop-1] org.apache.catalina.util.SessionIdGenerator.createSecureRandom Creation of SecureRa
ndom instance for session ID generation using [SHA1PRNG] took [142,673] milliseconds.
在apache-tomcat官方文檔:如何讓tomcat啟動(dòng)更快 里面提到了一些啟動(dòng)時(shí)的優(yōu)化項(xiàng),其中一項(xiàng)是關(guān)于隨機(jī)數(shù)生成時(shí),采用的“熵源”(entropy source)的策略。
他提到tomcat7的session id的生成主要通過java.security.SecureRandom生成隨機(jī)數(shù)來實(shí)現(xiàn),隨機(jī)數(shù)算法使用的是”SHA1PRNG”
private String secureRandomAlgorithm = "SHA1PRNG";
在sun/oracle的jdk里,這個(gè)算法的提供者在底層依賴到操作系統(tǒng)提供的隨機(jī)數(shù)據(jù),在linux上,與之相關(guān)的是/dev/random和/dev/urandom,對(duì)于這兩個(gè)設(shè)備塊的描述以前也見過討論隨機(jī)數(shù)的文章,wiki中有比較詳細(xì)的描述,摘抄過來,先看/dev/random :
在讀取時(shí),/dev/random設(shè)備會(huì)返回小于熵池噪聲總數(shù)的隨機(jī)字節(jié)。/dev/random可生成高隨機(jī)性的公鑰或一次性密碼本。若熵池空了,對(duì)/dev/random的讀操作將會(huì)被阻塞,直到收集到了足夠的環(huán)境噪聲為止
而 /dev/urandom 則是一個(gè)非阻塞的發(fā)生器:
dev/random的一個(gè)副本是/dev/urandom (”unlocked”,非阻塞的隨機(jī)數(shù)發(fā)生器),它會(huì)重復(fù)使用熵池中的數(shù)據(jù)以產(chǎn)生偽隨機(jī)數(shù)據(jù)。這表示對(duì)/dev/urandom的讀取操作不會(huì)產(chǎn)生阻塞,但其輸出的熵可能小于/dev/random的。它可以作為生成較低強(qiáng)度密碼的偽隨機(jī)數(shù)生成器,不建議用于生成高強(qiáng)度長(zhǎng)期密碼。
另外wiki里也提到了為什么linux內(nèi)核里的隨機(jī)數(shù)生成器采用SHA1散列算法而非加密算法,是為了避開法律風(fēng)險(xiǎn)(密碼出口限制)。
回到tomcat文檔里的建議,采用非阻塞的熵源(entropy source),通過java系統(tǒng)屬性來設(shè)置:
-Djava.security.egd=file:/dev/./urandom
catalina.sh中添加
這個(gè)系統(tǒng)屬性egd表示熵收集守護(hù)進(jìn)程(entropy gathering daemon),但這里值為何要在dev和random之間加一個(gè)點(diǎn)呢?是因?yàn)橐粋€(gè)jdk的bug,在這個(gè)bug的連接里有人反饋及時(shí)對(duì) securerandom.source 設(shè)置為 /dev/urandom 它也仍然使用的 /dev/random,有人提供了變通的解決方法,其中一個(gè)變通的做法是對(duì)securerandom.source設(shè)置為 /dev/./urandom 才行。也有人評(píng)論說這個(gè)不是bug,是有意為之。
我看了一下我當(dāng)前所用的jdk7的java.security文件里,配置里仍使用的是/dev/urandom:
# 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我不確定jdk7里,這個(gè) /dev/urandom 也同那個(gè)bug報(bào)告里所說的等同于 /dev/random;要使用非阻塞的熵池,這里還是要修改為/dev/./urandom 呢,還是jdk7已經(jīng)修復(fù)了這個(gè)問題,就是同注釋里的意思,只好驗(yàn)證一下。
使用bug報(bào)告里給出的代碼:
import java.security.SecureRandom; class JRand {public static void main(String args[]) throws Exception {System.out.println("Ok: " +SecureRandom.getInstance("SHA1PRNG").nextLong());} }java -Djava.security.egd=file:/dev/./urandom test.jar
自己測(cè)試tomcat8
catalina.sh
當(dāng)然,如果不想修改catalina.sh腳本的話,建議自己寫個(gè)啟動(dòng)程序,例如:
export JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom" # 這個(gè)JAVA_OPTS 在catalina.sh腳本執(zhí)行的時(shí)候,回獲取(具體可看catalina.sh遠(yuǎn)嗎) sh /home/work/software/tomcat/bin/catalina.sh start > /dev/null 2>&1 與50位技術(shù)專家面對(duì)面20年技術(shù)見證,附贈(zèng)技術(shù)全景圖總結(jié)
以上是生活随笔為你收集整理的[tomcat]-tomcat8启动时SessionIdGeneratorBase.createSecureRandom耗时的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell中$*和$@ 两个都区别
- 下一篇: javadoc源码获取