Auth0:使用算法签名时无法生成令牌的签名:SHA256withECDSA
Auth0: The Token's Signature couldn't be generated when signing using the Algorithm: SHA256withECDSA
大家好!
我正在尝试使用 auth0 ECDSA256 算法使用 ES256 签署 JWT 信息和 JWT 数据。
ECPublicKey publicKeyRs = getPublicKey();
ECPrivateKey privateKeyRs = getPrivateKey();
Algorithm algorithmRs = Algorithm.ECDSA256(publicKeyRs, privateKeyRs);
signedToken = JWT.create()
.withExpiresAt(new Date())
.withSubject(jwtData.getSub()).
withAudience(jwtData.getAud()).sign(algorithmRs);
但是当我 运行 创建 JWT 的符号函数时,我遇到了这个错误:
Exception in thread "main" com.auth0.jwt.exceptions.SignatureGenerationException: The Token's Signature couldn't be generated when signing using the Algorithm: SHA256withECDSA
at com.auth0.jwt.algorithms.ECDSAAlgorithm.sign(ECDSAAlgorithm.java:65)
at com.auth0.jwt.JWTCreator.sign(JWTCreator.java:441)
at com.auth0.jwt.JWTCreator.access0(JWTCreator.java:26)
at com.auth0.jwt.JWTCreator$Builder.sign(JWTCreator.java:419)
它是由以下原因造成的:
Caused by: java.security.SignatureException: Invalid DER signature format.
at com.auth0.jwt.algorithms.ECDSAAlgorithm.DERToJOSE(ECDSAAlgorithm.java:118)
at com.auth0.jwt.algorithms.ECDSAAlgorithm.sign(ECDSAAlgorithm.java:63)
... 6 more
你能帮忙解决这些错误并告诉我哪里出错了吗?
这里有一个简单示例,说明如何使用 ES256 获取签名的 JWT 令牌以及如何验证它:
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import java.security.KeyPair;
import java.time.LocalDate;
public class Jwt {
public static void main(String[] args) {
KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.ES256);
//generate signed JWT token
String signedToken = Jwts.builder()
.setExpiration(java.sql.Date.valueOf(LocalDate.now().plusWeeks(2)))
.setSubject("your subject")
.setAudience("your audience")
.signWith(keyPair.getPrivate())
.compact();
//verify signed JWT token (no exceptions means check is OK)
Jws<Claims> claimsJws = Jwts.parser()
.setSigningKey(keyPair.getPublic())
.parseClaimsJws(signedToken);
}
}
大家好! 我正在尝试使用 auth0 ECDSA256 算法使用 ES256 签署 JWT 信息和 JWT 数据。
ECPublicKey publicKeyRs = getPublicKey();
ECPrivateKey privateKeyRs = getPrivateKey();
Algorithm algorithmRs = Algorithm.ECDSA256(publicKeyRs, privateKeyRs);
signedToken = JWT.create()
.withExpiresAt(new Date())
.withSubject(jwtData.getSub()).
withAudience(jwtData.getAud()).sign(algorithmRs);
但是当我 运行 创建 JWT 的符号函数时,我遇到了这个错误:
Exception in thread "main" com.auth0.jwt.exceptions.SignatureGenerationException: The Token's Signature couldn't be generated when signing using the Algorithm: SHA256withECDSA
at com.auth0.jwt.algorithms.ECDSAAlgorithm.sign(ECDSAAlgorithm.java:65)
at com.auth0.jwt.JWTCreator.sign(JWTCreator.java:441)
at com.auth0.jwt.JWTCreator.access0(JWTCreator.java:26)
at com.auth0.jwt.JWTCreator$Builder.sign(JWTCreator.java:419)
它是由以下原因造成的:
Caused by: java.security.SignatureException: Invalid DER signature format.
at com.auth0.jwt.algorithms.ECDSAAlgorithm.DERToJOSE(ECDSAAlgorithm.java:118)
at com.auth0.jwt.algorithms.ECDSAAlgorithm.sign(ECDSAAlgorithm.java:63)
... 6 more
你能帮忙解决这些错误并告诉我哪里出错了吗?
这里有一个简单示例,说明如何使用 ES256 获取签名的 JWT 令牌以及如何验证它:
import io.jsonwebtoken.Claims;
import io.jsonwebtoken.Jws;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import io.jsonwebtoken.security.Keys;
import java.security.KeyPair;
import java.time.LocalDate;
public class Jwt {
public static void main(String[] args) {
KeyPair keyPair = Keys.keyPairFor(SignatureAlgorithm.ES256);
//generate signed JWT token
String signedToken = Jwts.builder()
.setExpiration(java.sql.Date.valueOf(LocalDate.now().plusWeeks(2)))
.setSubject("your subject")
.setAudience("your audience")
.signWith(keyPair.getPrivate())
.compact();
//verify signed JWT token (no exceptions means check is OK)
Jws<Claims> claimsJws = Jwts.parser()
.setSigningKey(keyPair.getPublic())
.parseClaimsJws(signedToken);
}
}