com.auth0.jwt.exceptions.SignatureVerificationException错误解决

本文探讨了在使用Shiro和JWT时遇到的SignatureVerificationException,焦点在于解密秘钥的使用不当。解决办法是确保使用数据库中加密后的秘钥进行签发和验证。同时,作者借此机会批评了一些收费服务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

com.auth0.jwt.exceptions.SignatureVerificationException报错解决

使用Shiro+jwt验签时报这个错误

com.auth0.jwt.exceptions.SignatureVerificationException: The Token's Signature resulted invalid when verified using the Algorithm: HmacSHA512

其实就是签发签名的secret是未加密的,在签发加密和验签解密的时候会导致字节流不一致,报签名错误。只要使用加密后的screct去签发验签就好了。

比如用前端传的密码(未加密)签发加密和验签解密,ro为前端请求传过来的json对象,这时候就会报错。

String token = JwtUtil.sign(userContext, ro.getPassword());

改成加密过后的密码就可以了(数据库存的)

String token = JwtUtil.sign(userContext, user.getPassword());

顺便Diss下那些收费的。

### Java 中使用 com.auth0JWT 库 #### 使用概述 `com.auth0.jwt` 是一个用于创建和验证 JSON Web Tokens (JWTs) 的流行库。它提供了简单易用的 API 来处理签名、加密以及令牌的有效性验证[^1]。 以下是关于如何在 Java 项目中集成并使用 `com.auth0.jwt` 库的一些核心功能: --- #### 添加依赖项 如果正在使用 Maven 构建工具,则可以在项目的 `pom.xml` 文件中添加以下依赖关系来引入该库: ```xml <dependency> <groupId>com.auth0</groupId> <artifactId>java-jwt</artifactId> <version>4.2.1</version> </dependency> ``` 对于 Gradle 用户,可以将下面的内容加入到构建文件中: ```gradle implementation 'com.auth0:java-jwt:4.2.1' ``` --- #### 创建 JWT Token 通过指定算法(如 HMAC 或 RSA),可以轻松生成带有有效负载数据的 JWT 字符串。例如,利用 HmacSHA256 算法实现如下代码片段所示的功能: ```java import com.auth0.jwt.JWT; import com.auth0.jwt.algorithms.Algorithm; public class JwtExample { public static void main(String[] args) throws Exception { String secret = "mySecretKey"; Algorithm algorithm = Algorithm.HMAC256(secret); // Create the JWT string. String token = JWT.create() .withIssuer("auth0") // Set issuer claim value as auth0 .withClaim("userId", 123) // Add custom claims such as userId here .sign(algorithm); System.out.println(token); } } ``` 上述程序会打印出由特定密钥签署的一个标准格式化的 JWT 字符串。 --- #### 验证 JWT Token 为了确认接收到的 JWT 是否合法且未被篡改,可以通过相同的秘密或者公私钥对来进行解码与校验操作。 ```java import com.auth0.jwt.exceptions.TokenExpiredException; import com.auth0.jwt.interfaces.DecodedJWT; // Verify and decode the received token using same algorithm instance used during creation phase. DecodedJWT decodedJwt = null; try{ decodedJwt = JWT.require(Algorithm.HMAC256(secret)) .withIssuer("auth0") .build() .verify(token); } catch(TokenExpiredException e){ // Handle expired tokens appropriately... } if(decodedJwt !=null ){ int userIdFromToken = decodedJwt.getClaim("userId").asInt(); System.out.printf("User ID extracted from token:%d%n", userIdFromToken ); } ``` 此部分展示了如何解析已签发的令牌,并从中提取有用的信息,比如用户的唯一标识号等。 --- #### 处理过期时间及其他声明 除了基本的身份认证外,还可以设置额外的安全特性,像定义最大存活期限(TTL),从而增强系统的安全性。 ```java String tokenWithExpiry = JWT.create() .withExpiresAt(new Date(System.currentTimeMillis()+ TimeUnit.MINUTES.toMillis(1))) // Expire after one minute ... ; ``` 当尝试访问超出有效期范围内的资源时,应该捕获异常并返回相应的错误消息给客户端提示重新登录或其他适当措施。 ---
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值