Test Spring Boot OAuth2 login with test users

Use Spring Security OAuth2 Login to test sign-in, back-end role checks and session logout with repeatable user identities.

Verified with Spring Boot 3.5.16, Spring Security 6.5.11.

The problem

A shared identity provider makes OAuth2 Login tests depend on existing users and role assignments. A controller test with a fabricated principal can check rendering, but it skips discovery, the browser callback and sign-out.

Why HeyIdentity fits

The Generic profile supplies OpenID Connect discovery and a user picker. Spring Security uses Authorization Code with S256 PKCE for the public client. The test users’ ID-token roles drive what the server renders at /admin.

Prerequisites

Install JDK 21, Node.js 22.12 or later, and Git. The checkout includes the Gradle wrapper.

Clone the verified example

Start in the directory where you keep your repositories. These commands open the revision used for this guide. Browse the runnable example on GitHub.

git clone https://github.com/heyidentity/examples.git heyidentity-examples
cd heyidentity-examples
git checkout --detach 391c07e010699fff660fa3b4c9f4ce86cd6fa234

Set up HeyIdentity

Reuse an active configuration if it uses the Generic profile and the issuer, client and registered URLs match this example. Register http://localhost:5173/login/oauth2/code/heyidentity for sign-in and http://localhost:5173/ for sign-out. Stop the previous app before running this one, or use an unused APP_PORT and register the corresponding URLs.

For a different profile, incompatible settings or an expired configuration, start from a separate checkout so the old .env and agent token remain available. Temporary setup asks you to accept the Terms of Use. An unclaimed configuration expires after seven days. Claim or recover a setup.

Use a temporary configuration

Provision a Generic configuration from the checkout root.

node setup/provision.mjs --temporary

Use a configuration you own

Copy .env.example to .env at the checkout root. Under Connect your app, copy the issuer to OIDC_AUTHORITY. It has the shape https://app.heyidentity.dev/t/{tenantRouteId}/c/{configurationId}. Copy the public client ID from Clients to OIDC_CLIENT_ID. Register the exact callback and sign-out URLs above. Set ADMIN_USER_KEY and READER_USER_KEY to the keys of two test users with roles of ["admin"] and ["reader"] respectively.

Configure Spring Security OAuth2 Login

The app maps the root settings to Spring’s issuer-uri and client ID. The none client authentication method selects the public-client flow.

spring:
  security:
    oauth2:
      client:
        registration:
          heyidentity:
            provider: heyidentity
            client-id: "${example.clientId}"
            client-authentication-method: none
            authorization-grant-type: authorization_code
            redirect-uri: "{baseUrl}/login/oauth2/code/{registrationId}"
            scope: openid,profile
        provider:
          heyidentity:
            issuer-uri: "${example.authority}"

The controller reads roles from the validated ID token and exposes the result to the template.

        List<String> roles = user == null ? null : user.getIdToken().getClaimAsStringList("roles");
        model.addAttribute("admin", roles != null && roles.contains("admin"));

Run and verify

From the checkout root, build and start the app. On Windows, run apps\spring-boot\gradlew.bat as the wrapper command.

apps/spring-boot/gradlew -p apps/spring-boot bootJar
java -jar apps/spring-boot/build/libs/spring-boot-example.jar

Open http://localhost:5173/ and sign in as the admin. Open /admin and verify that the admin area appears. Sign out and repeat with the reader. The page should show the reader’s identity and Access denied.

For the browser suite, stop the manually started app and add these settings to the root .env. They start this app and check its callback path.

EXAMPLE_APP_COMMAND="java -jar apps/spring-boot/build/libs/spring-boot-example.jar"
EXAMPLE_OIDC_CALLBACK_PATH=/login/oauth2/code/heyidentity

Run the test commands from the checkout root.

cd playwright/typescript
npm ci
npx playwright install chromium
npx playwright test

The parallel role checks finish before sign-out invalidates the saved admin session. The suite checks that the end-session request returns the browser to the exact registered root.

Limitations

The example uses a server-side session. Build the JAR before running the browser suite and keep saved browser state out of version control. HeyIdentity is for development and testing.

Set up your own test configuration

Sign up

Try the sign-in flow in the demo