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

时间:2016-05-15 09:55:45

标签: java eclipse azure azure-active-directory adal

我正在尝试构建azure AD library for java,但我在构建时遇到此错误,知道为什么?

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)

2 个答案:

答案 0 :(得分:2)

我试图重现这个问题,但失败了。我从GitHub克隆了adal4j存储库,并通过命令mvn package成功打包它。

以下是我的环境中的步骤。

OS: Ubuntu 14.04 LTS
Java SDK: JDK 1.8.0u51 64bit
Maven: 3.3.9
  1. ~/.profile文件中设置JDK和Maven的环境。
  2. 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&amp;行家
    2. ~ $ java -version
      ~ $ mvn -v
      
      1. 克隆项目adal4j的当前存储库。
      2. ~ $ git clone https://github.com/AzureAD/azure-activedirectory-library-for-java.git
        
        1. 打包adal4j项目。
        2. ~ $ cd azure-activedirectory-library-for-java
          ~ $ mvn package
          

          我尝试通过导入现有的Maven项目选项打包它并在Eclipse中导出jar文件,它也能成功运行。

          所以我认为这个问题可能是由环境变量中的噪音引起的,例如PATH无论你使用Windows还是其他人。请尝试清理您的环境或使用Eclipse之类的IDE进行编译。

          希望它有所帮助。如有任何疑虑,请随时告诉我。

答案 1 :(得分:1)

快速互联网搜索确实让人认为sun.net.www.protocol.https.HttpsURLConnectionImpl可以投放到javax.net.ssl.HttpsURLConnection。所以这看起来像一个类加载器问题。 您对使用中的类加载器(及其关系)有更多了解吗?

详细说明(如op的评论中所述):

jvm中的每个类都由三元组&#34;类名&#34;,&#34;包名&#34;标识。及其类加载器(更准确地说是用于加载类的类加载器的实例)。每个类加载器都是类加载器层次结构的一部分(特殊引导类加载器是根)。 ClassLoader能够加载类(即返回对相关类建模的类对象)本身或来自祖先,但不能通过类加载器作为兄弟或后代加载。

因此,如果你有两个类C_1和C_2具有相同的名称和相同的包名,但是从类加载器兄弟中加载---没有上升层次 - 类不被认为是相同的。因此,C_2类型的对象O_2不能分配给C_1类型的变量;你会得到一个ClassCastException。

在将对象(作为类的实例)分配给接口类型的变量时,同样适用于假定实现此接口的接口和类。如果类和接口已被不同的类加载器加载,则赋值可能会失败。

这就是为什么你应该查看你在设置中使用的类加载器以及哪个加载器加载接口以及哪个类加载的原因。您可以通过致电HttpsURLConnectionImpl.class.getClassLoader().toString()HttpsURLConnection.class.getClassLoader().toString()来解答这两个问题。