我在MyEclipse下开发了一个在Tomcat9下运行的Jersey Java REST API。
这个API使用JWT令牌,我使用io.jsonwebtoken来管理使其工作所需的内容。
一切都很好,除了当我的API调用一个生成以下错误的函数时:
java.lang.NoSuchMethodError: 'int io.jsonwebtoken.SignatureAlgorithm.getMinKeyLength()'
at io.jsonwebtoken.security.Keys.hmacShaKeyFor(Keys.java:84)
at com.knowledgeplaces.metalmsapi.utils.JWTUtils.createJWT(JWTUtils.java:18)
这是JWTUtils的代码:
package com.knowledgeplaces.metalmsapi.utils;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.JwtException;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.WeakKeyException;
import java.security.Key;
import java.nio.charset.StandardCharsets;
import java.util.Calendar;
import java.util.Date;
import java.util.UUID;
public class JWTUtils {
public String createJWT(String issuer, Integer userId, String jwtSecretKey, Integer jwtExpirationMs) throws Exception {
Key key;
try {
key = Keys.hmacShaKeyFor(jwtSecretKey.getBytes(StandardCharsets.UTF_8));
} catch (WeakKeyException ex) {
throw new Exception("invalidKPLMSCustomerConfigMetaLmsApiKey");
}
这是pom.xml:
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.2</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.2</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is preferred -->
<version>0.11.2</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>
感谢您的帮助。
我删除了最后一个依赖项,现在它已经修复了。
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.9.1</version>
</dependency>