问题描述
应用程序类型:使用客户端凭据流的 Java Spring Boot 守护程序应用程序.
我之前使用 microsoft-graph 2.10.0 从 Microsoft graph 中获取数据.但是在最近的 microsoft-graph 3.0.0 中,我想更新该项目.
I was earlier using microsoft-graph 2.10.0 for fetching data from Microsoft graph. However with the recent microsoft-graph 3.0.0, I wanted to update the project.
遵循升级 和 auth 详细信息,我使用以下内容使用 ClientCredentials azure-identity 获取 GraphServiceClient:
Following the upgrade and auth details, i have used the below to get the GraphServiceClient using ClientCredentials azure-identity:
String proxyUrl = "xxxxx";
int proxyPort = 8080;
ProxyOptions proxyOptions = new ProxyOptions(ProxyOptions.Type.HTTP,new InetSocketAddress(proxyUrl, proxyPort));
// proxyOptions.setCredentials(proxyUser, proxyPassword);
final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId(this.clientId).clientSecret(this.clientSecret).tenantId(this.tenantId)
.httpClient(HttpClient.createDefault(new HttpClientOptions().setProxyOptions(proxyOptions))).build();
final TokenCredentialAuthProvider authProvider = new TokenCredentialAuthProvider(this.scopes, clientSecretCredential);
final GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(authProvider)
.buildClient();
pom.xml
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!-- exclude logback , add log4j2 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<!-- Microsoft Graph -->
<dependency>
<groupId>com.microsoft.graph</groupId>
<artifactId>microsoft-graph</artifactId>
<version>3.0.0</version>
</dependency>
<!-- For SMTP Email -->
<dependency>
<groupId>javax.mail</groupId>
<artifactId>mail</artifactId>
<version>1.4.7</version>
</dependency>
<dependency>
<groupId>javax.activation</groupId>
<artifactId>activation</artifactId>
<version>1.1.1</version>
<scope>runtime</scope>
</dependency>
<!-- SQL -->
<dependency>
<groupId>com.microsoft.sqlserver</groupId>
<artifactId>mssql-jdbc</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-configuration-processor</artifactId>
<optional>true</optional>
</dependency>
</dependencies>
注意:使用 2.10.0 我曾经使用 JVM 参数传递代理,但是使用 3.0.0 同样不起作用,因此最终使用 链接.
Note : Using 2.10.0 i used to pass the Proxy using JVM arguments, however the same did not work using 3.0.0 hence ended up using the proxyOptions following link.
问题:在获取任何数据时收到以下错误,在上述代码中设置 httpClient 时会看到该错误.
Issue : Receiving the below error while fetching any data, the error is seen while setting the httpClient in the above code.
2021-03-22T16:04:18,295 ERROR [restartedMain] o.s.b.SpringApplication: Application run failed
java.lang.BootstrapMethodError: java.lang.NoClassDefFoundError: reactor/netty/tcp/ProxyProvider$TypeSpec
at com.azure.core.http.netty.NettyAsyncHttpClientBuilder.lambda$build$7(NettyAsyncHttpClientBuilder.java:142)
at reactor.netty.http.client.HttpClient.tcpConfiguration(HttpClient.java:1343)
at com.azure.core.http.netty.NettyAsyncHttpClientBuilder.build(NettyAsyncHttpClientBuilder.java:122)
at com.azure.core.http.netty.NettyAsyncHttpClientProvider.createInstance(NettyAsyncHttpClientProvider.java:32)
at com.azure.core.implementation.http.HttpClientProviders.createInstance(HttpClientProviders.java:48)
at com.azure.core.http.HttpClient.createDefault(HttpClient.java:50)
at com.app.intune.config.GraphClient.getGraphServiceClient(GraphClient.java:48)
at com.app.intune.util.UserUtil.getUsersWithRegisteredDevicesAndSave(UserUtil.java:140)
at com.app.intune.IntuneApplication.run(IntuneApplication.java:61)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:333)
at com.app.intune.IntuneApplication.main(IntuneApplication.java:56)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.NoClassDefFoundError: reactor/netty/tcp/ProxyProvider$TypeSpec
... 18 more
Caused by: java.lang.ClassNotFoundException: reactor.netty.tcp.ProxyProvider$TypeSpec
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 18 more
对上述任何帮助将不胜感激.
Any help on the above will be greatly appreciated.
更新 1:似乎 Spring Boot Dependency 正在管理 okhttp3.version.将属性更新为以下后,错误已解决.
Update 1:Seems the Spring Boot Dependency was managing okhttp3.version. After updating the property to below, the error was resolved.
<properties>
<java.version>1.8</java.version>
<okhttp3.version>4.9.1</okhttp3.version>
</properties>
但是现在有新的错误:
java.lang.NoClassDefFoundError: reactor/netty/channel/BootstrapHandlers
at com.azure.core.http.netty.NettyAsyncHttpClientBuilder.lambda$build$4(NettyAsyncHttpClientBuilder.java:139)
at reactor.netty.tcp.TcpClient.bootstrap(TcpClient.java:144)
at com.azure.core.http.netty.NettyAsyncHttpClientBuilder.lambda$build$7(NettyAsyncHttpClientBuilder.java:138)
at reactor.netty.http.client.HttpClient.tcpConfiguration(HttpClient.java:1343)
at com.azure.core.http.netty.NettyAsyncHttpClientBuilder.build(NettyAsyncHttpClientBuilder.java:122)
at com.app.intune.config.GraphClient.getGraphServiceClient(GraphClient.java:43)
at com.app.intune.util.UserUtil.getUsersWithRegisteredDevicesAndSave(UserUtil.java:140)
at com.app.intune.IntuneApplication.run(IntuneApplication.java:57)
at org.springframework.boot.SpringApplication.callRunner(SpringApplication.java:804)
at org.springframework.boot.SpringApplication.callRunners(SpringApplication.java:788)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:333)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1309)
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1298)
at com.app.intune.IntuneApplication.main(IntuneApplication.java:52)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at org.springframework.boot.devtools.restart.RestartLauncher.run(RestartLauncher.java:49)
Caused by: java.lang.ClassNotFoundException: reactor.netty.channel.BootstrapHandlers
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 19 more
推荐答案
对于任何面临问题的人,以下是解决方案:项目详情:
Spring Boot 2.4.4
微软图 3.0.0
For anyone facing the issue, below is a solution:Project Details:
Spring Boot 2.4.4
microsoft-graph 3.0.0
使用以下方法创建 GraphClient:
Create GraphClient using below:
final ClientSecretCredential clientSecretCredential = new ClientSecretCredentialBuilder()
.clientId(<clientId>)
.clientSecret(<clientSecret>)
.tenantId(<tenantId>)
.httpClient(new NettyAsyncHttpClientBuilder().proxy(
new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<proxyHost>", <proxyPort>))
.setCredentials("<proxyUser>","<proxyPassword>"))
.build())
.build();
final TokenCredentialAuthProvider tokenCredentialAuthProvider = new TokenCredentialAuthProvider(this.scopes, clientSecretCredential);
final GraphServiceClient graphClient = GraphServiceClient.builder().authenticationProvider(tokenCredentialAuthProvider).buildClient();
您可能会收到其他错误:
You might get additional errors as :
Exception in thread "main" java.lang.NoClassDefFoundError: reactor/netty/channel/BootstrapHandlers
at com.azure.core.http.netty.NettyAsyncHttpClientBuilder.lambda$build$4(NettyAsyncHttpClientBuilder.java:139)
at reactor.netty.tcp.TcpClient.bootstrap(TcpClient.java:187)
at com.azure.core.http.netty.NettyAsyncHttpClientBuilder.lambda$build$7(NettyAsyncHttpClientBuilder.java:138)
at reactor.netty.http.client.HttpClient.tcpConfiguration(HttpClient.java:1466)
at com.azure.core.http.netty.NettyAsyncHttpClientBuilder.build(NettyAsyncHttpClientBuilder.java:122)
at com.app.graph_test.App.main(App.java:43)
Caused by: java.lang.ClassNotFoundException: reactor.netty.channel.BootstrapHandlers
at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
... 6 more
出现上述问题是因为 microsoft-graph : 3.0.0 具有以下依赖树:
The above issues occurs as microsoft-graph : 3.0.0 has below dependency tree:
microsoft-graph : 3.0.0
- microsoft-graph-core : 2.0.0
- azure-identity :1.2.3
- azure-core-http-netty : 1.8.0
- reactor-netty : 0.9.15.RELEASE
Spring boot 2.4.4 有托管依赖,因此它包括以下版本:
Spring boot 2.4.4 has managed dependency, hence it includes the below versions :
reactor-core : 3.4.4
reactor-netty : 1.0.5
新版本的 reactor-netty 不适用于 microsoft-graph 3.0.因此,通过在 pom.xml 中添加以下内容手动覆盖它.
The new version of reactor-netty doesnt work for microsoft-graph 3.0. Hence manually override it by adding the below in pom.xml.
<dependency>
<groupId>io.projectreactor.netty</groupId>
<artifactId>reactor-netty</artifactId>
<version>0.9.15.RELEASE</version>
</dependency>
发布这个应该没有错误.
Post this there should be no errors.
这篇关于MS Graph (3.0) Java ClientCredentials 错误(java.lang.BootstrapMethodError:java.lang.NoClassDefFoundError:reactor/netty/tcp/ProxyProvider$TypeSpec)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!