Skip to content

Commit

Permalink
Merge pull request #2 from oktadeveloper/heroku
Browse files Browse the repository at this point in the history
  • Loading branch information
Matt Raible committed Dec 31, 2020
2 parents 8c7f576 + 52f910a commit 32ce0f7
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 10 deletions.
76 changes: 70 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ This example shows how to create a Spring Boot application, secure it, and build
> [Okta](https://developer.okta.com/) has Authentication and User Management APIs that reduce development time with instant-on, scalable user infrastructure. Okta's intuitive API and expert support make it easy for developers to authenticate, manage and secure users and roles in any application.
* [Getting Started](#getting-started)
* [Deploy to Heroku](#deploy-to-heroku)
* [Links](#links)
* [Help](#help)
* [License](#license)
Expand All @@ -32,25 +33,88 @@ okta register
okta apps create
```

Select **Web** > **Other**. Use `http://localhost:8080/login/oauth2/code/okta` as the Redirect URI. This will create an `.okta.env` file in your project's root directory. Source it and run it to confirm you can log in to Okta.

```bash
source .okta.env
./gradlew bootRun
```

Open your browser to `http://localhost:8080` and sign in.

Build an application image into local Docker:

```sh
```bash
./gradlew bootBuildImage --imageName=springbootdemo
```

Create an `.env` file in the project root and copy your Okta application settings from the `.okta.env` file:

```sh
OKTA_OAUTH2_ISSUER=https://dev-xxxxxx.okta.com/oauth2/default
OKTA_OAUTH2_CLIENT_SECRET=viVC58i6MzQHzAz9BeXjzhWpSz8qbg6U5B4RXnre
OKTA_OAUTH2_CLIENT_ID=zoa1bzlj7DWmzSI8o5d6
```bash
OKTA_OAUTH2_ISSUER=https://dev-ZZZ.okta.com/oauth2/default
OKTA_OAUTH2_CLIENT_SECRET=XXX
OKTA_OAUTH2_CLIENT_ID=YYY
```

Run your bootiful application!

```sh
```bash
docker run -it -p8080:8080 --env-file .env springbootdemo
```

## Deploy to Heroku

If you'd like to deploy your dockerized Spring Boot app to Heroku, you'll need to use [Heroku Buildpacks](https://jkutner.github.io/2020/05/19/spring-boot-buildpacks.html). This is because the Paketo buildpacks refuse to allocate heap on containers smaller than 1GB of RAM. A free Heroku dyno has 512MB.

First, you'll need to add the following to `src/main/resources/application.properties` so Spring Boot uses Heroku's `PORT` environment variable.

```properties
server.port=${PORT:8080}
```

Then, build your image with `--builder heroku/spring-boot-buildpacks`:

```bash
./gradlew bootBuildImage --imageName=springbootdemo --builder heroku/spring-boot-buildpacks
```

Create an app on Heroku:

```bash
heroku create
```

Then, log in to Heroku's container registry and push your app:

```bash
heroku container:login
docker tag springbootdemo registry.heroku.com/<your-app-name>/web
docker push registry.heroku.com/<your-app-name>/web
```

Set your Okta app settings as environment variables:

```bash
heroku config:set \
OKTA_OAUTH2_ISSUER="https://{yourOktaDomain}/oauth2/default" \
OKTA_OAUTH2_CLIENT_ID="{clientId}" \
OKTA_OAUTH2_CLIENT_SECRET="{clientSecret}"
```

Next, release your container and tail the logs.

```bash
heroku container:release web
heroku logs --tail
```

You'll need to update your Okta OIDC app to have your Heroku app's redirect URIs as well.

- Login redirect URI: `https://<your-app-name>.herokuapp.com/login/oauth2/code/okta`
- Logout redirect URI: `https://<your-app-name>.herokuapp.com`

Run `heroku open` to open your app and sign in.

## Links

This example uses the following open source libraries from Okta:
Expand Down
6 changes: 3 additions & 3 deletions build.gradle.kts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import org.jetbrains.kotlin.gradle.tasks.KotlinCompile

plugins {
id("org.springframework.boot") version "2.4.0"
id("org.springframework.boot") version "2.4.1"
id("io.spring.dependency-management") version "1.0.10.RELEASE"
kotlin("jvm") version "1.4.10"
kotlin("plugin.spring") version "1.4.10"
kotlin("jvm") version "1.4.21"
kotlin("plugin.spring") version "1.4.21"
}

group = "com.okta"
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1 +1 @@

server.port=${PORT:8080}

0 comments on commit 32ce0f7

Please sign in to comment.