简体   繁体   English

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

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

I'm trying to build azure AD library for java , but I'm getting this error when I build, any idea why? 我正在尝试为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

EDIT: 编辑:

java -version output is : 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 output is : 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"

This was the output of mvn package , I edited the pom.xml file to add slf4j-simple dependency, and it fixed the NOP error in the output, but then I got the ClassCastException error. 这是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] ------------------------------------------------------------------------

When using the jar resulted from the build to run public-client-adal4j-sample I'm getting this error: 当使用构建的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)

I tried to reproduce the issue, but failed. 我试图重现这个问题,但失败了。 I cloned the adal4j repository from GitHub, and packaged it via command mvn package successful. 我从GitHub克隆了adal4j存储库,并通过命令mvn package成功打包。

Here is the steps within my environment. 这是我环境中的步骤。

OS: Ubuntu 14.04 LTS
Java SDK: JDK 1.8.0u51 64bit
Maven: 3.3.9
  1. Set up the envionments for JDK and Maven in the ~/.profile file. ~/.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. Check the JDK & Maven 检查JDK和Maven
 ~ $ java -version ~ $ mvn -v 
  1. Clone the current repository for the project adal4j . 克隆项目adal4j的当前存储库。
 ~ $ git clone https://github.com/AzureAD/azure-activedirectory-library-for-java.git 
  1. Package the adal4j project. 打包adal4j项目。
 ~ $ cd azure-activedirectory-library-for-java ~ $ mvn package 

And I tried to package it via import the exsiting Maven project option and export the jar file in Eclipse, it also works successfully. 我尝试通过导入现有的Maven项目选项打包它并在Eclipse中导出jar文件,它也能成功运行。

So I think the issue might be caused by the noise in the environment variables like PATH whatever you used Windows or Others. 因此,我认为问题可能是由于环境变量(如PATH的噪声引起的,无论您使用的是Windows还是其他。 Please try to clean your environment or use some IDEs like Eclipse for compiling. 请尝试清理您的环境或使用Eclipse之类的IDE进行编译。

Hope it helps. 希望能帮助到你。 Any concern, please feel free to let me know. 如有任何疑虑,请随时告诉我。

A quick internet search indeed makes one think that sun.net.www.protocol.https.HttpsURLConnectionImpl may be cast to javax.net.ssl.HttpsURLConnection . 快速的互联网搜索确实让人认为sun.net.www.protocol.https.HttpsURLConnectionImpl可以转换为javax.net.ssl.HttpsURLConnection So this looks like a class loader problem. 所以这看起来像一个类加载器问题。 Do you know more about the class loaders (and their relations) in use? 您对使用中的类加载器(及其关系)有更多了解吗?

Elaboration (as asked in a comment by the op): 详述(如op的评论中所述):

Every class in the jvm is identified by the triplet "class name", "package name" and its class loader (to be more precise the instance of the class loader used to load the class). jvm中的每个类都由三元组“类名”,“包名”及其类加载器(更准确地说是用于加载类的类加载器的实例)标识。 Every class loader is part of a class loader hierarchy (the special boot strap class loader being the root). 每个类加载器都是类加载器层次结构的一部分(特殊引导类加载器是根)。 A ClassLoader is able to load classes (ie return class objects modelling the class in question) itself or from an ancestor but not via class loaders being siblings or descendants. ClassLoader能够加载类(即返回对相关类建模的类对象)本身或来自祖先,但不能通过类加载器作为兄弟或后代。

Thus if you have two classes C_1 and C_2 having the same name and the same package name, but loaded from class loader siblings --- without going up the hierachy --- the classes are not being considered the same. 因此,如果你有两个类C_1和C_2具有相同的名称和相同的包名,但是从类加载器兄弟中加载---没有上升层次 - 这些类不被认为是相同的。 As a consequence an object O_2 of type C_2 cannot be assigned to a variable of type C_1; 因此,C_2类型的对象O_2不能分配给C_1类型的变量; you get a ClassCastException instead. 你会得到一个ClassCastException。

The same holds for interfaces and classes presumely implementing this interface, when assigning an object (being an instance of the class) to a variable of the interface type. 当将对象(作为类的实例)分配给接口类型的变量时,同样适用于假定实现此接口的接口和类。 If the class and the interface have been loaded by different class loaders, the assignment may fail. 如果类和接口已由不同的类加载器加载,则分配可能会失败。

This is the reason, why you ought to look at the class loaders you are using in your setup and which loader loads the interface and which one the class. 这就是为什么你应该查看你在设置中使用的类加载器以及哪个加载器加载接口以及哪个类加载的原因。 You may question the two by calling HttpsURLConnectionImpl.class.getClassLoader().toString() and HttpsURLConnection.class.getClassLoader().toString() . 你可以通过调用HttpsURLConnectionImpl.class.getClassLoader().toString()HttpsURLConnection.class.getClassLoader().toString()来质疑这两个。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM