Xiamaer

jwt.io 上创建 jwt 为什么这么快?

  •  
  •   Xiamaer · Apr 22, 2019 · 6844 views
    This topic created in 2761 days ago, the information mentioned may be changed or developed.

    最近在学习 jwt 我分别用 jjwt 和 java-jwt 写了两个 demo 生成 jwt,生成一个 jwt 字符串至少得 900ms
    为啥在这里能几十毫秒就能创建一个?

    17 replies  •  2019-04-23 15:43:50 +08:00
    ntdll
    ntdll
    Apr 22, 2019
    1
    jwt 唯一可能耗时的只有签名,但是 900ms 怎么样都有点过分了,payload 应该不至于这么大吧?

    所以比较大的概率是耗时的过程不在生成的 jwt 上,而是你的其他代码有速度慢的地方。
    whileFalse
    whileFalse
    Apr 22, 2019
    2
    好奇怎么做到 900ms 的。
    raiz
    raiz
    Apr 22, 2019
    3
    900ms 有问题
    Xiamaer
    Xiamaer
    OP
    Apr 22, 2019
    4
    System.out.println(System.currentTimeMillis());
    String jws = Jwts.builder() // (1)

    .setSubject("Bob") // (2)

    .signWith(SignatureAlgorithm.HS256,"mykey") // (3)

    .compact(); // (4)
    System.out.println(System.currentTimeMillis());
    System.out.println(jws);

    @gam2046 @whileFalse @raiz 我就只用了 api 生成 jwt,代码就这几句自己写的,别的啥都没有
    ntdll
    ntdll
    Apr 22, 2019
    5
    你确定你的代码可以跑??

    我这跑起来抛异常:io.jsonwebtoken.security.WeakKeyException: The signing key's size is 24 bits which is not secure enough for the HS256 algorithm. The JWT JWA Specification (RFC 7518, Section 3.2) states that keys used with HS256 MUST have a size >= 256 bits (the key size must be greater than or equal to the hash output size). Consider using the io.jsonwebtoken.security.Keys class's 'secretKeyFor(SignatureAlgorithm.HS256)' method to create a key guaranteed to be secure enough for HS256. See https://tools.ietf.org/html/rfc7518#section-3.2 for more information.


    我这的使用方法(Kotlin),看了下,基本和你的差不多。并没有很慢,如果你那跑起来需要近 1 秒的时间,检查一下是否配置原因导致?

    https://i.loli.net/2019/04/22/5cbd79471e5d9.jpg
    invalidtoken
    invalidtoken
    Apr 22, 2019
    6
    https://i.loli.net/2019/04/22/5cbd7b58c51cd.png

    试了下 Node 版的,大概 2ms 左右?... 用了同样的签名算法
    misaka19000
    misaka19000
    Apr 22, 2019
    7
    System.out.println(System.currentTimeMillis());

    楼主你确定不是来黑 Java 的吗
    66beta
    66beta
    Apr 22, 2019 via Android
    8
    原来 Java 系统级 API 获取时间还有坑
    quadrapop
    quadrapop
    Apr 22, 2019
    9
    在下用的 RSA256 生成 jwt 平均耗时 800ms,用 HS256 这种平均 200ms 不到,HS256 不太安全建议使用 RSA
    Xiamaer
    Xiamaer
    OP
    Apr 22, 2019
    10
    @misaka19000 没怎么用过这个,还请大佬指点
    Xiamaer
    Xiamaer
    OP
    Apr 22, 2019
    11
    @quadrapop 是用的 java 吗?用的 jjwt 还是 java-jwt
    misaka19000
    misaka19000
    Apr 22, 2019
    12
    @Xiamaer #10 我的意思是你的时间计算代码中把 IO 的时间也算进去了
    0x4F5DA2
    0x4F5DA2
    Apr 22, 2019
    14
    emmm。。。。接上一条,手滑发出去了。。。实习的时候写的垃圾玩意儿。。。生成 jwt 好像蛮快的,基本上能秒出
    wm5821090
    wm5821090
    Apr 22, 2019 via iPhone
    15
    Java 的加密 lib 你是收费的?
    quadrapop
    quadrapop
    Apr 23, 2019
    16
    @Xiamaer 用的 nimbus-jose-jwt 的包
    Xiamaer
    Xiamaer
    OP
    Apr 23, 2019
    17
    找到原因了,java 刚启动的时候代码执行效率较低,cpu 占用高,我循环创建了一下发现还是很快的,可以到毫秒级。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   1028 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 52ms · UTC 19:02 · PVG 03:02 · LAX 11:02 · JFK 14:02
    ♥ Do have faith in what you're doing.