On this page
Sign users in to your web app using the redirect model
Add a user sign-in flow to a server-side web app with Okta's redirect model (opens new window).
Learning outcomes
- Implement a simple redirect to an Okta-hosted sign-in page.
- Configure a server-side web app to use Okta.
- Test that users can sign in and sign out.
- Define which parts of an app require authentication and which don't.
What you need
Sample code
Overview
The easiest and most secure way to add a user sign-in flow to your server-side web app is to use an Okta-hosted Sign-In Widget. When a user attempts to sign in, the app redirects them to the widget hosted on an Okta web page. After they've signed in successfully, Okta redirects them back to the app. This is known as the redirect authentication deployment model.
Note: To use the redirect model in a single-page application (SPA), see Sign users in to your SPA using the redirect model. To use the redirect model in a mobile app, see Sign users in to your mobile app using the redirect model.
In this quickstart, you:
- Create an app integration in the Admin Console.
- Create and configure a new web app to use Okta.
- Test that a user can sign in and sign out.
- Configure different levels of access for specific areas of the site.
Tip: You need your Okta org domain to follow this tutorial. It looks like
dev-123456.okta.com
. See Find your Okta domain. Where you see{yourOktaDomain}
in this guide, replace it with your Okta domain.
Create an app integration in the Admin Console
An app integration represents your app in your Okta org. Use it to configure how your app connects with Okta services.
To create an app integration for your app:
Open the Admin Console for your org.
- Sign in to your Okta organization (opens new window) with your administrator account.
- Click Admin in the upper-right corner of the page.
Go to Applications > Applications to view the current app integrations.
Click Create App Integration.
Select OIDC - OpenID Connect as the Sign-in method.
Select Web Application as the Application type, then click Next.
Note: You can break the sign-in or sign-out flows for your app if you choose the wrong app type.
Enter an App integration name. For example, My first web application.
Enter the callback URLs for the local development of your app.
- Enter
- Enter
Note: The values suggested here are those used in the sample app.
- Enter
Select Allow everyone in your organization to access for Controlled access.
Click Save to create the app integration.
The configuration page for the new app integration appears. Keep this page open.
Note: For a complete guide to all the options not explained in this guide, see Create OIDC app integrations (opens new window).
Note your client ID and client secret
Make a note of two values that you use to configure your web app. Both are in the configuration pane for the app integration that you've created:
- Client ID: Found on the General tab in the Client Credentials section.
- Client Secret: Found on the General tab in the Client Credentials section.
Moving on, where you see {clientId}
and {clientSecret}
in this guide, replace them with your client ID and client secret.
Create and configure a new web app to use Okta
Now that you have created the app integration and noted the configuration settings, complete the following steps:
- Create a web app.
- Add the required packages to your app
- Configure your app to use Okta
- Add the pages and logic for a user to sign in and sign out
Create a web app
Add the required packages to your app
Configure your app to use Okta
Earlier you noted the client ID and client secret values generated for your app integration. Add these and your Okta domain to your app's configuration.
Add the pages and logic for a user to sign in and sign out
A user can start the sign-in process by:
- Clicking a sign-in link or button
- Trying to access a protected page, such as their profile page.
In both cases, the app redirects the browser to the Okta-hosted sign-in page. See Redirect to the sign-in page.
After the user signs in, Okta redirects the browser to the sign-in redirect URI that you entered earlier. Similarly, after a user signs out, Okta redirects the browser to the sign-out redirect URI. Both sign-in and sign-out redirect URIs are called callback routes. Users don't see callback routes, and they aren't the user's final destination. However, your app does need to implement them. See Define a callback route.
After the user signs in, Okta returns some of their profile information to your app. The default profile items (called claims) returned by Okta include the user's email address, name, and preferred username. These are sent in an ID token (opens new window) as part of the redirect to the sign-in redirect URL. See Get the user's information.
Redirect to the sign-in page
Note: To customize the Okta sign-in form, see Style the Okta-hosted Sign-In Widget.
Define a callback route
Get the user's information
Since you requested the scopes openid profile email
, Okta also returns an ID token along with the access token. You can parse out the claims in the ID token to find the user's profile information.
For example, after the user signs in, you can extract the user's name from the ID token and show it in the app:
Open src > main > java > com > example > demo > DemoApplication.java.
Add the following import statements at the top of the file:
import org.springframework.security.core.annotation.AuthenticationPrincipal; import org.springframework.security.oauth2.core.oidc.user.OidcUser; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController;
Add the following route handler to the
DemoApplication
class beneathmain()
:@GetMapping("/") public String hello(@AuthenticationPrincipal OidcUser user) { return String.format("Welcome, %s", user.getFullName()); }
Note: The claims that you see may differ depending on the scopes requested by your app. See Configure your app to use Okta and Scopes (opens new window).
Test that a user can sign in and sign out
Your site now has enough content to sign a user in with Okta, prove they've signed in, and sign them out. Test it by starting your server and signing a user in.
Start your app with Spring Boot:
mvn spring-boot:run
Open a browser and go to
http://localhost:8080
. The browser redirects you to Okta to sign in using the Sign-In Widget.After you sign in, check that your user's name appears.
Configure different levels of access for specific areas of the site
Your app can require authentication for the entire site or just for specific routes. Routes that don't require authentication are accessible without signing in, which is also called anonymous access.
Require authentication for everything
Some apps require user authentication for all routes, for example a company intranet.
The Okta Spring Boot starter is secure-by-default, meaning that all of your routes are protected, which is the equivalent of:
@EnableWebSecurity
public class SecurityConfiguration {
@Bean
SecurityFilterChain oauth2SecurityFilterChain(HttpSecurity http) throws Exception {
http.authorizeRequests((requests) -> requests.anyRequest().authenticated());
// enables OAuth redirect login
http.oauth2Login();
// enables OAuth Client configuration
http.oauth2Client();
// enables REST API support for JWT bearer tokens
http.oauth2ResourceServer(OAuth2ResourceServerConfigurer::jwt);
return http.build();
}
}
Require authentication for a specific route
Your website may have a protected portion that is only available to authenticated users.
You can configure that as follows in your Spring app:
http.authorizeRequests(request ->
request.antMatchers("/checkout/**").authenticated())
Or, you might want to only allow a specific group to access a section of your site:
http.authorizeHttpRequests(authorize ->
authorize.mvcMatchers("/admin/**").hasAuthority("Admin Group"))
Configure a groups
claim in your authorization server for this to work.
Allow anonymous access
Your website may enable anonymous access for some content but require a user to sign in for other content or to take some other action. For example, an ecommerce site might allow a user to browse anonymously and add items to a cart, but require a user to sign in for checkout and payment.
You can allow anonymous access for specific URLs using Spring Security's permitAll
in your configuration:
http.authorizeHttpRequests(authorize ->
authorize.mvcMatchers("/", "/about").permitAll())
Next steps
- Protect your API endpoints.
- Custom domain and email address
- Style the Okta-hosted Sign-In Widget.
- Sign users in to your mobile app using the redirect model
- Multi-tenant solutions
Spring Security documentation for how to protect routes:
Okta Developer Blog: