• Stars
    star
    498
  • Rank 88,494 (Top 2 %)
  • Language
    Java
  • License
    Apache License 2.0
  • Created over 5 years ago
  • Updated about 2 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Java Microservices: Spring Boot, Spring Cloud, JHipster, Spring Cloud Config, and Spring Cloud Gateway

Java Microservices with Spring Boot & Spring Cloud 🍃☁️

This repository contains examples of how to build a Java microservices architecture with Spring Boot, Spring Cloud, and Netflix Eureka.

This repository has five examples in it:

  1. A bare-bones microservices architecture with Spring Boot, Spring Cloud, Eureka Server, and Zuul.
  2. A microservices architecture that's generated with JHipster and configured centrally with Spring Cloud Config.
  3. A microservices architecture that uses Spring Cloud Gateway and Spring WebFlux to show reactive microservices.
  4. A JHipster-generated reactive microservices architecture with Spring Cloud Gateway and Spring WebFlux.
  5. A JHipster 7 + Kubernetes example that deploys to Google Cloud with sealed secrets.

We think you'll enjoy them all!

  1. See Java Microservices with Spring Boot and Spring Cloud for an overview of the first example.
  2. Read Java Microservices with Spring Cloud Config and JHipster to learn about microservices with JHipster.
  3. Refer to Secure Reactive Microservices with Spring Cloud Gateway to learn about Spring Cloud Gateway and reactive microservices.
  4. Refer to Reactive Java Microservices with Spring Boot and JHipster to see how JHipster makes reactive microservices a breeze.
  5. Peruse Kubernetes to the Cloud with Spring Boot and JHipster to see how JHipster simplifies Kubernetes deployments.

Prerequisites: Java 11 and an internet connection.

Spring Boot + Spring Cloud Example

To install this example, run the following commands:

git clone https://github.com/oktadev/java-microservices-examples.git
cd java-microservices-examples/spring-boot+cloud

The api-gateway and car-service projects are already pre-configured to be locked down with OAuth 2.0 and Okta. That means if you try to run them, you won't be able to login until you create an account, and an application in it.

Create a Web Application in Okta

