This article is part of a series that explores the implementation of a Single Spring Boot application called StarVote. The application will use Keycloak or Okta as Identity Provider.
In the introductory article, we outline the sections we will cover:
Here’s a sneak peek of how the StarVote application will be at the end!
In this particular article, we’ll explore how to enable authentication using Okta as an Identity Provider, covering the necessary configurations and integration steps.
In the “Okta Admin Dashboard” main menu on the left, click “Applications” menu and then “Applications” sub-menu;
On the next page, click Create App Integration button;
Select OIDC - OpenID Connect as “Sign on method”, Web Application as “Application type”, and click Next;
In the “New Web App Integration” form:
Set star-vote in “App integration name” field;
Check Authorization Code and Implicit (hybrid) in “Grant type”;
For “Sign-in redirect URIs”, add http://localhost:8080/login/oauth2/code/okta and http://localhost:8080/api/callback/token;
For “Sign-out redirect URIs”, add http://localhost:8080;
For “Controlled access”, select Skip group assignment for now;
Click Save;
5. The Client ID and Client Secret are generated. The Okta Domain can be obtained by clicking the button-menu present on the up-right corner of the screen.
Create groups
In the “Okta Admin Dashboard” main menu on the left, click “Directory” menu and then “Groups” sub-menu;
Let’s create the Admin’s group:
Click Add Group;
Enter STAR-VOTE-ADMIN for the “Name” text-field;
Click Save;
3. Let’s repeat the same process to create the Users’s group:
Click Add Group;
Enter STAR-VOTE-USER for the “Name” text-field;
Click Save;
Add an Admin
In the “Okta Admin Dashboard” main menu on the left, click “Directory” menu and then “People” sub-menu;
The “Primary email” will be filled with the same content as the “Username”;
In “Groups”, start typing STAR. The STAR-VOTE-ADMIN group will pop up. Select it to add it;
In “Password”, select 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.
Assign Groups to Application
In the “Okta Admin Dashboard” main menu on the left, click “Directory” menu and then “Groups” sub-menu;
Select the STAR-VOTE-ADMIN group by clicking on its name;
Click “Applications” tab and then, click Assign Applications;
Click the Assign button related to star-vote application and then click Done;
Repeat the same process for the STAR-VOTE-USER group.
Add Claim
In the “Okta Admin Dashboard” main menu on the left, click “Security” menu and then “API” sub-menu;
In “Authorization Servers” tab, select the default by clicking on its name;
In “Claims” tab, click Add Claim;
Enter the following information for the new claim:
For the “Name”, type groups;
For the “Include in token type” field, select Access Token and keep Always in the right field;
For the Value type, select Groups;
For the “Filter” field, select Matches regrex and set .* in the right field
For the “Include in” select Any scope;
Click Create.
Enable the Registration of New Users
In the “Okta Admin Dashboard” main menu on the left, click “Directory” menu and then “Self-Service Registration” sub-menu;
In the “Self-Service Registration” screen, click Enable Registration button or, in case you have already done it before, edit the “Self-Service Registration” form;
Make sure the “Self-service registration” field is set to Enabled;
In “Registration” section, set STAR-VOTE-USER group to the field “Assign to group”;
In “Post-registration” section, “Activation requirements” field, disable the checkbox that says: “User must verify email address to be activated”;
Click Save.
That is it. Okta is configured!
Backend & Frontend Modifications
Update the application.properties
Let’s add the following lines to application.properties file:
The jwt.auth.converter.principal-attribute property configures the principal attribute. It determines how the user is identified in the JWT.
The spring.security.oauth2.resourceserver.jwt.issuer-uri property specifies the URI of the issuer for JWT tokens. The issuer URI is used to verify the authenticity and integrity of the JWT token received by the resource server;
The spring.security.oauth2.client.provider.okta.issuer-uri specifies the URI of the OpenID Connect (OIDC) provider (in this case, Okta) for the OAuth 2.0 client. This URI is used to configure the client to authenticate and interact with the OIDC provider during the OAuth 2.0 flow;
The spring.security.oauth2.client.provider.okta.user-name-attribute specifies the attribute in the JWT token that represents the username or user identifier. The username attribute is used to identify the user in the application;
The spring.security.oauth2.client.registration.okta.clientId specifies the client ID for the OAuth 2.0 client.
The spring.security.oauth2.client.registration.okta.clientSecret specifies the client secret for the OAuth 2.0 client;
The spring.security.oauth2.client.registration.okta.scope specifies the scope requested by the OAuth 2.0 client during the authorization process. Scopes define the permissions and access rights requested by the client. In this configuration, the client requests the openid, profile, and email scopes, which are commonly used to access basic user information and profile details.
Update the login.html
Let’s apply the following change (highlighted in bold) in login.html:
The th:href="@{/oauth2/authorization/okta}" is a Thymeleaf attribute used in an anchor tag (<a>) to specify the URL for the Okta authentication. This endpoint initiates the OAuth2 authorization flow with the chosen identity provider. When the user clicks on the corresponding link, it triggers the authentication process and redirects them to the appropriate authorization page.
Update the StarAPIController class
Let’s add a new endpoint in StarAPIControlle class:
The callbackToken method handles a POST request sent to the /api/callback/token endpoint. This endpoint is used to handle the callback from Okta after successful authentication, extracting relevant information from the request.
Update the WebSecurityConfig class
In WebSecurityConfig class, let’s implement the following changes:
Let’s modify the configuration to allow unrestricted access to perform HTTP POST requests to the /api/callback/token endpoint;
Provide the implementation to the extractRoles method.
Once the application is started, let’s open a browser and access: http://localhost:8080.
Then, click the Login button present on the top-right of the screen and select Okta.
The Okta login page will appear.
Enter [email protected] in the “Username” field, and provide the password you used during the admin creation in the “Password” field.
You are in! 🎉
Feel free to add your favorite movie stars or even register as a new user on StarVote! 😃
Testing StarVote API
Once the application is started, we can submit requests to its endpoints. Let’s try to retrieve all the stars. So, open another terminal and run the following command:
curl -i http://localhost:8080/api/stars
This command will return an HTTP 401 response, as the endpoint is now secured.
In order to access it, we need to provide an Bearer access token.
In a terminal, create the following environment variables:
This command should return an HTTP 200 response with an array of stars (maybe empty in case you haven’t added any star).
The API is working great! 🎉
8. To shut it down, go to the terminal where it’s running and press Ctrl+C.
Okta Cleanup
Once you no longer require the Okta configuration, let’s clean up the configuration that we have set up.
Delete Admin
In the “Okta Admin Dashboard” main menu on the left, click “Directory” menu and then “People” sub-menu;
Select the App Admin by clicking on its name;
In App Admin profile, click More Actions multi-button and then Deactivate and confirm deactivation;
Still in App Admin profile, click Delete button and confirm deletion;
Delete Groups
In the “Okta Admin Dashboard” main menu on the left, click “Directory” menu and then “Groups” sub-menu;
Select STAR-VOTE-ADMIN group by clicking on its name;
In STAR-VOTE-ADMIN profile, click Actions and then click Delete sub-button. Confirm deletion by clicking Delete Group button;
Do the same process to STAR-VOTE-USER group.
Delete Application
In the “Okta Admin Dashboard” main menu on the left, click “Applications” menu and then “Applications” sub-menu;
In Application list whose status is ACTIVE, click star-vote's gear icon and then click Deactivate; Confirm deactivation by clicking Deactivate Application button;
Now, in Application list whose status is INACTIVE, click star-vote's gear icon and then click Delete. Confirm deletion by clicking Delete Application button;
Delete Claim
In the “Okta Admin Dashboard” main menu on the left, click “Security” menu and then “API” sub-menu;
In “Authorization Servers” tab, select the default by clicking on its name;
In “Claims” tab, click the x icon related to the groups claim created;
Confirm deletion by clicking OK button.
Disable the Registration of New Users
In the “Okta Admin Dashboard” main menu on the left, click “Directory” menu and then “Self-Service Registration” sub-menu;
In the “Self-Service Registration” screen, click the edit link present on the right of the screen;
Select the Disabled for “Self-service registration” field;
Click Save.
That’s it
This concludes the series where we provided detailed explanations on implementing the StarVote application using Spring Boot and securing it with Keycloak or Okta as Identity Provider.