问题描述
在博文 “【Azure Developer】使用 Powershell az account get-access-token 命令获取Access Token (使用用户名+密码)” 和 “【Azure Developer】使用 Microsoft Authentication Libraries (MSAL) 如何来获取Token呢 (通过用户名和密码方式获取Access Token)” 中,使用了ADAL4J和MSAL4J 来获取Token,但是,当他们都在同一个Project中时,就出现了如下异常:
10:58:31.126 [ForkJoinPool.commonPool-worker-9] INFO com.microsoft.aad.msal4j.HttpHelper - [Correlation ID: null] Sent (null) Correlation Id is not same as received (null). 10:58:31.189 [ForkJoinPool.commonPool-worker-9] DEBUG com.microsoft.aad.msal4j.MexParser - found matching policy 10:58:31.191 [ForkJoinPool.commonPool-worker-9] DEBUG com.microsoft.aad.msal4j.MexParser - found matching policy 10:58:31.194 [ForkJoinPool.commonPool-worker-9] DEBUG com.microsoft.aad.msal4j.MexParser - Binding node did not match soap Action or Transport 10:58:31.196 [ForkJoinPool.commonPool-worker-9] DEBUG com.microsoft.aad.msal4j.MexParser - Found binding matching Action and Transport 10:58:31.386 [ForkJoinPool.commonPool-worker-9] INFO com.microsoft.aad.msal4j.HttpHelper - [Correlation ID: null] Sent (null) Correlation Id is not same as received (ed37821d-2750-473a-9fb0-14489663d03c). 10:58:31.420 [ForkJoinPool.commonPool-worker-9] INFO com.microsoft.aad.msal4j.WSTrustResponse - Found token of type: urn:oasis:names:tc:SAML:1.0:assertion 10:58:31.435 [ForkJoinPool.commonPool-worker-9] ERROR com.microsoft.aad.msal4j.PublicClientApplication - [Correlation ID: eaf1859c-8767-41b0-acb5-b930db79666e] Execution of class com.microsoft.aad.msal4j.AcquireTokenByAuthorizationGrantSupplier failed. java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to java.lang.String at com.nimbusds.oauth2.sdk.util.URLUtils.serializeParameters(URLUtils.java:105) at com.microsoft.aad.msal4j.TokenRequestExecutor.createOauthHttpRequest(TokenRequestExecutor.java:57) at com.microsoft.aad.msal4j.TokenRequestExecutor.executeTokenRequest(TokenRequestExecutor.java:35) at com.microsoft.aad.msal4j.AbstractClientApplicationBase.acquireTokenCommon(AbstractClientApplicationBase.java:128) at com.microsoft.aad.msal4j.AcquireTokenByAuthorizationGrantSupplier.execute(AcquireTokenByAuthorizationGrantSupplier.java:63) at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:59) at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:17) at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604) at java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1596) at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1067) at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1703) at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:172) Exception in thread "main" java.util.concurrent.CompletionException: java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to java.lang.String at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:89) at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:17) at java.util.concurrent.CompletableFuture$AsyncSupply.run(CompletableFuture.java:1604) at java.util.concurrent.CompletableFuture$AsyncSupply.exec(CompletableFuture.java:1596) at java.util.concurrent.ForkJoinTask.doExec(ForkJoinTask.java:289) at java.util.concurrent.ForkJoinPool$WorkQueue.runTask(ForkJoinPool.java:1067) at java.util.concurrent.ForkJoinPool.runWorker(ForkJoinPool.java:1703) at java.util.concurrent.ForkJoinWorkerThread.run(ForkJoinWorkerThread.java:172) Caused by: java.lang.ClassCastException: java.util.Collections$SingletonList cannot be cast to java.lang.String at com.nimbusds.oauth2.sdk.util.URLUtils.serializeParameters(URLUtils.java:105) at com.microsoft.aad.msal4j.TokenRequestExecutor.createOauthHttpRequest(TokenRequestExecutor.java:57) at com.microsoft.aad.msal4j.TokenRequestExecutor.executeTokenRequest(TokenRequestExecutor.java:35) at com.microsoft.aad.msal4j.AbstractClientApplicationBase.acquireTokenCommon(AbstractClientApplicationBase.java:128) at com.microsoft.aad.msal4j.AcquireTokenByAuthorizationGrantSupplier.execute(AcquireTokenByAuthorizationGrantSupplier.java:63) at com.microsoft.aad.msal4j.AuthenticationResultSupplier.get(AuthenticationResultSupplier.java:59) ... 7 more
如何来解决这个问题呢?
解决办法
就是不要把多种SDK混合在一起。就单独使用 com.microsoft.aad.msal4j 。
参考资料
【Azure Developer】使用 Powershell az account get-access-token 命令获取Access Token (使用用户名+密码)
【Azure Developer】使用 Microsoft Authentication Libraries (MSAL) 如何来获取Token呢 (通过用户名和密码方式获取Access Token)