当前位置: 首页 > 面试题库 >

服务响应时间慢:Java SecureRandom和/ dev / random

段干昊然
2023-03-14
问题内容

我正在尝试调试Tomcat上部署的应用程序提供的一些慢速响应。现在,我侧重于SecureRandom/dev/random(其他一些可能的原因进行了调查和排除)。模式如下:

  • Tomcat重新启动后,第一次调用恰好需要 30.0 xy秒(即使请求在启动后4分钟到达)
  • 后来,一些呼叫恰好花费了 15.0 pq秒(没有可以建立的特定模式, pq 是TP99中花费的时间)。

服务调用涉及加密和解密( AES / ECB / PKCS5Padding )。

SecureRandom初始化/重新填充是否有可能导致这种情况?

(尽管,在catalina.log中写了一条日志,上面写着"Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [28,760] milliseconds."

此外,为了检查是否/dev/random/dev/urandom正在使用,我到测试使用的这个问题。令我惊讶的是,与链接问题中发生的情况不同,我没有看到它们中的任何一个。这些是strace日志的最后几行:

3561  lstat("/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/jsse.jar", {st_mode=S_IFREG|0644, st_size=258525, ...}) = 0
3561  open("/usr/lib/jvm/java-1.6.0-openjdk-1.6.0.0.x86_64/jre/lib/jsse.jar", O_RDONLY) = 6
3561  stat("/dev/random", {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 8), ...}) = 0
3561  stat("/dev/urandom", {st_mode=S_IFCHR|0666, st_rdev=makedev(1, 9), ...}) = 0
3561  open("/dev/random", O_RDONLY)     = 7
3561  open("/dev/urandom", O_RDONLY)    = 8
3561  unlink("/tmp/hsperfdata_xxxx/3560") = 0

那么,什么用于播种SecureRandom?

fyi, java -version

java version "1.6.0_32"
OpenJDK Runtime Environment (IcedTea6 1.13.4) (rhel-7.1.13.4.el6_5-x86_64)
OpenJDK 64-Bit Server VM (build 23.25-b01, mixed mode)

问题答案:

我无法检查您的OpenJDK具体版本,但可以检查jdk6-b33。

SecureRandom使用SeedGenerator获取种子字节

public byte[] engineGenerateSeed(int numBytes) {
    byte[] b = new byte[numBytes];
    SeedGenerator.generateSeed(b);
    return b;
}

SeedGenerator
seedSource从SunEntries获取(String)

String egdSource = SunEntries.getSeedSource();

SunEntries尝试java.security.egd首先从系统属性获取源,如果找不到,则尝试securerandom.sourcejava.security属性文件获取属性,如果找不到属性,则返回空白字符串。

// name of the *System* property, takes precedence over PROP_RNDSOURCE
private final static String PROP_EGD = "java.security.egd";
// name of the *Security* property
private final static String PROP_RNDSOURCE = "securerandom.source";

final static String URL_DEV_RANDOM = "file:/dev/random";
final static String URL_DEV_URANDOM = "file:/dev/urandom";

private static final String seedSource;

static {
    seedSource = AccessController.doPrivileged(
            new PrivilegedAction<String>() {

        public String run() {
            String egdSource = System.getProperty(PROP_EGD, "");
            if (egdSource.length() != 0) {
                return egdSource;
            }
            egdSource = Security.getProperty(PROP_RNDSOURCE);
            if (egdSource == null) {
                return "";
            }
            return egdSource;
        }
    });
}

SeedGenerator检查该值初始化该实例

// Static instance is created at link time
private static SeedGenerator instance;

private static final Debug debug = Debug.getInstance("provider");

final static String URL_DEV_RANDOM = SunEntries.URL_DEV_RANDOM;
final static String URL_DEV_URANDOM = SunEntries.URL_DEV_URANDOM;

// Static initializer to hook in selected or best performing generator
static {
    String egdSource = SunEntries.getSeedSource();

    // Try the URL specifying the source
    // e.g. file:/dev/random
    //
    // The URL file:/dev/random or file:/dev/urandom is used to indicate
    // the SeedGenerator using OS support, if available.
    // On Windows, the causes MS CryptoAPI to be used.
    // On Solaris and Linux, this is the identical to using
    // URLSeedGenerator to read from /dev/random

    if (egdSource.equals(URL_DEV_RANDOM) || egdSource.equals(URL_DEV_URANDOM)) {
        try {
            instance = new NativeSeedGenerator();
            if (debug != null) {
                debug.println("Using operating system seed generator");
            }
        } catch (IOException e) {
            if (debug != null) {
                debug.println("Failed to use operating system seed "
                              + "generator: " + e.toString());
            }
        }
    } else if (egdSource.length() != 0) {
        try {
            instance = new URLSeedGenerator(egdSource);
            if (debug != null) {
                debug.println("Using URL seed generator reading from "
                              + egdSource);
            }
        } catch (IOException e) {
            if (debug != null)
                debug.println("Failed to create seed generator with "
                              + egdSource + ": " + e.toString());
        }
    }

    // Fall back to ThreadedSeedGenerator
    if (instance == null) {
        if (debug != null) {
            debug.println("Using default threaded seed generator");
        }
        instance = new ThreadedSeedGenerator();
    }
}

