【发布时间】:2023-04-04 17:34:01
【问题描述】:
我必须使用 SHA-256 算法和密钥(例如“blablablamysecretkey”)获取 JWT。
尽管检查了 SO,但有几个库及其文档我还不知道如何执行此操作。
如果我使用这个库https://github.com/jwtk/jjwt(最常用的之一),这是代码示例:
Key key = MacProvider.generateKey();
String s = Jwts.builder().setSubject("stringtoencode").signWith(SignatureAlgorithm.HS512, key).compact();
由于我必须使用 SHA-256 算法,我想我应该使用:
Key key = MacProvider.generateKey();
String s = Jwts.builder().setSubject("stringtoencode").signWith(SignatureAlgorithm.HS256, key).compact();
我的问题是这个示例(以及我顺便看到的所有示例)使用 Key key = MacProvider.generateKey();,如果我没记错,这会生成一个通用密钥。事实上,这就是文档所说的:
// 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 类的东西?
【问题讨论】: