概述
问题:
Tomcat启动慢,启动日志提示:警告 [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [172,808] milliseconds.
运行环境:
- OS Version: Linux/3.10.0-862.11.6.el7.x86_64
- Server version: Apache Tomcat/9.0.12
- JVM Version: Oracle Corporation/11.0.1+13-LTS
原因:
由于Tomcat的session ID的生成是通过java.security.SecureRandom生成随机数来实现,随机数算法使用的是”SHA1PRNG”。在Sun/Oracle的JDK里,这个算法的提供者在底层依赖到操作系统提供的随机数据,在Linux上,与之相关的是/dev/random和/dev/urandom(非阻塞的随机数发生器,它会重复使用熵池中的数据以产生伪随机数据。)。
请看JDK的 $JAVA_HOME/conf/security/java.security 文件中的相关配置:
#
# Sun Provider SecureRandom seed source.
#
# Select the primary source of seed data for the "NativePRNG", "SHA1PRNG"
# and "DRBG" SecureRandom implementations in the "Sun" provider.
# (Other SecureRandom implementations might also use this property.)
#
# On Unix-like systems (for example, Solaris/Linux/MacOS), the
# "NativePRNG", "SHA1PRNG" and "DRBG" implementations obtains seed data from
# special device files such as file:/dev/random.
#
# On Windows systems, specifying the URLs "file:/dev/random" or
# "file:/dev/urandom" will enable the native Microsoft CryptoAPI seeding
# mechanism for SHA1PRNG and DRBG.
#
# By default, an attempt is made to use the entropy gathering device
# specified by the "securerandom.source" Security property. If an
# exception occurs while accessing the specified URL:
#
# NativePRNG:
# a default value of /dev/random will be used. If neither
# are available, the implementation will be disabled.
# "file" is the only currently supported protocol type.
#
# SHA1PRNG and DRBG:
# the traditional system/thread activity algorithm will be used.
#
# The entropy gathering device can also be specified with the System
# property "java.security.egd". For example:
#
# % java -Djava.security.egd=file:/dev/random MainClass
#
# Specifying this System property will override the
# "securerandom.source" Security property.
#
# In addition, if "file:/dev/random" or "file:/dev/urandom" is
# specified, the "NativePRNG" implementation will be more preferred than
# DRBG and SHA1PRNG in the Sun provider.
#
securerandom.source=file:/dev/random
所以我们的目标就是改为非阻塞的随机数生成器,如果直接指定 file:/dev/urandom 不能解决的话,可以指定为:file:/dev/./urandom。
解决这个问题有三种方式:
- 在Tomcat环境中解决:在Tomcat的catalina.sh中加入这么一行:-Djava.security.egd=file:/dev/./urandom 即可。
- 如果是可运行的Jar,可以在运行的时候指定:java -Djava.security.egd=file:/dev/./urandom MainClass。
- 在JVM环境中解决:在$JAVA_PATH/jre/lib/security/java.security(JDK11对应: $JAVA_HOME/conf/security/java.security ,具体看Java版本)这个文件中,修改securerandom.source=file:/dev/./urandom即可。
重启之后,不再出现警告,同时启动时间也大大缩短。
文章引用:https://blog.roncoo.com/article/125962
最后
以上就是积极冬瓜为你收集整理的Tomcat9 启动时创建 SecureRandom 实例使用了过长的时间的全部内容,希望文章能够帮你解决Tomcat9 启动时创建 SecureRandom 实例使用了过长的时间所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复