如果来源是

final static String URL_DEV_RANDOM = "file:/dev/random";

要么

final static String URL_DEV_URANDOM = "file:/dev/urandom"

使用NativeSeedGenerator,在Windows上尝试使用原生CryptoAPILinux上的类简单地扩展SeedGenerator.URLSeedGenerator

package sun.security.provider;

import java.io.IOException;

/**
 * Native seed generator for Unix systems. Inherit everything from
 * URLSeedGenerator.
 *
 */
class NativeSeedGenerator extends SeedGenerator.URLSeedGenerator {

    NativeSeedGenerator() throws IOException {
        super();
    }

}

并调用/dev/random默认加载的超类构造函数

URLSeedGenerator() throws IOException {
    this(SeedGenerator.URL_DEV_RANDOM);
}

因此,/dev/random在您未在系统属性java.security.egdsecurerandom.source安全属性文件的属性中设置另一个值之前,OpenJDK
默认使用。

如果要使用阅读结果strace,可以更改命令行并添加trace=open,read表达式

sudo strace -o a.strace -f -e trace=open,read java class

您可以看到类似的内容(我使用Oracle JDK 6进行了测试)

13225 open("/dev/random", O_RDONLY)     = 8
13225 read(8, "@", 1)                   = 1
13225 read(3, "PK\3\4\n\0\0\0\0\0RyzB\36\320\267\325u\4\0\0u\4\0\0 \0\0\0", 30) = 30
....
....

如果您在启动过程中遇到延迟,请参阅“ Tomcat Wiki”部分中有关启动更快的建议,使用/ dev / urandom之类的非阻塞熵源

更多信息:https
:
//wiki.apache.org/tomcat/HowTo/FasterStartUp#Entropy_Source

希望这可以帮助。



 类似资料:
  • 我做了一个蛇游戏(通过尝试遵循youtuber的代码),方向由WASD控制。然而,当我按下其中一个键时,什么都没有发生。如果我按住它,它会改变方向,但会有很大的延迟,可能会超过一秒钟。如何修复此问题?我已经查看了我的代码,并将其与我关注过几次的youtube代码进行了比较,但似乎仍然看不出问题出在哪里。这是我第一次做游戏,所以我对这个很陌生。 如果有帮助的话,这是我试图跟踪的视频。 https:/

  • 如何在MULE ESB平台中获取Web服务的POST请求的响应时间?

  • 如何使用ruby获得服务器响应时间? get_response(URI. parse(url))到URL的响应代码和响应时间。实际上我的代码是: 它工作得很好,但我的响应时间太高,例如:Twitter。com(630.52ms)。如果我尝试ping推特。com,我收到70/120毫秒的回复。 这是计算此服务器响应时间的最佳方法吗?

  • 2.17 响应时间(边缘) 2.17.1 描述 返回5分钟颗粒度的状态码明细数据,主要返回时间戳、省份、运营商、HIT/MISS、状态码、响应时间、请求数 2.17.2 请求地址 地址: https://api.bokecs.com/channel/responseTime?time={time}domain={domain} 2.17.3 请求方式 GET 2.17.4 请求参数 参数名称 是否

  • 在Wireshark中,当我们在跟踪一个网站后检查DNS响应时,哪个部分反映了“关于名称服务器的信息”? 权威RRS? 附加RRS? 或在答案部分(名称,类型,类,时间,数据) 对不起,是英语和Wireshark的新手。 谢谢你

  • 问题内容: 一些背景信息:我想在Red Hat服务器上运行脚本以从/ dev / random中读取一些数据,并使用Perl unpack()命令将其转换为十六进制字符串,以备后用(基准数据库操作)。我在/ dev / random上运行了一些“ head -1”,它看起来工作得很好,但是多次调用后,它还是会挂起。几分钟后,它将最终输出一小段文本,然后结束。 我切换到/ dev / urandom

  • Response响应对象主要将JSP容器处理后的结果传回到客户端。可以通过response变量设置HTTP的状态和向客户端发送数据,如Cookie、HTTP文件头信息等。 一个典型的响应看起来就像下面这样: HTTP/1.1 200 OK Content-Type: text/html Header2: ... ... HeaderN: ... (空行) <!doctype ...> <ht