簡體   English   中英

如何在azure-activedirectory-library-for-java中修復ClassCastException?

[英]how can I fix ClassCastException in azure-activedirectory-library-for-java?

我正在嘗試為java構建azure AD庫 ,但是當我構建時我遇到了這個錯誤,任何想法為什么?

2218 [pool-1-thread-1] ERROR com.microsoft.aad.adal4j.AuthenticationContext - [Correlation ID: ac256c7c-b9ae-41ff-b39c-f0746a2275f7] Request to acquire token failed.
java.lang.ClassCastException: sun.net.www.protocol.https.HttpsURLConnectionImpl cannot be cast to javax.net.ssl.HttpsURLConnection
at com.microsoft.aad.adal4j.HttpHelper.openConnection(HttpHelper.java:102)
at com.microsoft.aad.adal4j.HttpHelper.openConnection(HttpHelper.java:115)
at com.microsoft.aad.adal4j.HttpHelper.executeHttpGet(HttpHelper.java:49)
at com.microsoft.aad.adal4j.AuthenticationAuthority.doDynamicInstanceDiscovery(AuthenticationAuthority.java:146)
at com.microsoft.aad.adal4j.AuthenticationAuthority.doInstanceDiscovery(AuthenticationAuthority.java:130)
at com.microsoft.aad.adal4j.AuthenticationContext.acquireTokenCommon(AuthenticationContext.java:813)
at com.microsoft.aad.adal4j.AuthenticationContext.access$1(AuthenticationContext.java:806)
at com.microsoft.aad.adal4j.AuthenticationContext$1.call(AuthenticationContext.java:175)
at com.microsoft.aad.adal4j.AuthenticationContext$1.call(AuthenticationContext.java:1)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)
Warning: Could not get charToByteConverterClass!
638 [main] INFO com.microsoft.aad.adal4j.WSTrustResponse - Found token of type: urn:oasis:names:tc:SAML:1.0:assertion

編輯:

java -version輸出是:

java version "1.8.0_91"
Java(TM) SE Runtime Environment (build 1.8.0_91-b14)
Java HotSpot(TM) 64-Bit Server VM (build 25.91-b14, mixed mode)

mvn -v輸出是:

Apache Maven 3.3.9 (bb52d8502b132ec0a5a3f4c09453c07478323dc5; 2015-11-10T18:41:47+02:00)
Maven home: /Users/vanddel/work/apache-maven-3.3.9
Java version: 1.8.0_91, vendor: Oracle Corporation
Java home: /Library/Java/JavaVirtualMachines/jdk1.8.0_91.jdk/Contents/Home/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "mac os x", version: "10.11.4", arch: "x86_64", family: "mac"

這是mvn package的輸出,我編輯了pom.xml文件來添加slf4j-simple依賴,並修復了輸出中的NOP錯誤,但后來我得到了ClassCastException錯誤。

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.6.4</version>
</dependency>




MacBook:azure-activedirectory-library-for-java vanddel$ mvn package
[INFO] Scanning for projects...
[INFO]                                                                         
[INFO] ------------------------------------------------------------------------
[INFO] Building adal4j 1.1.2
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-resources-plugin:2.6:resources (default-resources) @ adal4j ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] skip non existing resourceDirectory /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.0:compile (default-compile) @ adal4j ---
[INFO] Nothing to compile - all classes are up to date
[INFO] 
[INFO] --- maven-resources-plugin:2.6:testResources (default-testResources) @ adal4j ---
[INFO] Using 'UTF-8' encoding to copy filtered resources.
[INFO] Copying 5 resources
[INFO] 
[INFO] --- maven-compiler-plugin:3.0:testCompile (default-testCompile) @ adal4j ---
[INFO] Changes detected - recompiling the module!
[INFO] Compiling 16 source files to /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/test-classes
[INFO] 
[INFO] --- maven-surefire-plugin:2.10:test (default-test) @ adal4j ---
[INFO] Surefire report directory: /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/surefire-reports

-------------------------------------------------------
 T E S T S
