Okta Java 创建 jwt 验证器抛出异常

Okta Java create jwt verifier throws exception

问题:

一直在关注文档以验证访问令牌,但文档中的代码似乎不太令人满意...


文档:

https://developer.okta.com/docs/guides/validate-access-tokens/java/overview/#decoding-and-validating-the-access-token.


代码:

AccessTokenVerifier jwtVerifier = JwtVerifiers.accessTokenVerifierBuilder()
    .setIssuer("https://${yourOktaDomain}/oauth2/default")
    .setAudience("api://default")                // defaults to 'api://default'
    .setConnectionTimeout(Duration.ofSeconds(1)) // defaults to 1s
    .build();

错误:

结果

Method threw 'org.codehaus.groovy.runtime.InvokerInvocationException' exception.

原因

java.lang.NoClassDefFoundError: Could not initialize class 
okhttp3.internal.concurrent.TaskRunner

堆栈跟踪


java.lang.reflect.InvocationTargetException: null
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.grails.core.DefaultGrailsControllerClass$ReflectionInvoker.invoke(DefaultGrailsControllerClass.java:211)
    at org.grails.core.DefaultGrailsControllerClass.invoke(DefaultGrailsControllerClass.java:188)
    at org.grails.web.mapping.mvc.UrlMappingsInfoHandlerAdapter.handle(UrlMappingsInfoHandlerAdapter.groovy:90)
    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:1040)
    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:943)
    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1006)
    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:898)
    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:883)
    at com.juralio.auth.oauth.ThirdPartyJWTFilter.doFilter(ThirdPartyJWTFilter.groovy:33)
    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:209)
    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:178)
    at org.grails.web.servlet.mvc.GrailsWebRequestFilter.doFilterInternal(GrailsWebRequestFilter.java:77)
    at org.grails.web.filters.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:67)
    at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1128)
    at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:628)
    at java.base/java.lang.Thread.run(Thread.java:829)
Caused by: java.lang.NoClassDefFoundError: Could not initialize class okhttp3.internal.concurrent.TaskRunner
    at okhttp3.ConnectionPool.<init>(ConnectionPool.kt:41)
    at okhttp3.ConnectionPool.<init>(ConnectionPool.kt:47)
    at okhttp3.OkHttpClient$Builder.<init>(OkHttpClient.kt:471)
    at com.okta.commons.http.okhttp.OkHttpRequestExecutor.createOkHttpClient(OkHttpRequestExecutor.java:70)
    at com.okta.commons.http.okhttp.OkHttpRequestExecutor.<init>(OkHttpRequestExecutor.java:58)
    at com.okta.commons.http.okhttp.OkHttpRequestExecutorFactory.create(OkHttpRequestExecutorFactory.java:32)
    at com.okta.jwt.impl.http.OktaCommonsHttpClient.createRequestExecutor(OktaCommonsHttpClient.java:67)
    at com.okta.jwt.impl.http.OktaCommonsHttpClient.<init>(OktaCommonsHttpClient.java:47)
    at com.okta.jwt.impl.jjwt.BaseVerifierBuilderSupport.httpClient(BaseVerifierBuilderSupport.java:171)
    at com.okta.jwt.impl.jjwt.BaseVerifierBuilderSupport.signingKeyResolver(BaseVerifierBuilderSupport.java:155)
    at com.okta.jwt.impl.jjwt.JjwtAccessTokenVerifierBuilder.build(JjwtAccessTokenVerifierBuilder.java:58)
    at com.okta.jwt.impl.jjwt.JjwtAccessTokenVerifierBuilder.build(JjwtAccessTokenVerifierBuilder.java:27)
    at com.juralio.auth.oauth.OktaResponseController.$tt__get(OktaResponseController.groovy:30)
    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/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at grails.gorm.transactions.GrailsTransactionTemplate.doInTransaction(GrailsTransactionTemplate.groovy:94)
    at org.springframework.transaction.support.TransactionTemplate.execute(TransactionTemplate.java:140)
    at grails.gorm.transactions.GrailsTransactionTemplate.execute(GrailsTransactionTemplate.groovy:91)
    ... 18 common frames omitted

欢迎任何想法,谢谢。

我自己解决了这个问题

添加了以下依赖项:

implementation 'com.okta.jwt:okta-jwt-verifier:0.5.1'
implementation 'com.okta.jwt:okta-jwt-verifier-impl:0.5.1'
implementation 'com.okta.jwt:okta-jwt-verifier-integration-tests:0.5.1'

implementation 'com.okta.commons:okta-config-check:1.2.5'
implementation 'com.okta.commons:okta-commons-lang:1.2.5'
implementation 'com.okta.commons:okta-http-api:1.2.5'
implementation 'com.okta.commons:okta-http-okhttp:1.2.5'

implementation 'com.squareup.okhttp3:okhttp:4.9.1'
testImplementation 'com.squareup.okhttp3:mockwebserver:4.9.1'

implementation 'org.jetbrains.kotlin:kotlin-stdlib:1.5.31'