我正在尝试使用ADAL与REST API进行通信。低于错误
Exception in thread "main" java.lang.VerifyError: Cannot inherit from final class
at java.lang.ClassLoader.defineClass1(Native Method)
at java.lang.ClassLoader.defineClass(ClassLoader.java:800)
at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142)
at java.net.URLClassLoader.defineClass(URLClassLoader.java:449)
at java.net.URLClassLoader.access$100(URLClassLoader.java:71)
at java.net.URLClassLoader$1.run(URLClassLoader.java:361)
at java.net.URLClassLoader$1.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at com.microsoft.aad.adal4j.AuthenticationContext.acquireToken(AuthenticationContext.java:382)
at edu.stanford.test.AccToken.main(AccToken.java:40)
使用的代码是:
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import com.microsoft.aad.adal4j.AsymmetricKeyCredential;
import com.microsoft.aad.adal4j.AuthenticationContext;
import com.microsoft.aad.adal4j.AuthenticationResult;
public class AcceToken {
public static void main(String[] args) {
String authority = "https://login.microsoftonline.com/tenant-id/oauth2/authorize";
ExecutorService service = null;
service = Executors.newFixedThreadPool(1);
try {
AuthenticationContext authenticationContext = new AuthenticationContext(
authority, false, service);
System.out.println("Authentication Context is "+ authenticationContext.getAuthority() );
String certfile = "D:\\Microsoft-tools\\Samples\\Final\\test.pfx";
InputStream pkcs12Certificate = new FileInputStream(certfile);
String token = "";
AsymmetricKeyCredential credential = AsymmetricKeyCredential
.create("id", pkcs12Certificate, "password");
System.out.println("X509 is fine!");
Future<AuthenticationResult> future = authenticationContext.acquireToken("https://outlook.office365.com",(AsymmetricKeyCredential) credential, null);
token = future.get().getAccessToken();
Long uuid = UUID.randomUUID().getMostSignificantBits();
URL url = new URL(
"https://outlook.office365.com/api/v1.0/emailaddress/folders/inbox/messages");
HttpURLConnection con = (HttpURLConnection) url.openConnection();
con.setRequestMethod("GET");
con.setRequestProperty("Accept", "application/json");
con.setRequestProperty("User-Agent", "Testing/1.0 abc/1.1");
Date date = new Date();
SimpleDateFormat ft = new SimpleDateFormat(
"E, dd MM yyyy hh:mm:ss zzz");
System.out.println("Current Date: " + ft.format(date));
String dateString = ft.format(date);
con.setRequestProperty("Authorization", "Bearer " + token);
if (con.getResponseCode() != 200) {
System.out.println(con.getHeaderFields());
throw new RuntimeException("Failed : HTTP error code : "
+ con.getResponseCode());
}
BufferedReader br = new BufferedReader(new InputStreamReader(
(con.getInputStream())));
String output;
System.out.println("Output from Server .... \n");
while ((output = br.readLine()) != null) {
System.out.println(output);
}
con.disconnect();
service.shutdown();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
我在下面的一行收到错误:
Future<AuthenticationResult> future = authenticationContext.acquireToken("https://outlook.office365.com",(AsymmetricKeyCredential) credential, null);
当我调查它时,我发现在AuthenticationContext.java的第382行。我们正在调用JwtHelper类的buildJwt方法,该方法正在调用扩展JWTClaimsSet类的AdalJWTClaimsSet类。
JWTClaimsSet类不再公开,现在是最终的。
我使用早期版本的JWT罐子绑,但我不断收到其他错误。
答案 0 :(得分:1)
ADAL依赖于oauth2-oidc-sdk 4.5版,它依赖于nimbus-jose-jwt版本3.1.2。它将JWTClaimsSet类作为公共类。您是否在pom.xml中覆盖了这些库的版本?您可以在https://github.com/AzureAD/azure-activedirectory-library-for-java/tree/master/src/samples处尝试示例,并查看adal4j按预期工作。