The goal of this project is to implement an application where a user can manage (create/read/update/delete) jobs. For it, we will create a backend Restful API called jobs-api
and a frontend application called jobs-ui
. Furthermore, we will use Okta
to secure the complete application.
On ivangfr.github.io, I have compiled my Proof-of-Concepts (PoCs) and articles. You can easily search for the technology you are interested in by using the filter. Who knows, perhaps I have already implemented a PoC or written an article about what you are looking for.
- [Medium] Implementing and Securing a Simple Spring Boot REST API with Okta
- [Medium] Implementing and Securing a Simple Spring Boot UI (Thymeleaf + RBAC) with Okta
- [Medium] Implementing and Securing a Spring Boot GraphQL API with Okta
- [Medium] Building a Single Spring Boot App with Keycloak or Okta as IdP: Introduction
-
Spring Boot
Web Java application that exposes a REST API for managing jobs. The job data are stored in Elasticsearch It has some endpoints that are secured.Okta
is used to handle authentication and authorization.The table below shows the endpoins, whether they are secured or not, and the authorization role required to access the secured ones.
Endpoint Secured Role GET /actuator/*
No POST /callback/token
No GET /api/jobs/newest
No POST /api/jobs
Yes JOBS_STAFF
PUT /api/jobs/{id}
Yes JOBS_STAFF
DELETE /api/jobs/{id}
Yes JOBS_STAFF
GET /api/jobs/{id}
Yes JOBS_STAFF, JOBS_CUSTOMER
PUT /api/jobs/search
Yes JOBS_STAFF, JOBS_CUSTOMER
-
React
frontend application where customers can look for a job and staff members can handle jobs. In order to access it, a person must login. The authentication is handled byOkta
.
- If you do not have a Developer Edition Account, you can create one at https://developer.okta.com/signup/
- If you already have, access https://developer.okta.com/login/
The picture below shows how Okta Admin Dashboard
looks like
- In the
Okta Admin Dashboard
main menu on the left, clickApplications
menu and thenApplications
sub-menu - On the next page, click
Create App Integration
button - Select
OIDC - OpenID Connect
as Sign on method andSingle-Page Application
as Application type. ClickNext
button - Enter the following values in the form.
- General Settings
- App integration name:
Jobs Portal SPA
- Grant type: check
Authorization Code
andImplicit (hybrid)
- Sign-in redirect URIs:
http://localhost:3000/implicit/callback
andhttp://localhost:8080/callback/token
- Sign-out redirect URIs:
http://localhost:3000
- App integration name:
- Assignments
- Controlled access:
Skip group assignment for now
- Controlled access:
- General Settings
- Click
Save
button - On the next screen, the
Client ID
ofJobs Portal SPA
is displayed. TheOkta Domain
can be obtained by clicking the button-menu present on the up-right corner of the screen.
- In the
Okta Admin Dashboard
main menu on the left, clickDirectory
menu and thenGroups
sub-menu - Add Staff Group
- Click
Add Group
button - Enter
JOBS_STAFF
for the Name text-field - Click
Save
button
- Click
- Add Customer Group
- Click
Add Group
button - Enter
JOBS_CUSTOMER
for the Name text-field - Click
Save
button
- Click
- In the
Okta Admin Dashboard
main menu on the left, clickDirectory
menu and thenPeople
sub-menu - Click
Add person
button - Enter the following information for the Staff person
- First name:
Mario
- Last name:
Bros
- Username:
mario.bros@test.com
- Primary email:
mario.bros@test.com
- Groups:
JOBS_STAFF
(the group will popup; select it to add it) - Password:
Set by admin
- Set a strong password in the text-field that will appear
Uncheck
the check-box that says "User must change password on first login"- Click
Save and Add Another
button
- First name:
- Enter the following information for the Customer person
- First name:
Luigi
- Last name:
Bros
- Username:
luigi.bros@test.com
- Primary email:
luigi.bros@test.com
- Groups:
JOBS_CUSTOMER
(the group will popup; select it to add it) - Password:
Set by admin
- Set a strong password in the text-field that will appear
- Leave
Uncheck
the check-box that says "User must change password on first login" - Click
Save
button
- First name:
- In the
Okta Admin Dashboard
main menu on the left, clickDirectory
menu and thenGroups
sub-menu - Select
JOBS_STAFF
- Click
Applications
tab - Click
Assign Applications
button - Click the
Assign
button related toJobs Portal SPA
and then clickDone
- In the
Okta Admin Dashboard
main menu on the left, clickGroups
sub-menu again - Select
JOBS_CUSTOMER
- Click
Applications
tab - Click
Assign Applications
button - Click the
Assign
button related toJobs Portal SPA
and then clickDone
- In the
Okta Admin Dashboard
main menu on the left, clickSecurity
menu and thenAPI
sub-menu - In
Authorization Servers
tab, select thedefault
- In
Claims
tab, clickAdd Claim
- Enter the following information for the claim
- Name:
groups
- Include in token type:
- Select
Access Token
- Keep
Always
in the right field
- Select
- Value type:
Groups
- Filter:
- Select
Matches regrex
- Set
.*
in the right field
- Select
- Include in:
Any scope
- Click
Create
button
- Name:
-
Open a terminal and inside
okta-springboot-react
root folder rundocker compose up -d
-
Wait for
elasticsearch
Docker container to be up and running. To check it, rundocker compose ps
-
jobs-api
-
In a terminal, navigate to
okta-springboot-react/jobs-api
folder -
Export the following environment variables. Those values were obtained while adding Application
export OKTA_DOMAIN=... export OKTA_CLIENT_ID=...
-
Run the
Maven
command below to startjobs-api
./mvnw clean spring-boot:run
-
-
jobs-ui
-
Open a new terminal and navigate to
okta-springboot-react/jobs-ui
folder -
Create a file called
.env.local
with the following content. Those values were obtained while adding ApplicationREACT_APP_OKTA_ORG_URL=https://<OKTA_DOMAIN> REACT_APP_OKTA_CLIENT_ID=<OKTA_CLIENT_ID>
-
If you are running
jobs-ui
for the first time, execute thenpm
command belownpm install
-
To start
jobs-api
runnpm start
It will open
job-ui
in a browser automatically.
-
Application | URL |
---|---|
jobs-api | http://localhost:8080/swagger-ui.html |
jobs-ui | http://localhost:3000 |
-
Open a browser and access http://localhost:3000
-
Click
Login
in the navigation bar -
The Okta login page will appear. Enter the username & password of the person added at the step
Configuring Okta > Add people
and clickSign In
. -
Done!
Note: If you are using the person
luigi.bros@test.com
, you will not be able to create/update/delete a job because it doesn't have the required role for it.
In order to use just the jobs-api
endpoints, you must have an JWT
access token. Below are the steps to get it.
-
In a terminal, create the following environment variables. Those values were obtained while adding Application
OKTA_DOMAIN=... OKTA_CLIENT_ID=...
-
Get Okta Access Token Url
OKTA_ACCESS_TOKEN_URL="https://${OKTA_DOMAIN}/oauth2/default/v1/authorize?\ client_id=${OKTA_CLIENT_ID}\ &redirect_uri=http://localhost:8080/callback/token\ &scope=openid\ &response_type=token\ &response_mode=form_post\ &state=state\ &nonce=myNonceValue" echo $OKTA_ACCESS_TOKEN_URL
-
Copy the Okta Access Token Url from the previous step and paste it in a browser
-
The Okta login page will appear. Enter the username & password of the person added at the step
Configuring Okta > Add people
and clickSign In
button -
It will redirect to
/callback/token
endpoint ofjobs-api
and theAccess token
will be displayed, together with other information{ "state": "state", "access_token": "eyJraWQiOiJyNFdY...", "token_type": "Bearer", "expires_in": "3600", "scope": "openid" }
Note: In jwt.io, you can decode and verify the
JWT
access token
-
GET /api/jobs/newest
The
api/jobs/newest
endpoint is public, so we can access it without any problem.curl -i http://localhost:8080/api/jobs/newest?number=2
It should return
HTTP/1.1 200 [{"id":"uuulE2sBTYouQKNL1uoV", ...},{"id":"u-ulE2sBTYouQKNL1-qb", ...}]
-
GET /api/jobs
without Access TokenTry to get the list of jobs without informing the access token.
curl -i http://localhost:8080/api/jobs
It should return
HTTP/1.1 401
-
GET /api/jobs
with Access TokenFirst, get the access token as explained in
Getting Access Token
section. Then, create an environment variable for the access token.ACCESS_TOKEN=...
Call the get the list of jobs informing the access token
curl -i http://localhost:8080/api/jobs -H "Authorization: Bearer $ACCESS_TOKEN"
Response
HTTP/1.1 200 {"content":[{"id":"uISqEWsBpDcNLtN2kZv3","title":"Expert Java Developer - Cloud","company":"Microsoft","logoUrl"...}
Note: If you are using the person
luigi.bros@test.com
, you will not be able to create/update/delete a job because it doesn't have the required role for it.
-
First, get the access token as explained in
Getting Access Token
section. -
Open
jobs-api
Swagger website http://localhost:8080/swagger-ui.html -
Click
Authorize
button. Paste the access token in theValue
field. Then, clickAuthorize
andClose
to finalize. -
Done! You can now access the sensitive endpoints.
Note: If you are using the person
luigi.bros@test.com
, you will not be able to create/update/delete a job because it doesn't have the required role for it.
-
Go to the terminals where
jobs-api
andjobs-ui
are running and pressCtrl+C
-
To stop and remove containers, network and volumes, run in a terminal the following command inside
okta-springboot-react
root folderdocker compose down -v
- In the
Okta Admin Dashboard
main menu on the left, clickDirectory
menu and thenPeople
sub-menu - Click
Mario Bros
in the People list - In
Mario Bros
profile, clickMore Actions
multi-button and thenDeactivate
- Confirm deactivation by clicking
Deactivate
button - Still in
Mario Bros
profile, clickDelete
button - Confirm deletion by clicking
Delete
button - Click
Luigi Bros
in the People list - In
Luigi Bros
profile, clickMore Actions
multi-button and thenDeactivate
- Confirm deactivation by clicking
Deactivate
button - Still in
Luigi Bros
profile, clickDelete
button - Confirm deletion by clicking
Delete
button
- In the
Okta Admin Dashboard
main menu on the left, clickDirectory
menu and thenGroups
sub-menu - Select
JOBS_CUSTOMER
- In
JOBS_CUSTOMER
profile, clickActions
button and thenDelete
sub-button - Confirm deletion by clicking
Delete Group
button - Select
JOBS_STAFF
- In
JOBS_STAFF
profile, clickActions
button and thenDelete
sub-button - Confirm deletion by clicking
Delete Group
button
- In the
Okta Admin Dashboard
main menu on the left, clickApplications
menu and thenApplications
sub-menu - In Application list whose status is
ACTIVE
, clickJobs Portal SPA
'sgear
icon and then clickDeactivate
- Confirm deactivation by clicking
Deactivate Application
button - In Application list whose status is
INACTIVE
, clickJobs Portal SPA
'sgear
icon and then clickDelete
- Confirm deletion by clicking
Delete Application
button
- In the
Okta Admin Dashboard
main menu on the left, clickSecurity
menu and thenAPI
sub-menu - In
Authorization Servers
tab, select thedefault
- In
Claims
tab, click thex
icon related to thegroups
claim created - Confirm deletion by clicking
OK
button
-
In a terminal, make sure you are in
okta-springboot-react/jobs-ui
folder -
Run the following commands
npm upgrade npm i -g npm-check-updates ncu -u npm install