-------------------------------------------------------
Running TestSuite
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".
SLF4J: Defaulting to no-operation (NOP) logger implementation
SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.
Warning: Could not get charToByteConverterClass!
Tests run: 65, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 8.032 sec

Results :

Tests run: 65, Failures: 0, Errors: 0, Skipped: 0

[INFO] 
[INFO] --- maven-jar-plugin:2.5:jar (default-jar) @ adal4j ---
[INFO] 
[INFO] --- maven-javadoc-plugin:2.9.1:jar (attach-javadocs) @ adal4j ---
[INFO] 
Loading source files for package com.microsoft.aad.adal4j...
Constructing Javadoc information...
Standard Doclet version 1.8.0_91
Building tree for all the packages and classes...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/AsymmetricKeyCredential.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/AuthenticationCallback.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/AuthenticationContext.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/AuthenticationException.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/AuthenticationResult.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/ClientAssertion.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/ClientCredential.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/NamespaceContextImpl.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/UserInfo.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/package-frame.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/package-summary.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/package-tree.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/constant-values.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/serialized-form.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/class-use/UserInfo.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/class-use/NamespaceContextImpl.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/class-use/ClientCredential.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/class-use/ClientAssertion.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/class-use/AuthenticationResult.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/class-use/AuthenticationException.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/class-use/AuthenticationContext.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/class-use/AuthenticationCallback.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/class-use/AsymmetricKeyCredential.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/com/microsoft/aad/adal4j/package-use.html...
Building index for all the packages and classes...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/overview-tree.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/index-all.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/deprecated-list.html...
Building index for all classes...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/allclasses-frame.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/allclasses-noframe.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/index.html...
Generating /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/apidocs/help-doc.html...
14 warnings
[WARNING] Javadoc Warnings
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AsymmetricKeyCredential.java:95: warning: no description for @throws
[WARNING] * @throws CertificateEncodingException
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AsymmetricKeyCredential.java:96: warning: no description for @throws
[WARNING] * @throws NoSuchAlgorithmException
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AsymmetricKeyCredential.java:108: warning: no description for @throws
[WARNING] * @throws CertificateEncodingException
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AsymmetricKeyCredential.java:109: warning: no description for @throws
[WARNING] * @throws NoSuchAlgorithmException
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AsymmetricKeyCredential.java:136: warning: no description for @throws
[WARNING] * @throws KeyStoreException
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AsymmetricKeyCredential.java:137: warning: no description for @throws
[WARNING] * @throws NoSuchProviderException
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AsymmetricKeyCredential.java:138: warning: no description for @throws
[WARNING] * @throws NoSuchAlgorithmException
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AsymmetricKeyCredential.java:139: warning: no description for @throws
[WARNING] * @throws CertificateException
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AsymmetricKeyCredential.java:140: warning: no description for @throws
[WARNING] * @throws FileNotFoundException
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AsymmetricKeyCredential.java:141: warning: no description for @throws
[WARNING] * @throws IOException
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AsymmetricKeyCredential.java:142: warning: no description for @throws
[WARNING] * @throws UnrecoverableKeyException
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AuthenticationContext.java:145: warning: no description for @param
[WARNING] * @param sslSocketFactory
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AuthenticationContext.java:310: warning: no description for @throws
[WARNING] * @throws AuthenticationException
[WARNING] ^
[WARNING] /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/src/main/java/com/microsoft/aad/adal4j/AuthenticationContext.java:376: warning: no description for @throws
[WARNING] * @throws AuthenticationException
[WARNING] ^
[INFO] Building jar: /Users/vanddel/Documents/workspace/azure-activedirectory-library-for-java/target/adal4j-1.1.2-javadoc.jar
[INFO] 
[INFO] >>> maven-source-plugin:2.2.1:jar (attach-sources) > generate-sources @ adal4j >>>
[INFO] 
[INFO] <<< maven-source-plugin:2.2.1:jar (attach-sources) < generate-sources @ adal4j <<<
[INFO] 
[INFO] --- maven-source-plugin:2.2.1:jar (attach-sources) @ adal4j ---
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time: 27.151 s
[INFO] Finished at: 2016-05-16T16:39:49+02:00
[INFO] Final Memory: 25M/185M
[INFO] ------------------------------------------------------------------------

