我有以下代码尝试创建一个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;
}
错误记录
java.lang.NoClassDefFoundError:io / jsonwebtoken / SignatureAlgorithm 在com.test.service.JWTToken.createJWT(JWTToken.java:21) at com.test.controller.AccountController.authenticateUser(AccountController.java:82) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) 在org.glassfish.jersey.server.internal.routing.SubResourceLocatorRouter $ 1.run(SubResourceLocatorRouter.java:158) at 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) at 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) at com.sun.enterprise.v3.services.impl.ContainerMapper $ HttpHandlerCallable.call(ContainerMapper.java:459) at 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>
答案 0 :(得分:2)
我已经通过使用
解决了该问题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'}
答案 1 :(得分:1)
NoClassDefFoundError
是运行时问题。尝试在jar
文件中添加manifest
条目,这可以帮助您解决问题。
答案 2 :(得分:0)
请将其添加到您的pom.xml中:
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.2</version>
<scope>runtime</scope>
</dependency>