Skip to content

Commit

Permalink
add more commands
Browse files Browse the repository at this point in the history
  • Loading branch information
rideam committed Oct 6, 2023
1 parent df2edc3 commit 8afe5a5
Show file tree
Hide file tree
Showing 2 changed files with 92 additions and 18 deletions.
2 changes: 1 addition & 1 deletion astro/src/components/quickstarts/springboot-prereqs.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
- [Docker](https://www.docker.com): The quickest way to stand up FusionAuth. Ensure you also have [docker compose](https://docs.docker.com/compose/) installed.
- (Alternatively, you can [Install FusionAuth Manually](https://fusionauth.io/docs/v1/tech/installation-guide/)).

The example repository already has a Spring Boot template application set up that includes a Maven wrapper. You can find out more by viewing:
The example repository already has a Spring Boot application set up that includes a Maven wrapper. You can find out more by viewing:

- [Spring Initializr](https://start.spring.io/): The Spring Boot template generator webpage.
- [Maven Wrapper](https://maven.apache.org/wrapper/): A self-contained version of the Maven build tool which will download the application dependencies and compile the source code.
Expand Down
108 changes: 91 additions & 17 deletions astro/src/content/quickstarts/quickstart-springboot-web.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ In this section, you’ll build the integration with FusionAuth, which will allo

### Project Settings

From the `your-application` directory, open `src/main/resources/application.properties` and add the following.
Open the `src/main/resources/application.properties` file and add the following configuration settings.

<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/application.properties"/>

Expand All @@ -155,27 +155,46 @@ The properties with the `spring.security.oauth2.provider` prefix tell Spring whe

### Add Controllers

Find the base directory where the main application Java class is defined. In your starter package, the main class is `SpringwebApplication.java` in `src/main/java/io/fusionauth/quickstart/springweb`.
In your starter package, the main class which is the entrypoint of the application is `SpringwebApplication.java` in `src/main/java/io/fusionauth/quickstart/springweb`.

<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/java/io/fusionauth/quickstart/springweb/SpringwebApplication.java"
lang="java"/>

Create a file named `HomeController.java` in the `src/main/java/io/fusionauth/quickstart/springweb` directory. Then copy the following code for the `HomeController` Java class into the file.
<Aside type="note">All the commands that follow presume you are running the command from the `your-application` root project directory</Aside>

Create a file named `HomeController.java` in the `src/main/java/io/fusionauth/quickstart/springweb` directory. To do so you can run the command below from `your-application` directory.

```shell
touch src/main/java/io/fusionauth/quickstart/springweb/HomeController.java
```

Then copy the following code for the `HomeController` Java class into the file.

<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/java/io/fusionauth/quickstart/springweb/HomeController.java"
lang="java"/>

This controller serves the page for unauthenticated users on the index `/` route and shows the application name, which is injected from the `spring.application.name` property in the `application.properties` file. The `home` return string tells Spring to render the `home.html` template which you will create later.

Create another file in the `src/main/java/io/fusionauth/quickstart/springweb` directory. This time, name it `AccountController.java` and paste the following code into it.
Create another file in the `src/main/java/io/fusionauth/quickstart/springweb` directory. This time, name it `AccountController.java`.

```shell
touch src/main/java/io/fusionauth/quickstart/springweb/AccountController.java
```

Paste the following code into the `AccountController.java` file.

<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/java/io/fusionauth/quickstart/springweb/AccountController.java"
lang="java"/>

This controller serves the page for authenticated users on the `/account` route and adds the claims from the `OidcUser` returned from FusionAuth to the view model on the `profile` attribute. The `account` return string tells Spring to render the `account.html` template which you will create later.

Next, create a file named `MakeChangeController.java` in the `src/main/java/io/fusionauth/quickstart/springweb` directory. Then, copy the following code into the newly created file.
Next, create a file named `MakeChangeController.java` in the `src/main/java/io/fusionauth/quickstart/springweb` directory.

```shell
touch src/main/java/io/fusionauth/quickstart/springweb/MakeChangeController.java
```

Then, copy the following code into the newly created file.

<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/java/io/fusionauth/quickstart/springweb/MakeChangeController.java"
lang="java"/>
Expand All @@ -184,7 +203,19 @@ This controller serves both GET and POST requests on the `/make-change` route. B

### Add The Model Object

Now you'll create the model object referenced in the controllers. In the project base directory `src/main/java/io/fusionauth/quickstart/springweb`, create a new directory called `model`. In the `model` directory, and create a new file named `Change.java`. Now, populate it with the follow code for the `Change` class.
Now you'll create the model object referenced in the controllers. In the directory `src/main/java/io/fusionauth/quickstart/springweb`, create a new directory called `model`.

```shell
mkdir src/main/java/io/fusionauth/quickstart/springweb/model
```

In the `model` directory, and create a new file named `Change.java`.

```shell
touch src/main/java/io/fusionauth/quickstart/springweb/model/Change.java
```

Now, populate it with the follow code for the `Change` Java class.

<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/java/io/fusionauth/quickstart/springweb/model/Change.java"
lang="java" />
Expand All @@ -210,30 +241,58 @@ Create the template files in `src/main/resources/templates`.

Create the `home.html` template file.

```shell
touch src/main/resources/templates/home.html
```

Then paste the following code into it.

<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/templates/home.html"
lang="html" />

This template links to `/account` for login. Spring will automatically know that the user is not logged in and redirect them to FusionAuth based on what was defined in `application.properties`.

Create the `account.html` template file in the `src/main/resources/templates` directory.

```shell
touch src/main/resources/templates/account.html
```

Then paste the following code into it.

<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/templates/account.html"
lang="html" />

This page has an `a href` link to `/logout` that Spring will use to redirect the user to FusionAuth for logout. It also references the `${profile.email}` claim that was set in the account controller.

Create the `make-change.html` template file in the `src/main/resources/templates` directory.

```shell
touch src/main/resources/templates/make-change.html
```

Then paste the following code into it.

<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/templates/make-change.html"
lang="html" />

In addition to the references made above, this template adds a form that binds the `${change}` object as the model, submits the `*{total}` as input by the user, and displays the message or error depending on what is defined in the `Change` object.

### Configure Spring Web And Spring Security

From the base directory `src/main/java/io/fusionauth/quickstart/springweb`, add a new directory called `config` and add the following two files to it.
Add a new directory called `config` under `src/main/java/io/fusionauth/quickstart/springweb` using the following command.

```shell
mkdir src/main/java/io/fusionauth/quickstart/springweb/config
```

Add a `SecurityConfiguration.java` file to the `src/main/java/io/fusionauth/quickstart/springweb/config` directory using the following command.

```shell
touch src/main/java/io/fusionauth/quickstart/springweb/config/SecurityConfiguration.java
```

First, `SecurityConfiguration.java`, which contains the following code.
Then paste the following code into it.

<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/java/io/fusionauth/quickstart/springweb/config/SecurityConfiguration.java"
lang="java" />
Expand All @@ -243,7 +302,13 @@ This class uses the `@Configuration` annotation to provide additional configurat
- Require authentication for all requests *except* to `/` (`home.html`), `/images`, and `/css` (implicitly referring to the directories under `src/main/resources/static`).
- Redirect the user on logout to FusionAuth to complete the logout.

Second, `WebConfig.java`, which contains the code below.
Also add a `WebConfig.java`, to the `src/main/java/io/fusionauth/quickstart/springweb/config` directory.

```shell
touch src/main/java/io/fusionauth/quickstart/springweb/config/WebConfig.java
```

Then paste the following code into it.

<RemoteCode url="https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/java/io/fusionauth/quickstart/springweb/config/WebConfig.java"
lang="java" />
Expand All @@ -254,24 +319,29 @@ This configuration class enables Web MVC and sets up resource handlers for the s

The template files you added to the `templates` directory reference a stylesheet and images. Create a `css` and `images` directory under `src/main/resources/static/` to place the stylesheet and images.

In `src/main/resources/static/css`, add `styles.css` from [the example styles](https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/static/css/styles.css) or write your own.

```shell
mkdir src/main/resources/static/css
mkdir src/main/resources/static/images
```
curl -o styles.css 'https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/static/css/styles.css'

Add a `styles.css` file to the `src/main/resources/static/css` directory using the command below.

```shell
curl -o src/main/resources/static/css/styles.css 'https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/static/css/styles.css'
```

You can update the stylesheet as you see fit, but be sure to update the templates if you make changes.

In `src/main/resources/static/images`, download the [bank logo image](https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/static/images/example_bank_logo.svg) as `example_bank_logo.svg` and the [money image](https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/static/images/money.jpg) as `money.jpg`.
Download the [bank logo image](https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/static/images/example_bank_logo.svg) as `example_bank_logo.svg` and the [money image](https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/static/images/money.jpg) as `money.jpg` into the `src/main/resources/static/images` directory using the commands below.

```
curl -o example_bank_logo.svg 'https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/static/images/example_bank_logo.svg'
curl -o money.jpg 'https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/static/images/money.jpg'
```shell
curl -o src/main/resources/static/images/example_bank_logo.svg 'https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/static/images/example_bank_logo.svg'
curl -o src/main/resources/static/images/money.jpg 'https://raw.githubusercontent.com/FusionAuth/fusionauth-quickstart-springboot-web/main/complete-application/src/main/resources/static/images/money.jpg'
```

### Run The Application

From the root directory, run the application using the following command.
From the root `your-application` directory, run the application using the following command.

```shell
./mvnw spring-boot:run
Expand Down Expand Up @@ -314,6 +384,10 @@ Ensure FusionAuth is running in the Docker container. You should be able to log

Ensure the value for `spring.security.oauth2.client.registration.fusionauth-client.client-id` in the file `src/main/resources/application.properties` matches client the Id configured in FusionAuth for the "Example App" Application at [http://localhost:9011/admin/application/](http://localhost:9011/admin/application/).

* I get an error "package org.springframework.x.x.x does not exist".

Ensure to you have added the required dependencies `OAuth2 Client`, `Thymeleaf` and `Spring Web` when configuring your starter package on the [Spring Initializr](https://start.spring.io/) site.

* It still doesn't work.

You can always pull down a complete running application and compare what's different.
Expand Down

0 comments on commit 8afe5a5

Please sign in to comment.