grpc-java/examples/example-jwt-auth
Eric Anderson efd968bcbb Upgrade Protobuf to 3.19.1 and Guava to 30.1.1
Protobuf uses Guava 30.1.1, so I upgrade it at the same time. It also
caused an update to rules_jvm_external and reworking the Bazel build.
Protobuf no longer requires bind() so they were dropped. Although
Protobuf's protobuf_deps() brings in rules_jvm_external, and so we don't
need to define it ourselves, it seems better to define it directly and
not depend on transitive deps since we use it directly.

Protobuf now has support for maven_install() by exposing
PROTOBUF_MAVEN_ARTIFACTS, which required reorganizing the WORKSPACE to
use maven_install() after loading protobuf. Protobuf still doesn't
define target overrides for itself so we still maintain those. When
reorganizing the WORKSPACE I noticed http_archive should ideally be
above io_grpc_grpc_java as most users will need it there, so I fixed
that since there were lots of other load()-reordering already.
2021-12-09 10:35:39 -08:00
..
src examples: Add a JWT authentication example (#5915) 2020-03-17 17:43:31 -07:00
README.md examples: Add a JWT authentication example (#5915) 2020-03-17 17:43:31 -07:00
build.gradle Upgrade Protobuf to 3.19.1 and Guava to 30.1.1 2021-12-09 10:35:39 -08:00
pom.xml Upgrade Protobuf to 3.19.1 and Guava to 30.1.1 2021-12-09 10:35:39 -08:00
settings.gradle Swap to new Google Maven Central mirror URL 2020-07-10 08:45:49 -05:00

README.md

Authentication Example

This example illustrates a simple JWT-based authentication implementation in gRPC using server interceptor. It uses the JJWT library to create and verify JSON Web Tokens (JWTs).

The example requires grpc-java to be pre-built. Using a release tag will download the relevant binaries from a maven repository. But if you need the latest SNAPSHOT binaries you will need to follow COMPILING to build these.

The source code is here. To build the example, run in this directory:

$ ../gradlew installDist

The build creates scripts auth-server and auth-client in the build/install/example-jwt-auth/bin/ directory which can be used to run this example. The example requires the server to be running before starting the client.

Running auth-server is similar to the normal hello world example and there are no arguments to supply:

auth-server:

The auth-server accepts optional argument for port on which the server should run:

USAGE: auth-server [port]

The auth-client accepts optional arguments for server-host, server-port, user-name and client-id:

auth-client:

USAGE: auth-client [server-host [server-port [user-name [client-id]]]]

The user-name value is simply passed in the HelloRequest message as payload and the value of client-id is included in the JWT claims passed in the metadata header.

How to run the example:

# Run the server:
./build/install/example-jwt-auth/bin/auth-server 50051
# In another terminal run the client
./build/install/example-jwt-auth/bin/auth-client localhost 50051 userA clientB

That's it! The client will show the user-name reflected back in the message from the server as follows:

INFO: Greeting: Hello, userA

And on the server side you will see the message with the client's identifier:

Processing request from clientB

Maven

If you prefer to use Maven follow these steps. You can run the example as follows:

$ # Run the server
$ mvn exec:java -Dexec.mainClass=io.grpc.examples.authentication.AuthServer -Dexec.args="50051"
$ # In another terminal run the client
$ mvn exec:java -Dexec.mainClass=io.grpc.examples.authentication.AuthClient -Dexec.args="localhost 50051 userA clientB"