• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2019 The gRPC Authors
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package io.grpc.examples.jwtauth;
18 
19 import io.grpc.Grpc;
20 import io.grpc.InsecureServerCredentials;
21 import io.grpc.Server;
22 import io.grpc.examples.helloworld.GreeterGrpc;
23 import io.grpc.examples.helloworld.HelloReply;
24 import io.grpc.examples.helloworld.HelloRequest;
25 import io.grpc.stub.StreamObserver;
26 import java.io.IOException;
27 import java.util.logging.Logger;
28 
29 /**
30  * Server that manages startup/shutdown of a {@code Greeter} server. This also uses a {@link
31  * JwtServerInterceptor} to intercept the JWT token passed
32  */
33 public class AuthServer {
34 
35   private static final Logger logger = Logger.getLogger(AuthServer.class.getName());
36 
37   private Server server;
38   private int port;
39 
AuthServer(int port)40   public AuthServer(int port) {
41     this.port = port;
42   }
43 
start()44   private void start() throws IOException {
45     server = Grpc.newServerBuilderForPort(port, InsecureServerCredentials.create())
46         .addService(new GreeterImpl())
47         .intercept(new JwtServerInterceptor())  // add the JwtServerInterceptor
48         .build()
49         .start();
50     logger.info("Server started, listening on " + port);
51     Runtime.getRuntime().addShutdownHook(new Thread() {
52       @Override
53       public void run() {
54         // Use stderr here since the logger may have been reset by its JVM shutdown hook.
55         System.err.println("*** shutting down gRPC server since JVM is shutting down");
56         AuthServer.this.stop();
57         System.err.println("*** server shut down");
58       }
59     });
60   }
61 
stop()62   private void stop() {
63     if (server != null) {
64       server.shutdown();
65     }
66   }
67 
68   /**
69    * Await termination on the main thread since the grpc library uses daemon threads.
70    */
blockUntilShutdown()71   private void blockUntilShutdown() throws InterruptedException {
72     if (server != null) {
73       server.awaitTermination();
74     }
75   }
76 
77   /**
78    * Main launches the server from the command line.
79    */
main(String[] args)80   public static void main(String[] args) throws IOException, InterruptedException {
81 
82     // The port on which the server should run
83     int port = 50051; // default
84     if (args.length > 0) {
85       port = Integer.parseInt(args[0]);
86     }
87 
88     final AuthServer server = new AuthServer(port);
89     server.start();
90     server.blockUntilShutdown();
91   }
92 
93   static class GreeterImpl extends GreeterGrpc.GreeterImplBase {
94     @Override
sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver)95     public void sayHello(HelloRequest req, StreamObserver<HelloReply> responseObserver) {
96       // get client id added to context by interceptor
97       String clientId = Constant.CLIENT_ID_CONTEXT_KEY.get();
98       logger.info("Processing request from " + clientId);
99       HelloReply reply = HelloReply.newBuilder().setMessage("Hello, " + req.getName()).build();
100       responseObserver.onNext(reply);
101       responseObserver.onCompleted();
102     }
103   }
104 }
105