Skip to content

Commit bdb2e35

Browse files
authored
[Java Example] OAuth2 authentication supports Azure AD (#86)
1 parent 56242dc commit bdb2e35

File tree

5 files changed

+104
-11
lines changed

5 files changed

+104
-11
lines changed

cloud/java/pom.xml

Lines changed: 38 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,24 @@
3434
</repositories>
3535

3636
<properties>
37-
<pulsar.version>2.7.0-742fc5c9b</pulsar.version>
37+
<pulsar.version>2.8.1.5</pulsar.version>
38+
<log4j2.version>2.14.0</log4j2.version>
39+
<slf4j.version>1.7.25</slf4j.version>
3840
</properties>
3941

4042
<dependencies>
4143
<dependency>
42-
<groupId>org.apache.pulsar</groupId>
44+
<groupId>io.streamnative</groupId>
4345
<artifactId>pulsar-client</artifactId>
4446
<version>${pulsar.version}</version>
4547
</dependency>
4648
<dependency>
47-
<groupId>org.apache.pulsar</groupId>
49+
<groupId>io.streamnative</groupId>
4850
<artifactId>pulsar-client-admin</artifactId>
4951
<version>${pulsar.version}</version>
5052
</dependency>
5153
<dependency>
52-
<groupId>org.apache.pulsar</groupId>
54+
<groupId>io.streamnative</groupId>
5355
<artifactId>pulsar-client-original</artifactId>
5456
<version>${pulsar.version}</version>
5557
<type>pom</type>
@@ -60,6 +62,38 @@
6062
<artifactId>jcommander</artifactId>
6163
<version>1.29</version>
6264
</dependency>
65+
66+
<dependency>
67+
<groupId>org.slf4j</groupId>
68+
<artifactId>slf4j-api</artifactId>
69+
<version>${slf4j.version}</version>
70+
</dependency>
71+
72+
<dependency>
73+
<groupId>org.slf4j</groupId>
74+
<artifactId>slf4j-simple</artifactId>
75+
<version>${slf4j.version}</version>
76+
</dependency>
77+
78+
<dependency>
79+
<groupId>org.slf4j</groupId>
80+
<artifactId>jul-to-slf4j</artifactId>
81+
<version>${slf4j.version}</version>
82+
</dependency>
83+
84+
<dependency>
85+
<groupId>org.slf4j</groupId>
86+
<artifactId>jcl-over-slf4j</artifactId>
87+
<version>${slf4j.version}</version>
88+
</dependency>
89+
90+
<dependency>
91+
<groupId>org.apache.logging.log4j</groupId>
92+
<artifactId>log4j-bom</artifactId>
93+
<version>${log4j2.version}</version>
94+
<type>pom</type>
95+
<scope>import</scope>
96+
</dependency>
6397
</dependencies>
6498

6599
</project>

cloud/java/src/main/java/io/streamnative/examples/oauth2/JCommanderPulsar.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,6 @@
1919

2020
import com.beust.jcommander.Parameter;
2121

22-
import java.util.ArrayList;
23-
import java.util.List;
24-
2522
public class JCommanderPulsar {
2623
@Parameter(names = {"--issuerUrl"}, description = "issuerUrl is a named external system that provides identity and API access by issuing OAuth access tokens")
2724
String issuerUrl = "";
@@ -35,6 +32,9 @@ public class JCommanderPulsar {
3532
@Parameter(names = {"--audience"}, description = "audience is the address of the accessed service")
3633
String audience = "";
3734

35+
@Parameter(names = {"--scope"}, description = "OAuth2 scope")
36+
String scope = "";
37+
3838
@Parameter(names = "--help", help = true)
3939
boolean help;
4040
}

cloud/java/src/main/java/io/streamnative/examples/oauth2/SampleConsumer.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import com.beust.jcommander.JCommander;
2121

2222
import java.net.URL;
23+
import org.apache.commons.lang3.StringUtils;
2324
import org.apache.pulsar.client.api.*;
2425

2526
import org.apache.pulsar.client.impl.auth.oauth2.AuthenticationFactoryOAuth2;
@@ -35,10 +36,19 @@ public static void main(String[] args) throws Exception {
3536

3637
String topic = "persistent://public/default/topic-1";
3738

39+
Authentication authentication;
40+
41+
if (StringUtils.isEmpty(jct.scope)) {
42+
authentication = AuthenticationFactoryOAuth2.clientCredentials(
43+
new URL(jct.issuerUrl), new URL(jct.credentialsUrl), jct.audience);
44+
} else {
45+
authentication = AuthenticationFactoryOAuth2.clientCredentials(
46+
new URL(jct.issuerUrl), new URL(jct.credentialsUrl), jct.audience, jct.scope);
47+
}
48+
3849
PulsarClient client = PulsarClient.builder()
3950
.serviceUrl(jct.serviceUrl)
40-
.authentication(
41-
AuthenticationFactoryOAuth2.clientCredentials(new URL(jct.issuerUrl), new URL(jct.credentialsUrl), jct.audience))
51+
.authentication(authentication)
4252
.build();
4353

4454
Consumer<byte[]> consumer = client.newConsumer(Schema.BYTES)

cloud/java/src/main/java/io/streamnative/examples/oauth2/SampleProducer.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import com.beust.jcommander.JCommander;
2121

2222
import java.net.URL;
23+
import org.apache.commons.lang3.StringUtils;
24+
import org.apache.pulsar.client.api.Authentication;
2325
import org.apache.pulsar.client.api.Producer;
2426
import org.apache.pulsar.client.api.ProducerBuilder;
2527
import org.apache.pulsar.client.api.PulsarClient;
@@ -38,10 +40,19 @@ public static void main(String[] args) throws Exception {
3840

3941
String topic = "persistent://public/default/topic-1";
4042

43+
Authentication authentication;
44+
45+
if (StringUtils.isEmpty(jct.scope)) {
46+
authentication = AuthenticationFactoryOAuth2.clientCredentials(
47+
new URL(jct.issuerUrl), new URL(jct.credentialsUrl), jct.audience);
48+
} else {
49+
authentication = AuthenticationFactoryOAuth2.clientCredentials(
50+
new URL(jct.issuerUrl), new URL(jct.credentialsUrl), jct.audience, jct.scope);
51+
}
52+
4153
PulsarClient client = PulsarClient.builder()
4254
.serviceUrl(jct.serviceUrl)
43-
.authentication(
44-
AuthenticationFactoryOAuth2.clientCredentials(new URL(jct.issuerUrl), new URL(jct.credentialsUrl), jct.audience))
55+
.authentication(authentication)
4556
.build();
4657

4758
ProducerBuilder<byte[]> producerBuilder = client.newProducer().topic(topic)
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<!--
3+
4+
Licensed to the Apache Software Foundation (ASF) under one
5+
or more contributor license agreements. See the NOTICE file
6+
distributed with this work for additional information
7+
regarding copyright ownership. The ASF licenses this file
8+
to you under the Apache License, Version 2.0 (the
9+
"License"); you may not use this file except in compliance
10+
with the License. You may obtain a copy of the License at
11+
12+
http://www.apache.org/licenses/LICENSE-2.0
13+
14+
Unless required by applicable law or agreed to in writing,
15+
software distributed under the License is distributed on an
16+
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17+
KIND, either express or implied. See the License for the
18+
specific language governing permissions and limitations
19+
under the License.
20+
21+
-->
22+
<Configuration status="INFO">
23+
<Appenders>
24+
<Console name="Console" target="SYSTEM_OUT">
25+
<PatternLayout pattern="%d{ISO8601_OFFSET_DATE_TIME_HHMM} %-5level [%t{12}] %c{1.}@%L - %msg %X%n" />
26+
</Console>
27+
</Appenders>
28+
<Loggers>
29+
<Root level="info">
30+
<AppenderRef ref="Console" />
31+
</Root>
32+
<Logger name="org.eclipse.jetty" level="info"/>
33+
<Logger name="org.apache.pulsar" level="info"/>
34+
<Logger name="org.apache.bookkeeper" level="info"/>
35+
<Logger name="org.apache.kafka" level="info"/>
36+
<Logger name="org.testcontainers" level="info"/>
37+
</Loggers>
38+
</Configuration>

0 commit comments

Comments
 (0)