java.lang.NoClassDefFoundError: io/jsonwebtoken/SignatureAlgorithm
Posted
技术标签:
【中文标题】java.lang.NoClassDefFoundError: io/jsonwebtoken/SignatureAlgorithm【英文标题】: 【发布时间】:2018-01-05 21:29:06 【问题描述】:我有以下代码尝试创建 jwt 令牌。每当它尝试执行 SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256; 时总会出现如下所示的错误
我已经手动删除了 jwt jar 文件并再次更新 maven 以重新安装 jar,但问题仍然存在。
任何帮助将不胜感激!
//Sample method to construct a JWT
public static String createJWT(String id, String issuer, String subject,
String payload, String role, String name, long ttlMillis)
String jwt = "";
try
//The JWT signature algorithm we will be using to sign the token
SignatureAlgorithm signatureAlgorithm = SignatureAlgorithm.HS256;
long nowMillis = System.currentTimeMillis();
Date now = new Date(nowMillis);
//We will sign our JWT with our ApiKey secret
byte[] apiKeySecretBytes = DatatypeConverter.parseBase64Binary(apiKey.getSecret());
Key signingKey = new SecretKeySpec(apiKeySecretBytes, signatureAlgorithm.getJcaName());
//Let's set the JWT Claims
JwtBuilder builder = Jwts.builder()
.setIssuedAt(now)
.setSubject("test")
.claim("role", role)
.claim("name", name)
.setIssuer("tester")
.signWith(io.jsonwebtoken.SignatureAlgorithm.HS256, signingKey);
//.signWith(signatureAlgorithm, apiKey.getSecret().getBytes("UTF-8"));
//
//if it has been specified, let's add the expiration
if (ttlMillis >= 0)
long expMillis = nowMillis + ttlMillis;
Date exp = new Date(expMillis);
builder.setExpiration(exp);
//Builds the JWT and serializes it to a compact, URL-safe string
jwt = builder.compact();
catch (Exception ex)
return jwt;
错误日志
在 com.test.service.JWTToken.createJWT(JWTToken.java:21) 在 com.test.controller.AccountController.authenticateUser(AccountController.java:82) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) 在 sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 在 java.lang.reflect.Method.invoke(Method.java:498) 在 org.glassfish.jersey.server.internal.routing.SubResourceLocatorRouter$1.run(SubResourceLocatorRouter.java:158) 在 org.glassfish.jersey.server.internal.routing.SubResourceLocatorRouter.getResource(SubResourceLocatorRouter.java:178) 在 org.glassfish.jersey.server.internal.routing.SubResourceLocatorRouter.apply(SubResourceLocatorRouter.java:109) 在 org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:109) 在 org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) 在 org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) 在 org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) 在 org.glassfish.jersey.server.internal.routing.RoutingStage._apply(RoutingStage.java:112) 在 org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:92) 在 org.glassfish.jersey.server.internal.routing.RoutingStage.apply(RoutingStage.java:61) 在 org.glassfish.jersey.process.internal.Stages.process(Stages.java:197) 在 org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:301) 在 org.glassfish.jersey.internal.Errors$1.call(Errors.java:271) 在 org.glassfish.jersey.internal.Errors$1.call(Errors.java:267) 在 org.glassfish.jersey.internal.Errors.process(Errors.java:315) 在 org.glassfish.jersey.internal.Errors.process(Errors.java:297) 在 org.glassfish.jersey.internal.Errors.process(Errors.java:267) 在 org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:317) 在 org.glassfish.jersey.server.ServerRuntime.process(ServerRuntime.java:292) 在 org.glassfish.jersey.server.ApplicationHandler.handle(ApplicationHandler.java:1139) 在 org.glassfish.jersey.servlet.WebComponent.service(WebComponent.java:460) 在 org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:386) 在 org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:334) 在 org.glassfish.jersey.servlet.ServletContainer.service(ServletContainer.java:221) 在 org.apache.catalina.core.StandardWrapper.service(StandardWrapper.java:1682) 在 org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:318) 在 org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:160) 在 org.apache.catalina.core.StandardPipeline.doInvoke(StandardPipeline.java:734) 在 org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:673) 在 com.sun.enterprise.web.WebPipeline.invoke(WebPipeline.java:99) 在 org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:174) 在 org.apache.catalina.connector.CoyoteAdapter.doService(CoyoteAdapter.java:416) 在 org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:283) 在 com.sun.enterprise.v3.services.impl.ContainerMapper$HttpHandlerCallable.call(ContainerMapper.java:459) 在 com.sun.enterprise.v3.services.impl.ContainerMapper.service(ContainerMapper.java:167) 在 org.glassfish.grizzly.http.server.HttpHandler.runService(HttpHandler.java:206) 在 org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:180) 在 org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:235) 在 org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:283) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:132) 在 org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:111) 在 org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:77) 在 org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:536) 在 org.glassfish.grizzly.strategies.Abstractiostrategy.fireIOEvent(AbstractIOStrategy.java:112) 在 org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:117) 在 org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:56) 在 org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:137) 在 org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:591) 在 org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:571) 在 java.lang.Thread.run(Thread.java:745)
POM 依赖项
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.8</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-client</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.25</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20160810</version>
</dependency>
<dependency>
<groupId>commons-beanutils</groupId>
<artifactId>commons-beanutils</artifactId>
<version>1.9.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.jaxrs</groupId>
<artifactId>jackson-jaxrs-json-provider</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.8.9</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-json-jackson</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.eclipse.persistence</groupId>
<artifactId>org.eclipse.persistence.moxy</artifactId>
<version>2.6.4</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
<version>2.22.1</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>0.7.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.42</version>
</dependency>
</dependencies>
【问题讨论】:
【参考方案1】:NoClassDefFoundError
是运行时问题。尝试在您的 manifest
文件中添加 jar
条目,这应该可以帮助您解决问题。
【讨论】:
@pothios 你好,我也遇到了同样的问题。你能分享一下你在清单文件中添加了什么吗?【参考方案2】:我已经使用 gradle 解决了这个问题
implementation "io.jsonwebtoken:jjwt-api:0.11.0"
implementation "io.jsonwebtoken:jjwt-jackson:0.11.0", exclude group:'io.jsonwebtoken', module:'jjwt-api'
implementation "io.jsonwebtoken:jjwt-impl:0.11.0", exclude group:'io.jsonwebtoken', module:'jjwt-api'
【讨论】:
【参考方案3】:请将此添加到您的 pom.xml 中:
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.2</version>
<scope>runtime</scope>
</dependency>
【讨论】:
请解释此依赖项的作用以及它将如何帮助解决问题。以上是关于java.lang.NoClassDefFoundError: io/jsonwebtoken/SignatureAlgorithm的主要内容,如果未能解决你的问题,请参考以下文章