Skip to content

Commit c8a7842

Browse files
committed
Examples: Move the JWT authentication example to a separate directory
1 parent cd8ae0a commit c8a7842

File tree

15 files changed

+273
-67
lines changed

15 files changed

+273
-67
lines changed

β€Žexamples/BUILD.bazelβ€Ž

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -99,24 +99,6 @@ java_binary(
9999
],
100100
)
101101

102-
java_binary(
103-
name = "auth-client",
104-
testonly = 1,
105-
main_class = "io.grpc.examples.authentication.AuthClient",
106-
runtime_deps = [
107-
":examples",
108-
],
109-
)
110-
111-
java_binary(
112-
name = "auth-server",
113-
testonly = 1,
114-
main_class = "io.grpc.examples.authentication.AuthServer",
115-
runtime_deps = [
116-
":examples",
117-
],
118-
)
119-
120102
java_binary(
121103
name = "route-guide-client",
122104
testonly = 1,

β€Žexamples/README.mdβ€Ž

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,6 @@ before trying out the examples.
2525

2626
- [Json serialization](src/main/java/io/grpc/examples/advanced)
2727

28-
- [Authentication](AUTHENTICATION_EXAMPLE.md)
29-
3028
- <details>
3129
<summary>Hedging</summary>
3230

@@ -161,6 +159,8 @@ $ bazel-bin/hello-world-client
161159

162160
- [Google Authentication](example-gauth)
163161

162+
- [JWT-based Authentication](example-jwt-auth/README.md)
163+
164164
- [Kotlin examples](example-kotlin)
165165

166166
- [Kotlin Android examples](example-kotlin/android)

β€Žexamples/build.gradleβ€Ž

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,6 @@ dependencies {
3333
// examples/advanced need this for JsonFormat
3434
implementation "com.google.protobuf:protobuf-java-util:${protobufVersion}"
3535

36-
// examples/authentication need this to create and verify JSON Web Tokens (JWTs)
37-
implementation "io.jsonwebtoken:jjwt:0.9.1"
38-
implementation "javax.xml.bind:jaxb-api:2.3.1"
39-
4036
runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}"
4137

4238
testImplementation "io.grpc:grpc-testing:${grpcVersion}"
@@ -108,20 +104,6 @@ task hedgingHelloWorldClient(type: CreateStartScripts) {
108104
classpath = startScripts.classpath
109105
}
110106

111-
task authServer(type: CreateStartScripts) {
112-
mainClassName = 'io.grpc.examples.authentication.AuthServer'
113-
applicationName = 'auth-server'
114-
outputDir = new File(project.buildDir, 'tmp')
115-
classpath = startScripts.classpath
116-
}
117-
118-
task authClient(type: CreateStartScripts) {
119-
mainClassName = 'io.grpc.examples.authentication.AuthClient'
120-
applicationName = 'auth-client'
121-
outputDir = new File(project.buildDir, 'tmp')
122-
classpath = startScripts.classpath
123-
}
124-
125107
task compressingHelloWorldClient(type: CreateStartScripts) {
126108
mainClassName = 'io.grpc.examples.experimental.CompressingHelloWorldClient'
127109
applicationName = 'compressing-hello-world-client'
@@ -136,8 +118,6 @@ applicationDistribution.into('bin') {
136118
from(helloWorldClient)
137119
from(hedgingHelloWorldClient)
138120
from(hedgingHelloWorldServer)
139-
from(authServer)
140-
from(authClient)
141121
from(compressingHelloWorldClient)
142122
fileMode = 0755
143123
}

β€Žexamples/AUTHENTICATION_EXAMPLE.mdβ€Ž renamed to β€Žexamples/example-jwt-auth/README.mdβ€Ž

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,10 @@ This example illustrates a simple JWT-based authentication implementation in gRP
66

77
The example requires grpc-java to be pre-built. Using a release tag will download the relevant binaries
88
from a maven repository. But if you need the latest SNAPSHOT binaries you will need to follow
9-
[COMPILING](../COMPILING.md) to build these.
9+
[COMPILING](../../COMPILING.md) to build these.
1010

1111
The source code is [here](src/main/java/io/grpc/examples/authentication). Please follow the
12-
[steps](./README.md#to-build-the-examples) to build the examples. The build creates scripts
12+
[steps](../README.md#to-build-the-examples) to build the examples. The build creates scripts
1313
`auth-server` and `auth-client` in the `build/install/examples/bin/` directory which can be
1414
used to run this example. The example requires the server to be running before starting the
1515
client.
@@ -38,9 +38,9 @@ The `user-name` value is simply passed in the `HelloRequest` message as payload
3838

3939
```bash
4040
# Run the server:
41-
./build/install/examples/bin/auth-server
41+
./build/install/example-jwt-auth/bin/auth-server
4242
# In another terminal run the client
43-
./build/install/examples/bin/auth-client userA clientB
43+
./build/install/example-jwt-auth/bin/auth-client userA clientB
4444
```
4545

4646
That's it! The client will show the user-name reflected back in the message from the server as follows:
@@ -55,22 +55,11 @@ Processing request from clientB
5555

5656
## Maven
5757

58-
If you prefer to use Maven follow these [steps](./README.md#maven). You can run the example as follows:
58+
If you prefer to use Maven follow these [steps](../README.md#maven). You can run the example as follows:
5959

6060
```
6161
$ # Run the server
6262
$ mvn exec:java -Dexec.mainClass=io.grpc.examples.authentication.AuthServer
6363
$ # In another terminal run the client
64-
$ mvn exec:java -Dexec.mainClass=io.grpc.examples.authentication.AuthClient -Dexec.args="client-userA token-valueB"
65-
```
66-
67-
## Bazel
68-
69-
If you prefer to use Bazel:
70-
```
71-
$ bazel build :auth-server :auth-client
72-
$ # Run the server
73-
$ bazel-bin/auth-server
74-
$ # In another terminal run the client
75-
$ bazel-bin/auth-client client-userA token-valueB
64+
$ mvn exec:java -Dexec.mainClass=io.grpc.examples.authentication.AuthClient -Dexec.args="userA clientB"
7665
```
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
plugins {
2+
// Provide convenience executables for trying out the examples.
3+
id 'application'
4+
// ASSUMES GRADLE 2.12 OR HIGHER. Use plugin version 0.7.5 with earlier gradle versions
5+
id 'com.google.protobuf' version '0.8.8'
6+
// Generate IntelliJ IDEA's .idea & .iml project files
7+
id 'idea'
8+
}
9+
10+
repositories {
11+
maven { // The google mirror is less flaky than mavenCentral()
12+
url "https://maven-central.storage-download.googleapis.com/repos/central/data/"
13+
}
14+
mavenLocal()
15+
}
16+
17+
sourceCompatibility = 1.7
18+
targetCompatibility = 1.7
19+
20+
// IMPORTANT: You probably want the non-SNAPSHOT version of gRPC. Make sure you
21+
// are looking at a tagged version of the example and not "master"!
22+
23+
// Feel free to delete the comment at the next line. It is just for safely
24+
// updating the version in our release process.
25+
def grpcVersion = '1.23.0-SNAPSHOT' // CURRENT_GRPC_VERSION
26+
def protobufVersion = '3.7.1'
27+
def protocVersion = protobufVersion
28+
29+
dependencies {
30+
implementation "io.grpc:grpc-protobuf:${grpcVersion}"
31+
implementation "io.grpc:grpc-stub:${grpcVersion}"
32+
implementation "io.jsonwebtoken:jjwt:0.9.1"
33+
implementation "javax.xml.bind:jaxb-api:2.3.1"
34+
35+
compileOnly "javax.annotation:javax.annotation-api:1.2"
36+
37+
runtimeOnly "io.grpc:grpc-netty-shaded:${grpcVersion}"
38+
39+
testImplementation "io.grpc:grpc-testing:${grpcVersion}"
40+
testImplementation "junit:junit:4.12"
41+
testImplementation "org.mockito:mockito-core:2.25.1"
42+
}
43+
44+
protobuf {
45+
protoc { artifact = "com.google.protobuf:protoc:${protocVersion}" }
46+
plugins {
47+
grpc { artifact = "io.grpc:protoc-gen-grpc-java:${grpcVersion}" }
48+
}
49+
generateProtoTasks {
50+
all()*.plugins { grpc {} }
51+
}
52+
}
53+
54+
// Inform IDEs like IntelliJ IDEA, Eclipse or NetBeans about the generated code.
55+
sourceSets {
56+
main {
57+
java {
58+
srcDirs 'build/generated/source/proto/main/grpc'
59+
srcDirs 'build/generated/source/proto/main/java'
60+
}
61+
}
62+
}
63+
64+
startScripts.enabled = false
65+
66+
task authServer(type: CreateStartScripts) {
67+
mainClassName = 'io.grpc.examples.authentication.AuthServer'
68+
applicationName = 'auth-server'
69+
outputDir = new File(project.buildDir, 'tmp')
70+
classpath = startScripts.classpath
71+
}
72+
73+
task authClient(type: CreateStartScripts) {
74+
mainClassName = 'io.grpc.examples.authentication.AuthClient'
75+
applicationName = 'auth-client'
76+
outputDir = new File(project.buildDir, 'tmp')
77+
classpath = startScripts.classpath
78+
}
79+
80+
applicationDistribution.into('bin') {
81+
from(authServer)
82+
from(authClient)
83+
fileMode = 0755
84+
}
Lines changed: 136 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,136 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0"
2+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
<groupId>io.grpc</groupId>
6+
<artifactId>example-jwt-auth</artifactId>
7+
<packaging>jar</packaging>
8+
<!-- Feel free to delete the comment at the end of these lines. It is just
9+
for safely updating the version in our release process. -->
10+
<version>1.23.0-SNAPSHOT</version><!-- CURRENT_GRPC_VERSION -->
11+
<name>example-jwt-auth</name>
12+
<url>https://github.com/grpc/grpc-java</url>
13+
14+
<properties>
15+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
16+
<grpc.version>1.23.0-SNAPSHOT</grpc.version><!-- CURRENT_GRPC_VERSION -->
17+
<protobuf.version>3.7.1</protobuf.version>
18+
<protoc.version>3.7.1</protoc.version>
19+
<!-- required for jdk9 -->
20+
<maven.compiler.source>1.7</maven.compiler.source>
21+
<maven.compiler.target>1.7</maven.compiler.target>
22+
</properties>
23+
24+
<dependencyManagement>
25+
<dependencies>
26+
<dependency>
27+
<groupId>io.grpc</groupId>
28+
<artifactId>grpc-bom</artifactId>
29+
<version>${grpc.version}</version>
30+
<type>pom</type>
31+
<scope>import</scope>
32+
</dependency>
33+
</dependencies>
34+
</dependencyManagement>
35+
36+
<dependencies>
37+
<dependency>
38+
<groupId>io.grpc</groupId>
39+
<artifactId>grpc-netty-shaded</artifactId>
40+
<scope>runtime</scope>
41+
</dependency>
42+
<dependency>
43+
<groupId>io.grpc</groupId>
44+
<artifactId>grpc-protobuf</artifactId>
45+
</dependency>
46+
<dependency>
47+
<groupId>io.grpc</groupId>
48+
<artifactId>grpc-stub</artifactId>
49+
</dependency>
50+
<dependency>
51+
<groupId>io.jsonwebtoken</groupId>
52+
<artifactId>jjwt</artifactId>
53+
<version>0.9.1</version>
54+
</dependency>
55+
<dependency>
56+
<groupId>javax.xml.bind</groupId>
57+
<artifactId>jaxb-api</artifactId>
58+
<version>2.3.1</version>
59+
</dependency>
60+
<dependency>
61+
<groupId>javax.annotation</groupId>
62+
<artifactId>javax.annotation-api</artifactId>
63+
<version>1.2</version>
64+
<scope>provided</scope> <!-- not needed at runtime -->
65+
</dependency>
66+
<dependency>
67+
<groupId>io.grpc</groupId>
68+
<artifactId>grpc-testing</artifactId>
69+
<scope>test</scope>
70+
</dependency>
71+
<dependency>
72+
<groupId>junit</groupId>
73+
<artifactId>junit</artifactId>
74+
<version>4.12</version>
75+
<scope>test</scope>
76+
</dependency>
77+
<dependency>
78+
<groupId>org.mockito</groupId>
79+
<artifactId>mockito-core</artifactId>
80+
<version>2.25.1</version>
81+
<scope>test</scope>
82+
</dependency>
83+
</dependencies>
84+
85+
<build>
86+
<extensions>
87+
<extension>
88+
<groupId>kr.motd.maven</groupId>
89+
<artifactId>os-maven-plugin</artifactId>
90+
<version>1.5.0.Final</version>
91+
</extension>
92+
</extensions>
93+
<plugins>
94+
<plugin>
95+
<groupId>org.xolstice.maven.plugins</groupId>
96+
<artifactId>protobuf-maven-plugin</artifactId>
97+
<version>0.5.1</version>
98+
<configuration>
99+
<protocArtifact>
100+
com.google.protobuf:protoc:${protoc.version}:exe:${os.detected.classifier}
101+
</protocArtifact>
102+
<pluginId>grpc-java</pluginId>
103+
<pluginArtifact>
104+
io.grpc:protoc-gen-grpc-java:${grpc.version}:exe:${os.detected.classifier}
105+
</pluginArtifact>
106+
</configuration>
107+
<executions>
108+
<execution>
109+
<goals>
110+
<goal>compile</goal>
111+
<goal>compile-custom</goal>
112+
</goals>
113+
</execution>
114+
</executions>
115+
</plugin>
116+
<plugin>
117+
<groupId>org.apache.maven.plugins</groupId>
118+
<artifactId>maven-enforcer-plugin</artifactId>
119+
<version>1.4.1</version>
120+
<executions>
121+
<execution>
122+
<id>enforce</id>
123+
<goals>
124+
<goal>enforce</goal>
125+
</goals>
126+
<configuration>
127+
<rules>
128+
<requireUpperBoundDeps/>
129+
</rules>
130+
</configuration>
131+
</execution>
132+
</executions>
133+
</plugin>
134+
</plugins>
135+
</build>
136+
</project>
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
pluginManagement {
2+
repositories {
3+
maven { // The google mirror is less flaky than mavenCentral()
4+
url "https://maven-central.storage-download.googleapis.com/repos/central/data/"
5+
}
6+
gradlePluginPortal()
7+
}
8+
}
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)