简体   繁体   中英

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?

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 "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 :

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.

<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:

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.

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.
 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
 ~ $ java -version ~ $ mvn -v 
  1. Clone the current repository for the project adal4j .
 ~ $ git clone https://github.com/AzureAD/azure-activedirectory-library-for-java.git 
  1. Package the adal4j project.
 ~ $ 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.

So I think the issue might be caused by the noise in the environment variables like PATH whatever you used Windows or Others. Please try to clean your environment or use some IDEs like Eclipse for compiling.

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 . 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):

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). 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.

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. As a consequence an object O_2 of type C_2 cannot be assigned to a variable of type C_1; you get a ClassCastException instead.

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() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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