Log in to your Okta Developer account (or sign up if you don't have an account).

  1. From the Applications page, choose Add Application.
  2. On the Create New Application page, select Web.
  3. Give your app a memorable name, add http://localhost:8080/login/oauth2/code/okta as a Login redirect URI, select Refresh Token (in addition to Authorization Code), and click Done.

Copy the issuer (found under API > Authorization Servers), client ID, and client secret into the application.properties of the api-gateway and car-service projects.

okta.oauth2.issuer=https://{yourOktaDomain}/oauth2/default
okta.oauth2.client-id=$clientId
okta.oauth2.client-secret=$clientSecret

Then, run all the projects with ./mvnw in separate terminal windows. You should be able to navigate to http://localhost:8761 and see the apps have been registered with Eureka.

Then, navigate to http://localhost:8080/cool-cars in your browser, log in with Okta, and see the resulting JSON.

JHipster + Spring Cloud Config Example

To install this example, run the following commands:

git clone https://github.com/oktadev/java-microservices-examples.git
cd java-microservices-examples/jhipster

Create Docker containers for all gateway and microservice applications:

mvn -Pprod verify com.google.cloud.tools:jib-maven-plugin:dockerBuild

Create a Web Application in Okta

Log in to your Okta Developer account (or sign up if you don't have an account).

  1. From the Applications page, choose Add Application.
  2. On the Create New Application page, select Web.
  3. Give your app a memorable name, add http://localhost:8080/login/oauth2/code/okta as a Login redirect URI, select Refresh Token (in addition to Authorization Code), and click Done.
  4. To configure Logout to work in JHipster, Edit your app, add http://localhost:8080 as a Logout redirect URI, then click Save.

Rather than modifying each of your apps for Okta, you can use Spring Cloud Config in JHipster Registry to do it. Open docker-compose/central-server-config/application.yml and add your Okta settings.

The client ID and secret are available on your app settings page. You can find the issuer under API > Authorization Servers.

spring:
  security:
    oauth2:
      client:
        provider:
          oidc:
            issuer-uri: https://{yourOktaDomain}/oauth2/default
        registration:
          oidc:
            client-id: {yourClientId}
            client-secret: {yourClientSecret}

The registry, gateway, blog, and store applications are all configured to read this configuration on startup.

Start all your containers from the docker-compose directory:

docker-compose up -d

Before you can log in to the registry, you'll need to add redirect URIs for JHipster Registry, ensure your user is in a ROLE_ADMIN group and that groups are included in the ID token.

Log in to your Okta dashboard, edit your OIDC app, and add the following Login redirect URI:

  • http://localhost:8761/login/oauth2/code/oidc

You'll also need to add a Logout redirect URI:

  • http://localhost:8761

Then, click Save.

Create Groups and Add Them as Claims to the ID Token

JHipster is configured by default to work with two types of users: administrators and users. Keycloak is configured with users and groups automatically, but you need to do some one-time configuration for your Okta organization.

Create a ROLE_ADMIN group (Users > Groups > Add Group) and add your user to it. Navigate to API > Authorization Servers, and click on the the default server. Click the Claims tab and Add Claim. Name it groups, and include it in the ID Token. Set the value type to Groups and set the filter to be a Regex of .*. Click Create.

Now when you hit http://localhost:8761 or http://localhost:8080, you should be able to log in with Okta!

Spring Cloud Gateway Example

To install this example, run the following commands:

git clone https://github.com/oktadev/java-microservices-examples.git
cd java-microservices-examples/spring-cloud-gateway

The api-gateway and car-service projects are already pre-configured to be locked down with OAuth 2.0 and Okta. That means if you try to run them, you won't be able to login until you create an account, and an application in it.

If you already have an Okta account, see the Create a Web Application in Okta section below. Otherwise, we created a Maven plugin that configures a free Okta developer account + an OIDC app (in under a minute!).

To use it, run ./mvnw com.okta:okta-maven-plugin:setup to create an account and configure the gateway to work with Okta.

Copy the okta.* properties from the gateway's src/main/resources/application.properties to the same file in the car-service project.

Then, run all the projects with ./mvnw in separate terminal windows. You should be able to navigate to http://localhost:8761 and see the apps have been registered with Eureka.

Then, navigate to http://localhost:8080/cars in your browser, log in with Okta, and see the resulting JSON.

Create a Web Application in Okta

Log in to your Okta Developer account (or sign up if you don't have an account).

  1. From the Applications page, choose Add Application.
  2. On the Create New Application page, select Web.
  3. Give your app a memorable name, add http://localhost:8080/login/oauth2/code/okta as a Login redirect URI and click Done.

Copy the issuer (found under API > Authorization Servers), client ID, and client secret into the application.properties of the api-gateway and car-service projects.

okta.oauth2.issuer=https://{yourOktaDomain}/oauth2/default
okta.oauth2.client-id=$clientId
okta.oauth2.client-secret=$clientSecret

Reactive Microservices with JHipster Example

To install this example, run the following commands:

git clone https://github.com/oktadev/java-microservices-examples.git
cd java-microservices-examples/reactive-jhipster

The JHipster Registry and Spring Cloud Config are pre-configured to use Okta. That means if you try to run them, you won't be able to login until you create an account, and an application in it.

Install the Okta CLI using the instructions on cli.okta.com and come back here when you're done. If you don't have an Okta developer account, run okta register.

NOTE: You can also use your browser and Okta's developer console to register an app. See JHipster's security documentation for those instructions.

From the gateway project's directory, run okta apps create jhipster. Accept the default redirect URIs.

This process does several things:

  1. Registers an OIDC app in Okta with JHipster's configured redirect URIs.
  2. Creates ROLE_ADMIN and ROLE_USER groups and adds your user to both.
  3. Creates a groups claim and adds it to ID tokens.
  4. Creates a .okta.env file with the values you'll need to talk to Okta.

Spring Cloud Config allows you to distribute Spring's configuration between apps. Update gateway/src/main/docker/central-server-config/localhost-config/application.yml to use your Okta app settings. You can find the values for each property in the .okta.env file.

spring:
  security:
    oauth2:
      client:
        provider:
          oidc:
            issuer-uri: https://<your-okta-domain>/oauth2/default
        registration:
          oidc:
            client-id: <client-id>
            client-secret: <client-secret>

Save your changes. These values will be distributed to the JHipster Registry, gateway, blog, and store apps. Start all the services and apps using the following commands:

cd gateway
docker-compose -f src/main/docker/keycloak.yml up -d #jhkeycloakup
docker-compose -f src/main/docker/postgresql.yml up -d #jhpostgresqlup
docker-compose -f src/main/docker/jhipster-registery up -d #jhregistryup
./gradlew

Open a new terminal window, start the blog app's Neo4j database, and then the app itself.

cd ../blog
docker-compose -f src/main/docker/neo4j.yml up -d #jhneo4jup
./gradlew

Then, open another terminal window, start the store app's MongoDB database, and the microservice.

cd ../store
docker-compose -f src/main/docker/mongodb.yml up -d #jhmongoup
./gradlew

Now, open a new incognito browser window, go to http://localhost:8080, and sign in. Rejoice that using Okta for authentication works!

TIP: You can also run everything using Docker Compose. See the blog post for how to do that.

Kubernetes + Reactive Java with JHipster Example

To install this example, run the following commands:

git clone https://github.com/oktadev/java-microservices-examples.git
cd java-microservices-examples/jhipster-k8s/k8s

If you don't have JHipster installed, install it.

npm i -g generator-jhipster@7

Run JHipster's Kubernetes sub-generator.

jhipster k8s

You will be prompted with several questions. The answers will be pre-populated from choices I made when creating this app. Answer as follows, changing the Docker repository name to yours, or leaving it blank if you don't have one.

  • Type of application: Microservice application
  • Root directory: ../
  • Which applications? <select all>
  • Set up monitoring? No
  • Which applications with clustered databases? select store
  • Admin password for JHipster Registry: <generate one>
  • Kubernetes namespace: demo
  • Docker repository name: <your docker hub username>
  • Command to push Docker image: docker push
  • Enable Istio? No
  • Kubernetes service type? LoadBalancer
  • Use dynamic storage provisioning? Yes
  • Use a specific storage class? <leave empty>

Install Minikube to Run Kubernetes Locally

If you have Docker installed, you can run Kubernetes locally with Minikube. Run minikube start to begin.

minikube --memory 8g --cpus 8 start

Build Docker images for each app. In the {gateway, blog, store } directories, run the following Gradle command (where <image-name> is gateway, store, or blog).

./gradlew bootJar -Pprod jib -Djib.to.image=<docker-repo-name>/<image-name>

You can also build your images locally and publish them to your Docker daemon. This is the default if you didn't specify a base Docker repository name.

# this command exposes Docker images to minikube
eval $(minikube docker-env)
./gradlew -Pprod bootJar jibDockerBuild

Because this publishes your images locally to Docker, you'll need to make modifications to your Kubernetes deployment files to use imagePullPolicy: IfNotPresent.

- name: gateway-app
  image: gateway
  imagePullPolicy: IfNotPresent

Make sure to add this imagePullPolicy to the following files:

  • k8s/gateway-k8s/gateway-deployment.yml
  • k8s/blog-k8s/blog-deployment.yml
  • k8s/store-k8s/store-deployment.yml

Register an OIDC App for Auth

Install the Okta CLI using the instructions on cli.okta.com and come back here when you're done. If you don't have an Okta developer account, run okta register.

NOTE: You can also use your browser and Okta's developer console to register an app. See JHipster's security documentation for those instructions.

From the gateway project's directory, run okta apps create jhipster. Accept the default redirect URIs.

This process does several things:

  1. Registers an OIDC app in Okta with JHipster's configured redirect URIs.
  2. Creates ROLE_ADMIN and ROLE_USER groups and adds your user to both.
  3. Creates a groups claim and adds it to ID tokens.
  4. Creates a .okta.env file with the values you'll need to talk to Okta.

Update k8s/registry-k8s/application-configmap.yml to contain your OIDC settings from the .okta.env file the Okta CLI just created. The Spring Cloud Config server reads from this file and shares the values with the gateway and microservices.

data:
  application.yml: |-
    ...
    spring:
      security:
        oauth2:
          client:
            provider:
              oidc:
                issuer-uri: https://<your-okta-domain>/oauth2/default
            registration:
              oidc:
                client-id: <client-id>
                client-secret: <client-secret>

To configure the JHipster Registry to use OIDC for authentication, modify k8s/registry-k8s/jhipster-registry.yml to enable the oauth2 profile.

- name: SPRING_PROFILES_ACTIVE
  value: prod,k8s,oauth2

Then, in the k8s directory, start your engines!

./kubectl-apply.sh -f

You can see if everything starts up using the following command.

kubectl get pods -n default

You can use the name of a pod with kubectl logs to tail its logs.

kubectl logs <pod-name> --tail=-1 -n default

You can use port-forwarding to see the JHipster Registry.

kubectl port-forward svc/jhipster-registry -n default 8761

Open a browser and navigate to http://localhost:8761. You'll need to sign in with your Okta credentials.

Once all is green, use port-forwarding to see the gateway app.

kubectl port-forward svc/gateway -n default 8080

Then, go to http://localhost:8080, and you should be able to add blogs, posts, tags, and products.

Please read the Kubernetes to the Cloud with Spring Boot and JHipster for more information.

Links

These examples use the following open source libraries:

Help

Please post any questions as comments on the example's blog post, or on the Okta Developer Forums.

License

Apache 2.0, see LICENSE.

More Repositories

1

okta-aws-cli-assume-role

Okta AWS CLI Assume Role Tool
HTML
337
star
2

spring-boot-microservices-example

Bootiful Microservices with Spring Boot
TypeScript
307
star
3

okta-spring-boot-react-crud-example

Simple CRUD with React and Spring Boot 3
Java
252
star
4

okta-spring-boot-2-angular-5-example

A Cool Cars Example that showcases Spring Boot 2, Angular 5, and Okta's Support for both.
TypeScript
131
star
5

jhipster-microservices-example

JHipster Microservices Example using Spring Cloud, Spring Boot, Angular, Docker, and Kubernetes
Java
126
star
6

spring-boot-react-example

Example app showing how to build a Spring Boot REST API and React UI
TypeScript
109
star
7

spring-boot-angular-example

Example app showing how to build a Spring Boot REST API and Angular UI
TypeScript
94
star
8

okta-vue-node-example

Example of a Basic CRUD App with Vue.js and Node
JavaScript
90
star
9

okta-spring-boot-2-angular-7-example

A Cool Cars Example that showcases Spring Boot 2.1, Angular 7, and Okta's support for both.
TypeScript
86
star
10

schematics

Schematics for adding Okta and Auth0 Auth to your projects
TypeScript
69
star
11

okta-node-express-typescript-vue-example

Example Node.js API using TypeScript, Express, and Vue
TypeScript
66
star
12

native-java-examples

Native Java Apps with Micronaut, Quarkus, and Spring Boot
HTML
61
star
13

okta-react-node-example

A simple React.js + Node.js example app.
JavaScript
58
star
14

okta-spring-boot-authz-server-example

Spring Security OAuth 2.0 Guide
Java
58
star
15

okta-spring-boot-saml-example

Spring Boot, SAML, and Okta
Java
57
star
16

okta-oidc-flows-example

JavaScript
56
star
17

okta-spring-boot-2-angular-8-example

Angular 8 + Spring Boot 2.2 = ❤️
TypeScript
53
star
18

python-flask-react-crud-example

Build a simple CRUD application with Python, Flask, and React.
JavaScript
51
star
19

okta-php-core-rest-api-example

PHP
49
star
20

okta-go-vue-example

An Okta example app built with Go + Vue.
Go
47
star
21

okta-nodejs-sql-server-example

JavaScript
47
star
22

okta-scim-beta

Python
44
star
23

okta-kong-origin-example

Kong Gateway Example with OIDC and Okta
Java
44
star
24

okta-spring-boot-vue-crud-example

A Todo App that showcases Spring Boot, Vue.js, and Okta's support for both.
Vue
42
star
25

okta-blog

The Okta developer blog.
SCSS
41
star
26

okta-spring-boot-2-angular-9-example

CRUD Example with Angular 9.0 and Spring Boot 2.2
TypeScript
40
star
27

okta-dac

Okta Delegated Admin Console
Vue
38
star
28

okta-aspnetcore-vue-crud-example

CRUD Application with ASP.NET Core and Vue.js
JavaScript
37
star
29

okta-php-symfony-vue-crud-example

Build a Basic CRUD App with Symfony 4 and Vue
PHP
31
star
30

okta-node-sql-server-example

Build a Node.js App with SQL Server Tutorial
JavaScript
30
star
31

okta-jhipster-microservices-oauth-example

A microservices architecture built with JHipster, OAuth 2.0, and Okta
Java
30
star
32

okta-react-native-app-auth-example

React Native Example that Authenticates with Okta using App Auth
JavaScript
29
star
33

auth0-java-microservices-examples

Java Microservice Examples
Java
29
star
34

okta-wordpress-sign-in-widget

Replaces the WordPress login screen with the Okta sign-in widget
PHP
29
star
35

okta-vue-auth-example

A Vue.js Example with Okta for Authentication
JavaScript
29
star
36

okta-aspnet-mvc-example

ASP.NET 4.x MVC + Okta example
JavaScript
28
star
37

spring-boot-ionic-example

Example app that shows how to develop a mobile app with Ionic, Angular, and Spring Boot
TypeScript
28
star
38

auth0-full-stack-java-example

🔥 Full Stack Java Example
Java
28
star
39

auth0-java-oauth-examples

Java
27
star
40

okta-react-native-spring-boot-example

React Native + Spring Boot + OIDC
Java
27
star
41

auth0-spring-boot-angular-crud-example

Angular and Spring Boot CRUD Example
Java
27
star
42

ionic-jhipster-starter

Starter Template for Ionic Apps with JHipster
TypeScript
27
star
43

spring-boot-angular-pwa-example

Spring Boot API with Angular PWA
TypeScript
24
star
44

okta-angular-microfrontend-example

Starter code + completed project for micro-frontends using Webpack 5 and Module Federation plugin in Angular and sharing authenticated state
TypeScript
24
star
45

byob-dashboard

Vue
24
star
46

auth0-micro-frontends-jhipster-example

Micro Frontends for Java Microservices
Java
24
star
47

okta-ionic-auth-example

Example showing Ionic Authentication with Okta
TypeScript
23
star
48

spring-boot-vue-example

Bootiful Spring Boot + Vue Example
Vue
23
star
49

okta-cli-mfa-example

Shell
23
star
50

shiro-spring-boot-example

Java
23
star
51

auth0-java-rest-api-examples

Java REST API Examples
HTML
23
star
52

okta-aspnetcore-mvc-example

ASP.NET Core 2.0 MVC + Okta example
C#
22
star
53

okta-php-laravel-vue-crud-example

PHP
22
star
54

okta-angular-openid-connect-example

Angular + Angular CLI with Authentication from OpenID Connect and Okta
TypeScript
22
star
55

okta-react-photo-gallery-example

A Photo Gallery Example with React, Spring Boot, and JHipster 5 + Auth by Okta
TypeScript
22
star
56

crypto-invoicer

A personal web portal for invoicing clients with Bitcoin.
JavaScript
21
star
57

gatsby-netlify-okta-example

Gatsby, Netlify, and Okta Example App
JavaScript
21
star
58

okta-spring-boot-oauth-example

Spring Boot, OAuth 2.0, and Okta Example
Groovy
21
star
59

okta-smartfhir-docs

Documentation for using Okta as a SMART/FHIR authorization server.
20
star
60

api-security-book

CSS
20
star
61

okta-angular-material-login-example

Angular Material Login Example
TypeScript
20
star
62

okta-graphql-react-express-example

Web App with Express, React and GraphQL
JavaScript
20
star
63

okta-microservice-security-examples

Demos from Oktane18: API and Microservices Best Practices
Java
18
star
64

okta-spring-security-5-example

Authentication with Spring Security 5 and Okta OIDC
Java
18
star
65

okta-angular-sign-in-widget-example

Angular + Okta Sign-In Widget Example
TypeScript
18
star
66

okta-node-express-example

A simple sample application built using Node and Express that contains user login, registration, and password reset functionality.
JavaScript
17
star
67

okta-smartfhir-demo

Example of using Okta as an authorization server as part of a SMART on FHIR deployment.
JavaScript
16
star
68

okta-express-graphql-example

JavaScript
16
star
69

okta-spring-boot-oidc-sso-example

JavaScript
15
star
70

okta-openidconnect-appauth-android

Android Application with Okta as the IdP and AppAuth as the OpenID Connect mobile library
Java
14
star
71

example-maven-plugin

A simple example Apache Maven plugin
Java
14
star
72

okta-dotnetcore-react-example

C#
14
star
73

okta-node-express-typescript-react-example

Node.js API with TypeScript
TypeScript
14
star
74

okta-node-rest-api-example

Simple REST API with Node and OAuth 2.0
JavaScript
14
star
75

okta-angular-spring-boot-docker-example

Angular + Spring Boot Docker Example
TypeScript
14
star
76

okta-socket-io-chat-example

A simple chat application based on Socket.IO, React, and Express.
JavaScript
14
star
77

okta-spring-security-roles-example

Okta + Groups with Spring Security
Java
14
star
78

okta-ionic-4-login-example

Ionic 4 User Login and Registration Example
TypeScript
13
star
79

okta-nestjs-postgres-example

Sample application using NestJS and Postgres
TypeScript
13
star
80

okta-angular-node-example

Basic CRUD Example with Angular and Node
TypeScript
13
star
81

terraform-provider-oktaasa

Terraform Okta ASA provider
Go
13
star
82

okta-android-kotlin-crud-example

The source code for the Okta Kotlin Android CRUD tutorial.
Kotlin
13
star
83

okta-angular-mysql-timeline-example

An Angular + MySQL Timeline Example
TypeScript
13
star
84

okta-kotlin-typescript-notes-example

Kotlin and TypeScript! 🎉
TypeScript
13
star
85

auth0-spring-boot-passkeys-demo

A passkeys demo using Spring Boot and Auth0 as IdP
Java
13
star
86

okta-openidconnect-appauth-ios

iOS Sample Application with AppAuth
Swift
12
star
87

okta-angular-electron-example

TypeScript
12
star
88

spring-boot-client-credentials-example

Shell
12
star
89

okta-spring-jx-example

CI/CD for Spring Boot with Jenkins X and Kubernetes
Java
12
star
90

okta-react-docker-example

React + Docker Example
JavaScript
12
star
91

okta-oauth2-proxy-example

Java
12
star
92

okta-spring-jwt-and-opaque-token-example

Java
12
star
93

okta-spring-boot-angular-auth-code-flow-example

Package Angular and Spring Boot into a single JAR!
Java
12
star
94

okta-vanilla-js-example

A Vanilla JavaScript App with Authentication
JavaScript
12
star
95

rockstar

rockstar Chrome extension - enhance Okta
JavaScript
11
star
96

okta-php-symfony-angular-crud-example

Example CRUD App with Symfony and Angular
PHP
11
star
97

okta-appauth-xamarin-example

Xamarin.Forms authentication with OpenID Connect and Okta
C#
10
star
98

okta-node-cli-example

A command-line applications using Node.js
JavaScript
10
star
99

okta-node-express-15-minute-auth-example

Secure Authentication with Express and Okta
Handlebars
10
star
100

okta-php-trivia-react

PHP
10
star