无法在 Quarkus 应用程序中创建 JwtClaimsBuilder 实例
Unable to create instance of JwtClaimsBuilder in Quarkus application
我目前正在开发 Quarkus 应用程序。
我已经创建了一个身份验证服务,它将验证用户并创建 JWT 令牌并在身份验证成功后将其发送给客户端。
但目前,我在创建 JWT 令牌时遇到问题。对于令牌创建,我使用
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-jwt</artifactId>
</dependency>
请参考下面用于创建令牌的代码。
public static String generateTokenString(PrivateKey privateKey, String kid,
String jsonResName, Map<String, Long> timeClaims) throws Exception {
JwtClaimsBuilder claims = Jwt.claims(jsonResName);
long currentTimeInSecs = currentTimeInSecs();
long exp = timeClaims != null && timeClaims.containsKey(Claims.exp.name())
? timeClaims.get(Claims.exp.name()) : currentTimeInSecs + 300;
claims.issuedAt(currentTimeInSecs);
claims.claim(Claims.auth_time.name(), currentTimeInSecs);
claims.expiresAt(exp);
return claims.jws().signatureKeyId(kid).sign(privateKey);
}
执行上述方法后JwtClaimsBuilder claims = Jwt.claims(jsonResName);
抛出 NullPointerException,因为它无法解析 JWT 声明。
请参考以下日志
Caused by: io.smallrye.jwt.build.JwtException: Failure to parse the JWT claims:java.lang.NullPointerException
at io.smallrye.jwt.build.impl.JwtSigningUtils.parseJwtClaims(JwtSigningUtils.java:358)
at io.smallrye.jwt.build.impl.JwtClaimsBuilderImpl.parseJsonToClaims(JwtClaimsBuilderImpl.java:279)
at io.smallrye.jwt.build.impl.JwtClaimsBuilderImpl.<init>(JwtClaimsBuilderImpl.java:37)
at io.smallrye.jwt.build.impl.JwtProviderImpl.claims(JwtProviderImpl.java:35)
at io.smallrye.jwt.build.Jwt.claims(Jwt.java:74)
at com.keepnote.util.JwtUtil.generateTokenString(JwtUtil.java:33)
at com.keepnote.util.JwtUtil.generateTokenString(JwtUtil.java:28)
at com.keepnote.services.AuthenticationServiceImpl.generateToken(AuthenticationServiceImpl.java:59)
at com.keepnote.services.AuthenticationServiceImpl.login(AuthenticationServiceImpl.java:39)
at com.keepnote.services.AuthenticationServiceImpl_Subclass.login$$superaccessor16(AuthenticationServiceImpl_Subclass.zig:130)
at com.keepnote.services.AuthenticationServiceImpl_Subclass$$function$.apply(AuthenticationServiceImpl_Subclass$$function$.zig:33)
at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:119)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:92)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired.doIntercept(TransactionalInterceptorRequired.java:32)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.intercept(TransactionalInterceptorBase.java:53)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired.intercept(TransactionalInterceptorRequired.java:26)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired_Bean.intercept(TransactionalInterceptorRequired_Bean.zig:168)
at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:41)
at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:41)
at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:32)
at com.keepnote.services.AuthenticationServiceImpl_Subclass.login(AuthenticationServiceImpl_Subclass.zig:78)
at com.keepnote.resources.AuthenticationResource.login(AuthenticationResource.java:36)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:130)
at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:621)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:487)
at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget(ResourceMethodInvoker.java:437)
at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:362)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:439)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:400)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:374)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:67)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:488)
... 20 more
Caused by: java.lang.NullPointerException
at java.base/java.io.Reader.<init>(Reader.java:167)
at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at io.smallrye.jwt.build.impl.JwtSigningUtils.readJsonContent(JwtSigningUtils.java:280)
at io.smallrye.jwt.build.impl.JwtSigningUtils.parseJwtClaims(JwtSigningUtils.java:356)
... 57 more
正在传递的文件。
{
"iss": "https://quarkus.io/using-jwt-rbac",
"jti": "a-123",
"sub": "jdoe-using-jwt-rbac",
"upn": "chandanmishra3706@gmail.com",
"preferred_username": "Chandan",
"aud": "using-jwt-rbac",
"birthdate": "2001-07-13",
"roleMappings": {
"group1": "Group1MappedRole",
"group2": "Group2MappedRole"
},
"groups": [
"Echoer",
"Tester",
"Subscriber",
"group2"
]
}
这是 quarkus 开发模式中的错误。将在 1.5.0 中修复。另见此处:https://github.com/quarkusio/quarkus/issues/9135
我目前正在开发 Quarkus 应用程序。 我已经创建了一个身份验证服务,它将验证用户并创建 JWT 令牌并在身份验证成功后将其发送给客户端。
但目前,我在创建 JWT 令牌时遇到问题。对于令牌创建,我使用
<dependency>
<groupId>io.quarkus</groupId>
<artifactId>quarkus-smallrye-jwt</artifactId>
</dependency>
请参考下面用于创建令牌的代码。
public static String generateTokenString(PrivateKey privateKey, String kid,
String jsonResName, Map<String, Long> timeClaims) throws Exception {
JwtClaimsBuilder claims = Jwt.claims(jsonResName);
long currentTimeInSecs = currentTimeInSecs();
long exp = timeClaims != null && timeClaims.containsKey(Claims.exp.name())
? timeClaims.get(Claims.exp.name()) : currentTimeInSecs + 300;
claims.issuedAt(currentTimeInSecs);
claims.claim(Claims.auth_time.name(), currentTimeInSecs);
claims.expiresAt(exp);
return claims.jws().signatureKeyId(kid).sign(privateKey);
}
执行上述方法后JwtClaimsBuilder claims = Jwt.claims(jsonResName);
抛出 NullPointerException,因为它无法解析 JWT 声明。
请参考以下日志
Caused by: io.smallrye.jwt.build.JwtException: Failure to parse the JWT claims:java.lang.NullPointerException
at io.smallrye.jwt.build.impl.JwtSigningUtils.parseJwtClaims(JwtSigningUtils.java:358)
at io.smallrye.jwt.build.impl.JwtClaimsBuilderImpl.parseJsonToClaims(JwtClaimsBuilderImpl.java:279)
at io.smallrye.jwt.build.impl.JwtClaimsBuilderImpl.<init>(JwtClaimsBuilderImpl.java:37)
at io.smallrye.jwt.build.impl.JwtProviderImpl.claims(JwtProviderImpl.java:35)
at io.smallrye.jwt.build.Jwt.claims(Jwt.java:74)
at com.keepnote.util.JwtUtil.generateTokenString(JwtUtil.java:33)
at com.keepnote.util.JwtUtil.generateTokenString(JwtUtil.java:28)
at com.keepnote.services.AuthenticationServiceImpl.generateToken(AuthenticationServiceImpl.java:59)
at com.keepnote.services.AuthenticationServiceImpl.login(AuthenticationServiceImpl.java:39)
at com.keepnote.services.AuthenticationServiceImpl_Subclass.login$$superaccessor16(AuthenticationServiceImpl_Subclass.zig:130)
at com.keepnote.services.AuthenticationServiceImpl_Subclass$$function$.apply(AuthenticationServiceImpl_Subclass$$function$.zig:33)
at io.quarkus.arc.impl.AroundInvokeInvocationContext.proceed(AroundInvokeInvocationContext.java:54)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:119)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.invokeInOurTx(TransactionalInterceptorBase.java:92)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired.doIntercept(TransactionalInterceptorRequired.java:32)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorBase.intercept(TransactionalInterceptorBase.java:53)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired.intercept(TransactionalInterceptorRequired.java:26)
at io.quarkus.narayana.jta.runtime.interceptor.TransactionalInterceptorRequired_Bean.intercept(TransactionalInterceptorRequired_Bean.zig:168)
at io.quarkus.arc.impl.InterceptorInvocation.invoke(InterceptorInvocation.java:41)
at io.quarkus.arc.impl.AroundInvokeInvocationContext.perform(AroundInvokeInvocationContext.java:41)
at io.quarkus.arc.impl.InvocationContexts.performAroundInvoke(InvocationContexts.java:32)
at com.keepnote.services.AuthenticationServiceImpl_Subclass.login(AuthenticationServiceImpl_Subclass.zig:78)
at com.keepnote.resources.AuthenticationResource.login(AuthenticationResource.java:36)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.base/java.lang.reflect.Method.invoke(Method.java:566)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:167)
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:130)
at org.jboss.resteasy.core.ResourceMethodInvoker.internalInvokeOnTarget(ResourceMethodInvoker.java:621)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTargetAfterFilter(ResourceMethodInvoker.java:487)
at org.jboss.resteasy.core.ResourceMethodInvoker.lambda$invokeOnTarget(ResourceMethodInvoker.java:437)
at org.jboss.resteasy.core.interception.jaxrs.PreMatchContainerRequestContext.filter(PreMatchContainerRequestContext.java:362)
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:439)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:400)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:374)
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:67)
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:488)
... 20 more
Caused by: java.lang.NullPointerException
at java.base/java.io.Reader.<init>(Reader.java:167)
at java.base/java.io.InputStreamReader.<init>(InputStreamReader.java:72)
at io.smallrye.jwt.build.impl.JwtSigningUtils.readJsonContent(JwtSigningUtils.java:280)
at io.smallrye.jwt.build.impl.JwtSigningUtils.parseJwtClaims(JwtSigningUtils.java:356)
... 57 more
正在传递的文件。
{
"iss": "https://quarkus.io/using-jwt-rbac",
"jti": "a-123",
"sub": "jdoe-using-jwt-rbac",
"upn": "chandanmishra3706@gmail.com",
"preferred_username": "Chandan",
"aud": "using-jwt-rbac",
"birthdate": "2001-07-13",
"roleMappings": {
"group1": "Group1MappedRole",
"group2": "Group2MappedRole"
},
"groups": [
"Echoer",
"Tester",
"Subscriber",
"group2"
]
}
这是 quarkus 开发模式中的错误。将在 1.5.0 中修复。另见此处:https://github.com/quarkusio/quarkus/issues/9135