[tomcat]-tomcat8启动时SessionIdGeneratorBase.createSecureRandom耗时
https://blog.csdn.net/xujiamin0022016/article/details/88142973
tomcat8啟動時SessionIdGeneratorBase.createSecureRandom耗時
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啟動更快 里面提到了一些啟動時的優化項,其中一項是關于隨機數生成時,采用的“熵源”(entropy source)的策略。
他提到tomcat7的session id的生成主要通過java.security.SecureRandom生成隨機數來實現,隨機數算法使用的是”SHA1PRNG”
private String secureRandomAlgorithm = "SHA1PRNG";
在sun/oracle的jdk里,這個算法的提供者在底層依賴到操作系統提供的隨機數據,在linux上,與之相關的是/dev/random和/dev/urandom,對于這兩個設備塊的描述以前也見過討論隨機數的文章,wiki中有比較詳細的描述,摘抄過來,先看/dev/random :
在讀取時,/dev/random設備會返回小于熵池噪聲總數的隨機字節。/dev/random可生成高隨機性的公鑰或一次性密碼本。若熵池空了,對/dev/random的讀操作將會被阻塞,直到收集到了足夠的環境噪聲為止
而 /dev/urandom 則是一個非阻塞的發生器:
dev/random的一個副本是/dev/urandom (”unlocked”,非阻塞的隨機數發生器),它會重復使用熵池中的數據以產生偽隨機數據。這表示對/dev/urandom的讀取操作不會產生阻塞,但其輸出的熵可能小于/dev/random的。它可以作為生成較低強度密碼的偽隨機數生成器,不建議用于生成高強度長期密碼。
另外wiki里也提到了為什么linux內核里的隨機數生成器采用SHA1散列算法而非加密算法,是為了避開法律風險(密碼出口限制)。
回到tomcat文檔里的建議,采用非阻塞的熵源(entropy source),通過java系統屬性來設置:
-Djava.security.egd=file:/dev/./urandom
catalina.sh中添加
這個系統屬性egd表示熵收集守護進程(entropy gathering daemon),但這里值為何要在dev和random之間加一個點呢?是因為一個jdk的bug,在這個bug的連接里有人反饋及時對 securerandom.source 設置為 /dev/urandom 它也仍然使用的 /dev/random,有人提供了變通的解決方法,其中一個變通的做法是對securerandom.source設置為 /dev/./urandom 才行。也有人評論說這個不是bug,是有意為之。
我看了一下我當前所用的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里,這個 /dev/urandom 也同那個bug報告里所說的等同于 /dev/random;要使用非阻塞的熵池,這里還是要修改為/dev/./urandom 呢,還是jdk7已經修復了這個問題,就是同注釋里的意思,只好驗證一下。
使用bug報告里給出的代碼:
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
自己測試tomcat8
catalina.sh
當然,如果不想修改catalina.sh腳本的話,建議自己寫個啟動程序,例如:
export JAVA_OPTS="-Djava.security.egd=file:/dev/./urandom" # 這個JAVA_OPTS 在catalina.sh腳本執行的時候,回獲取(具體可看catalina.sh遠嗎) sh /home/work/software/tomcat/bin/catalina.sh start > /dev/null 2>&1 與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的[tomcat]-tomcat8启动时SessionIdGeneratorBase.createSecureRandom耗时的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: shell中$*和$@ 两个都区别
- 下一篇: javadoc源码获取