當使用構建的jar來運行public-client-adal4j-sample時,我收到此錯誤:

com.microsoft.aad.adal4j.AuthenticationException: {"error_description":"AADSTS50034: To sign into this application the account must be added to the windows.net directory.\r\nTrace ID: 408f33c8-0716-476f-916c-7b5fbdca40c2\r\nCorrelation ID: ef2af04b-23a2-4f14-b001-d952374d11f8\r\nTimestamp: 2016-05-17 09:46:10Z","error":"invalid_grant"}
    at com.microsoft.aad.adal4j.AdalTokenRequest.executeOAuthRequestAndProcessResponse(AdalTokenRequest.java:108)
    at com.microsoft.aad.adal4j.AuthenticationContext.acquireTokenCommon(AuthenticationContext.java:819)
    at com.microsoft.aad.adal4j.AuthenticationContext.access$100(AuthenticationContext.java:66)
    at com.microsoft.aad.adal4j.AuthenticationContext$1.call(AuthenticationContext.java:174)
    at com.microsoft.aad.adal4j.AuthenticationContext$1.call(AuthenticationContext.java:163)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

我試圖重現這個問題,但失敗了。 我從GitHub克隆了adal4j存儲庫,並通過命令mvn package成功打包。

這是我環境中的步驟。

OS: Ubuntu 14.04 LTS
Java SDK: JDK 1.8.0u51 64bit
Maven: 3.3.9
  1. ~/.profile文件中設置JDK和Maven的環境。
 export JAVA_HOME=~/<my-sdk-path>/jdk1.8.0_51 export CLASSPATH=.:$JAVA_HOME/lib export PATH=$JAVA_HOME/bin:$PATH export M2_HOME=~/<my-sdk-path>/apache-maven-3.3.9 export PATH=$PATH:$M2_HOME/bin 
  1. 檢查JDK和Maven
 ~ $ java -version ~ $ mvn -v 
  1. 克隆項目adal4j的當前存儲庫。
 ~ $ git clone https://github.com/AzureAD/azure-activedirectory-library-for-java.git 
  1. 打包adal4j項目。
 ~ $ cd azure-activedirectory-library-for-java ~ $ mvn package 

我嘗試通過導入現有的Maven項目選項打包它並在Eclipse中導出jar文件,它也能成功運行。

因此,我認為問題可能是由於環境變量(如PATH的噪聲引起的,無論您使用的是Windows還是其他。 請嘗試清理您的環境或使用Eclipse之類的IDE進行編譯。

希望能幫助到你。 如有任何疑慮,請隨時告訴我。

快速的互聯網搜索確實讓人認為sun.net.www.protocol.https.HttpsURLConnectionImpl可以轉換為javax.net.ssl.HttpsURLConnection 所以這看起來像一個類加載器問題。 您對使用中的類加載器(及其關系)有更多了解嗎?

詳述(如op的評論中所述):

jvm中的每個類都由三元組“類名”,“包名”及其類加載器(更准確地說是用於加載類的類加載器的實例)標識。 每個類加載器都是類加載器層次結構的一部分(特殊引導類加載器是根)。 ClassLoader能夠加載類(即返回對相關類建模的類對象)本身或來自祖先,但不能通過類加載器作為兄弟或后代。

因此,如果你有兩個類C_1和C_2具有相同的名稱和相同的包名,但是從類加載器兄弟中加載---沒有上升層次 - 這些類不被認為是相同的。 因此,C_2類型的對象O_2不能分配給C_1類型的變量; 你會得到一個ClassCastException。

當將對象(作為類的實例)分配給接口類型的變量時,同樣適用於假定實現此接口的接口和類。 如果類和接口已由不同的類加載器加載,則分配可能會失敗。

這就是為什么你應該查看你在設置中使用的類加載器以及哪個加載器加載接口以及哪個類加載的原因。 你可以通過調用HttpsURLConnectionImpl.class.getClassLoader().toString()HttpsURLConnection.class.getClassLoader().toString()來質疑這兩個。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM