gpt4 book ai didi

java - 在 Web 服务之间共享 JJWT 签名

转载 作者:行者123 更新时间:2023-12-02 12:12:14 25 4
gpt4 key购买 nike

我已成功设置 JJWT 以在一系列 Web 服务的身份验证过程中使用。问题在于它们是在一个 Web 服务中创建的,但跨多个服务进行身份验证。如何成功、安全地使用签名,同时确保我的所有 Web 服务使用相同的签名来验证传入的 JWT?

// We need a signing key, so we'll create one just for this example. Usually
// the key would be read from your application configuration instead.
Key key = MacProvider.generateKey();

String compactJws = Jwts.builder()
.setSubject("Joe")
.signWith(SignatureAlgorithm.HS512, key)
.compact();

我知道我可以使用一个普通的旧字符串来 .signWith(Algorithm,String) 但是我已经意识到使用标准的 Java String (字面意思是)不够安全。我正在使用类似的东西:

String compactJws = Jwts.builder()
.setSubject("Joe")
.signWith(SignatureAlgorithm.HS512, "shared complex passphrase")
.compact();

最佳答案

根据RFC 7518 - JSON Web 算法 (JWA):

A key of the same size as the hash output (for instance, 256 bits for"HS256") or larger MUST be used with this algorithm. (Thisrequirement is based on Section 5.3.4 (Security Effect of the HMACKey) of NIST SP 800-117 (sic) [NIST.800-107], which states that theeffective security strength is the minimum of the security strengthof the key and two times the size of the internal hash value.)

对于 HS512,您必须使用至少 512 位的 key 。

key 需要随机选择。您可以使用MacProvider.generateKey();或其他随机生成器生成 key 并将其分发到您的服务器(例如以base64编码)

另一种方法是使用 RSA key 对。您使用私钥对 token 进行签名,并使用公钥进行验证。公钥可以安全地发布并被所有服务使用

另请参阅https://security.stackexchange.com/questions/95972/what-are-requirements-for-hmac-secret-key

关于java - 在 Web 服务之间共享 JJWT 签名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46457612/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com