|
1 |
| -# microservice-template-java |
| 1 | +# Java Microservice Quickstart Template |
| 2 | +Spring-Boot application preconfigured for to use [EGO](https://github.com/overture-stack/ego/) generated JWTs for authorization. |
| 3 | +[](https://opensource.org/licenses/Apache-2.0) |
| 4 | + |
| 5 | + |
| 6 | +## Features |
| 7 | +This template provides the following: |
| 8 | + |
| 9 | +* Spring-Boot Application with Spring Security |
| 10 | +* JWT Authentication |
| 11 | +* JWT Asymmetric Verificaiton - fetches public-key from web on start-up |
| 12 | +* JWT Filter - User Role and Status requirements implemented by default |
| 13 | +* Docker and Docker-Compose configuration |
| 14 | + |
| 15 | + |
| 16 | +## Requirements |
| 17 | +The following software is required to run the application. It can be run locally or in a docker container. |
| 18 | + |
| 19 | + |
| 20 | +### EGO |
| 21 | +A running instance of [EGO](https://github.com/overture-stack/ego/) is required to generate the Authorization tokens and to provide the verification key. |
| 22 | + |
| 23 | +[EGO](https://github.com/overture-stack/ego/) can be cloned and run locally if no public server is available. |
| 24 | + |
| 25 | + |
| 26 | +### Local |
| 27 | +* [Java 8 SDK](http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html) |
| 28 | +* [Maven](https://maven.apache.org/download.cgi) |
| 29 | + |
| 30 | + |
| 31 | +### Docker |
| 32 | +* [Docker](https://www.docker.com/get-docker) |
| 33 | + |
| 34 | + |
| 35 | +## Quick Start |
| 36 | +Make sure the JWT Verification Key is configured, then you can run the server in a docker container or on your local machine. |
| 37 | + |
| 38 | + |
| 39 | +### Configure JWT Verification Key |
| 40 | +Update __application.yml__. Set `auth.jwt.publicKeyUrl` to the URL to fetch the JWT verification key. The application will not start if it can't set the verification key for the JWTConverter. |
| 41 | + |
| 42 | +The default value in the __application.yml__ file is set to connect to EGO running locally on its default port `8081`. |
| 43 | + |
| 44 | +### Run Local |
| 45 | +```bash |
| 46 | +$ mvn spring-boot:run |
| 47 | +``` |
| 48 | + |
| 49 | +Application will run by default on port `1234` |
| 50 | + |
| 51 | +Configure the port by changing `server.port` in __application.yml__ |
| 52 | + |
| 53 | + |
| 54 | +### Run Docker |
| 55 | + |
| 56 | +First build the image: |
| 57 | +```bash |
| 58 | +$ docker-compose build |
| 59 | +``` |
| 60 | + |
| 61 | +When ready, run it: |
| 62 | +```bash |
| 63 | +$ docker-compose up |
| 64 | +``` |
| 65 | + |
| 66 | +Application will run by default on port `1234` |
| 67 | + |
| 68 | +Configure the port by changing `services.api.ports` in __docker-compose.yml__. Port 1234 was used by default so the value is easy to identify and change in the configuration file. |
| 69 | + |
| 70 | +### Test Endpoint |
| 71 | +The application has a single endpoint `/test` that will accept GET and POST requests with a valid token. |
| 72 | + |
| 73 | +A JWT must be passed in a request header, following the Bearer token pattern. Below is a usable value to test with, it is valid vs. the example keystore given in the EGO repo. |
| 74 | + |
| 75 | + ``` |
| 76 | + Authorization=Bearer eyJhbGciOiJSUzI1NiJ9.eyJpYXQiOjE1MTI3NjIxODIsImV4cCI6MjE0NzQ4MzY0Nywic3ViIjoiNjA2IiwiaXNzIjoiZWdvIiwiYXVkIjpbXSwiY29udGV4dCI6eyJ1c2VyIjp7Im5hbWUiOiJEZW1vLlVzZXJAZXhhbXBsZS5jb20iLCJlbWFpbCI6IkRlbW8uVXNlckBleGFtcGxlLmNvbSIsInN0YXR1cyI6IkFwcHJvdmVkIiwiZmlyc3ROYW1lIjoiRGVtbyIsImxhc3ROYW1lIjoiVXNlciIsImNyZWF0ZWRBdCI6IjIwMTctMTEtMjIgMDM6MTA6NTUiLCJsYXN0TG9naW4iOiIyMDE3LTEyLTA4IDA3OjQzOjAyIiwicHJlZmVycmVkTGFuZ3VhZ2UiOm51bGwsInJvbGVzIjpbIlVTRVIiXX19LCJqdGkiOiI0OGE5NGIzNy1mMTJlLTQxNWQtYjM1Zi1kZDhmOThiMDQ4ZDcifQ.Cmgbd_xnUp8dPnIJvmUXmh5LYnHgHSk_n_0VzCn0k9r4WVNdsupb-MQqJvgOMg3K8si5mzhIjzLi9rZL5N_JwFXtpjKXKRVT7KF4mY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6w8Nvqxcp-3uoff6K0uKLUdQ' |
| 90 | +``` |
| 91 | + |
| 92 | +If everything is working as expected, the request should return a pleasant greeting. ;) |
| 93 | + |
| 94 | +## Template Guide |
| 95 | +Here is a convenient list of steps to create a new application based on this template: |
| 96 | + |
| 97 | +1. Fork this! |
| 98 | +2. Update __pom.xml__: |
| 99 | + - `groupId` |
| 100 | + - `artifactId` |
| 101 | + - `name` |
| 102 | + - `description` |
| 103 | +3. Update __application.yml__: |
| 104 | + - `auth.jwt.publicKeyUrl` - URL to fetch the JWT verification key |
| 105 | +4. Update __README.md__: |
| 106 | + - Replace current README with template - __README.template.md__ |
| 107 | + - Remove template file |
| 108 | + - Update Project name and description in new README |
| 109 | + |
0 commit comments