zoukankan      html  css  js  c++  java
  • [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中添加

    if [[ "$JAVA_OPTS" != *-Djava.security.egd=* ]]; then
        JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/./urandom"
    fi
    

    这个系统属性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

    if [[ "$JAVA_OPTS" != *-Djava.security.egd=* ]]; then
        JAVA_OPTS="$JAVA_OPTS -Djava.security.egd=file:/dev/./urandom"
    fi
    

    当然,如果不想修改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
    
  • 相关阅读:
    Yarn的三种调度器
    tcpdump 常用用法
    Openssh升级脚本
    RAID详解及软RAID管理
    zabbix安装部署
    Linux根分区、SWAP分区扩容
    Windows server 2012 控制面板打不开防火墙
    Windows Server 设置时间同步出错问题
    Windows dos命令激活系统
    Swap分区大小设置
  • 原文地址:https://www.cnblogs.com/nxzblogs/p/12176717.html
Copyright © 2011-2022 走看看