Better, Faster, Lighter Java with Java 12 and JHipster 6
Tags: java, jhiptser, security, oauth
Published: 2019-11-14 17:43:49

---
title: Better, Faster, Lighter Java with Java 12 and JHipster 6
published: true
description: Stay hip with the latest Java and Spring Boot releases by using JHipster 6! This post shows you how to get started.
tags: java, jhiptser, security, oauth 
canonical_url: https://developer.okta.com/blog/2019/04/04/java-11-java-12-jhipster-oidc
cover_image: https://developer.okta.com/assets-jekyll/blog/java-12-jhipster-6/authenticated-by-okta-c114234313c83a96ea1415c77dd665c626e49ac88487ab51513b9524176f6b9d.png
---

There’s a lot going on in the Java ecosystem lately. Now that major Java versions are released every six months, it can be difficult to keep up. Personally, I develop the majority of my Java applications using Spring Boot. Because of this, I was stuck on Java 8 until Spring Boot 2.1 was released. 

Spring Boot 2.1 added Java 11 support, along with performance improvements, a new metrics implementation, new actuator endpoints, and Spring Security’s first-class OIDC support.

I was a web developer years before I became a Java developer. I’ve been developing Java for 20 years, so that’s saying a lot! If you’re a Java developer, chances are you’re baffled by the pace of change in the JavaScript world. If you think Java is hard to keep up with, try keeping up with JavaScript and its ecosystem! The good news is there’s an excellent way to keep up with both: just use [JHipster](https://www.jhipster.tech).

JHipster 6 uses Spring Boot 2.1 for its backend API, which means you can use Java 11+ to develop your JHipster apps!

If you'd rather watch a video, I created a [screencast] (https://youtu.be/Ktnvqoouulg) of this blog post.

## How Does JHipster Make Java and JavaScript Development Easier?

JHipster is one of my favorite open-source projects on the planet. It’s a development platform to generate, develop, and deploy Spring Boot + Angular applications. In addition, it supports React, Vue, and creating microservice architectures with Netflix OSS, the Elastic Stack, and Docker.

Java developers tend to dislike JavaScript, but many are OK with TypeScript. Guess what? JHipster uses TypeScript for all its UI framework implementations!

JHipster makes Java and JavaScript development easier by configuring everything for you and using the latest versions of its underlying frameworks. Want to upgrade your app to use the latest versions? Simply run `jhipster upgrade`.

## Install Java 11 or Java 12

Before you worry about installing JHipster, install Java 11. I recommend using [SDKMAN!](https://sdkman.io/) for this task. If you don’t have it installed, it’s just a command away:

```shell
curl -s "https://get.sdkman.io" | bash
```

Once you have it installed, you can see all the Java 11 versions available using `sdk list java`.

Install the OpenJDK version:

```shell
sdk install java 11.0.2-open
```

If you’d rather use Amazon’s Corretto, simply use `11.0.2-amzn` for the name. If you want to be as hipster as possible, you can even use Java 12!

```shell
sdk install java 12.0.0-open
```

**TIP:** Azul’s Zulu is also available if you use `12.0.0-zulu` for the name. 

## Install JHipster 6

A beta of JHipster 6 was [released on May 3, 2019](https://www.jhipster.tech/2019/04/04/jhipster-release-6.0.0-beta.0.html). You can install it using the command below.

```shell
npm install -g generator-jhipster@beta
```

**NOTE**: The `npm` command is part of [Node.js](https://nodejs.org). You’ll need to have Node 10.x to install JHipster and run useful commands. 

## Create a Spring Boot + Angular App that runs on Java 11+

The most basic way to get started with JHipster is to create a new directory, cd into it, and run `jhipster`. You’ll be prompted to answer a number of questions about the app you’d like to create. Question range from the name of your app, to the authentication type you’d like to use, to SQL vs. NoSQL.

JHipster also has a domain language (called JDL for JHipster Domain Language) that you can use to define your app.

``` 
application {
  config {}
}
```

If you used the above code to create your app, it’ll use the default values:

- baseName: `jhipster`

- applicationType: `monolith`

- databaseType: `sql`

- etc.

**TIP:** You can see all the default values in [JHipster’s JDL documentation](https://www.jhipster.tech/jdl/#available-application-options). 

To generate an OAuth 2.0-enabled app with JHipster, create an `app.jh` file in a new project directory (e.g., `~/hipapp`):

``` 
application {
  config {
    baseName hipapp
    authenticationType oauth2
  }
}
```

Open a terminal window and navigate to the same directory as this file. Run the following command to generate a Spring Boot API with an Angular UI.

**WARNING:** Make sure you’re not in your home directory! Your project will be generated in the same directory as `app.jh`. 

```shell
jhipster import-jdl app.jh
```

This will create a multitude of files and install dependencies using `npm install`. Your terminal should look similar to the following when you run this command:

 ![Results of import-jdl](https://developer.okta.com/assets-jekyll/blog/java-12-jhipster-6/import-jdl-7db81bd6575856b5ad3769531b8d5f358d67dae3a1c975aefd2fea6aeb8c99bc.png)

If you’d prefer to see what this command looks line in-action, you can watch the recording below.

{% youtube sbwD697nHwM %}

Since you specified `oauth2` as the authentication type, a Docker Compose configuration will be installed for Keycloak.

[Keycloak](https://www.keycloak.org/) is an Apache-licensed open source identity and access management solution. In addition to creating a [`src/main/docker/keycloak.yml`](https://github.com/jhipster/generator-jhipster/blob/master/generators/server/templates/src/main/docker/keycloak.yml.ejs) file for Docker Compose, JHipster generates a [`src/main/docker/config/realm-config`](https://github.com/jhipster/generator-jhipster/tree/master/generators/server/templates/src/main/docker/config/realm-config) directory with files in it that configure Keycloak to work with JHipster out-of-the-box.

## Run Your JHipster App and Log in with Keycloak

Keycloak must be running for your JHipster app to start successfully. This is because Spring Security 5.1’s [first-class OIDC support](https://developer.okta.com/blog/2019/03/05/spring-boot-migration) is leveraged in JHipster 6.

This OIDC support includes discovery, which means that Spring Security talks to a `/.well-known/openid-configuration` endpoint to configure itself. I completed [the migration](https://github.com/jhipster/generator-jhipster/pull/9416) myself and deleted more code than I added!

Start Keycloak using Docker Compose:

```shell
docker-compose -f src/main/docker/keycloak.yml up -d
```

**TIP:** If you don’t have Docker Compose installed, see [these instructions](https://docs.docker.com/compose/install/) for how to install it. 

Then start your application using Maven:

```shell
./mvnw
```

When your app is up and running, open `http://localhost:8080` in your favorite browser and click **sign in**. You’ll be redirected to Keycloak, where you can enter `admin/admin` to log in.

 ![Welcome, Java Hipster](https://developer.okta.com/assets-jekyll/blog/java-12-jhipster-6/welcome-java-hipster-f98915cd9e41341e9bea62953d579dd0cdbe25dbdba7a5d1a55037f2ec310427.png)

Pretty slick, eh? You just created a modern single page application (SPA) that uses the latest released version of Angular! Not only that, but it uses the most secure form of OAuth 2.0 - [authorization code flow](https://developer.okta.com/blog/2018/04/10/oauth-authorization-code-grant-type).

**NOTE:** If you’re confused by how OAuth 2.0 and OpenID Connect (OIDC) work together, please see [What the Heck is OAuth?](https://developer.okta.com/blog/2017/06/21/what-the-heck-is-oauth) In short, OIDC is a thin layer on top of OAuth 2.0 that adds identity. 

## Use [Okta](https://developer.okta.com): We’re Always On!

Keycloak is an excellent project that works great for development and testing. However, if you use it in production, you’ll be responsible for maintaining it, updating it to the latest releases, and making sure it’s up 24/7. For these reasons, I recommend using [Okta](https://developer.okta.com) in production. After all, we’re _always on!_ 😃

### Create an OpenID Connect Web Application

To begin, sign up for a [forever-free Okta developer account](https://developer.okta.com/signup/) (or [sign in](https://login.okta.com/?SAMLRequest=fc%2B7CsJAEAXQXvAflu1NNJUMeZBGELTx1a%2FrYILJTtyZGD%2FfSBRiYzlw77lMnD3rSj3Qc0ku0YtgrhU6S5fSXRN9PKxmS52l00nMpq6iBvJWCrfDe4ss6vStRe9aDzmGIZfo1jsgwyWDMzUyiIV9vt1AH4XGk5ClSvewUgMNa%2BYW%2FVj5jxhm9NLP67QQaSAMu64L6CYmsFSHlnzT4ZlLwTgcL6Sf8%2FeX9AU%3Dhttps://login.okta.com/?SAMLRequest=fc%2B7CsJAEAXQXvAflu1NNJUMeZBGELTx1a%2FrYILJTtyZGD%2FfSBRiYzlw77lMnD3rSj3Qc0ku0YtgrhU6S5fSXRN9PKxmS52l00nMpq6iBvJWCrfDe4ss6vStRe9aDzmGIZfo1jsgwyWDMzUyiIV9vt1AH4XGk5ClSvewUgMNa%2BYW%2FVj5jxhm9NLP67QQaSAMu64L6CYmsFSHlnzT4ZlLwTgcL6Sf8%2FeX9AU%3D) to `{yourOktaDomain}` if you already have an account).

Once you’re signed in to Okta, register your client application.

- In the top menu, click on **Applications**

- Click on **Add Application**

- Select **Web** and click **Next**

- Enter `Awesome OIDC` for the **Name** (this value doesn’t matter, so feel free to change it)

- Change the Login redirect URI to be `http://localhost:8080/login/oauth2/code/oidc`

- Click **Done**, then **Edit** and add `http://localhost:8080` as a Logout redirect URI

- Click **Save**

Your settings should resemble the screenshot below when you’re finished.

 ![OIDC App Settings](https://developer.okta.com/assets-jekyll/blog/java-12-jhipster-6/app-settings-36cee2d55fa2872188a92e5c2c3c8d9d20ad2082d86b8fe6c7433c89567c3145.png)

Create an `okta.env` file in your project’s root directory and replace the `{..}` values with those from your Okta application:

```shell
export SPRING_SECURITY_OAUTH2_CLIENT_PROVIDER_OIDC_ISSUER_URI="https://{yourOktaDomain}/oauth2/default"
export SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_OIDC_CLIENT_ID="{clientId}"
export SPRING_SECURITY_OAUTH2_CLIENT_REGISTRATION_OIDC_CLIENT_SECRET="{clientSecret}"
```

**TIP:** Add `*.env` to your `.gitignore` file so this file won’t end up in your source control system.

### 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` and `ROLE_USER` group (**Users** > **Groups** > **Add Group**) and add users to them. You can use the account you signed up with, or create a new user (**Users** > **Add Person**). 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**.

 ![Add Claim](https://developer.okta.com/assets-jekyll/blog/java-12-jhipster-6/add-claim-915f54ead19da14f9117ac982daa317f6782c0f8d9fe9fcb5de68f7beb2d6f90.png)

Start your application with the following commands:

```shell
source okta.env
./mvnw
```

Navigate to `http://localhost:8080` and log in with your Okta credentials.

 ![Authenticated by Okta](https://developer.okta.com/assets-jekyll/blog/java-12-jhipster-6/authenticated-by-okta-c114234313c83a96ea1415c77dd665c626e49ac88487ab51513b9524176f6b9d.png)

Isn’t it cool how Spring Boot and Spring Security make it easy to switch OIDC providers?!

## CRUD with JHipster

I’ve barely scratched the service of what JHipster is capable of in this post. For example, you can create CRUD functionality for entities (with tests!) using JDL. For example, create a `blog.jh` file with the code below.

```
entity Blog {
  name String required minlength(3),
  handle String required minlength(2)
}

entity BlogEntry {
  title String required,
  content TextBlob required,
  date Instant required
}

entity Tag {
  name String required minlength(2)
}

relationship ManyToOne {
  Blog{user(login)} to User,
  BlogEntry{blog(name)} to Blog
}

relationship ManyToMany {
  BlogEntry{tag(name)} to Tag{entry}
}

paginate BlogEntry, Tag with infinite-scroll
```

Then run `jhipster import-jdl blog.jh` in your project. The [jdl-samples](https://github.com/jhipster/jdl-samples) GitHub repository has many more examples.

## Do More with JHipster

I’d like to thank [Joe Grandja](https://twitter.com/joe_grandja) and [Rob Winch](https://twitter.com/rob_winch) from the Spring Security team. Without their help, JHipster’s migration to use Spring Security 5.1 would not have been possible. You guys _rock!!_

I did not create a GitHub repository for this post because all of the code was generated. You can find the source code for [JHipster on GitHub](https://github.com/jhipster/generator-jhipster).

Thanks JHipster and all its fabulous [contributors](https://github.com/jhipster/generator-jhipster/graphs/contributors). You all do a tremendous amount of work in your free time and it’s greatly appreciated.

Not quite ready to make the leap to JHipster 6 and Java 11+? I’ve written a few tutorials that use JHipster 5 and Java 8.

- [Build a Photo Gallery PWA with React, Spring Boot, and JHipster](https://developer.okta.com/blog/2018/06/25/react-spring-boot-photo-gallery-pwa)

- [Develop a Microservices Architecture with OAuth 2.0 and JHipster](https://developer.okta.com/blog/2018/03/01/develop-microservices-jhipster-oauth)

- [Use Ionic for JHipster to Create Mobile Apps with OIDC Authentication](https://developer.okta.com/blog/2018/01/30/jhipster-ionic-with-oidc-authentication)

- [Build a Mobile App with React Native and Spring Boot](https://developer.okta.com/blog/2018/10/10/react-native-spring-boot-mobile-app)

I also wrote a [free mini-book on JHipster](https://www.infoq.com/minibooks/jhipster-mini-book-5) for InfoQ.

If you’d like to learn more about Spring Security 5.1 and its OIDC support, we’ve got some of those too:

- [A Quick Guide to OAuth 2.0 with Spring Security](https://developer.okta.com/blog/2019/03/12/oauth2-spring-security-guide)

- [Migrate Your Spring Boot App to the Latest and Greatest Spring Security and OAuth 2.0](https://developer.okta.com/blog/2019/03/05/spring-boot-migration)

- [Spring Boot 2.1: Outstanding OIDC, OAuth 2.0, and Reactive API Support](https://developer.okta.com/blog/2018/11/26/spring-boot-2-dot-1-oidc-oauth2-reactive-apis)

Follow us on [@oktadev](https://twitter.com/oktadev) to stay up-to-date with Java and the leading JavaScript frameworks.