Skip to content

Fix OAuth2 login redirect when using context path#1892

Open
omerlebo wants to merge 2 commits into
kafbat:mainfrom
omerlebo:fix/oauth2-context-path-login-redirect
Open

Fix OAuth2 login redirect when using context path#1892
omerlebo wants to merge 2 commits into
kafbat:mainfrom
omerlebo:fix/oauth2-context-path-login-redirect

Conversation

@omerlebo

@omerlebo omerlebo commented Jun 23, 2026

Copy link
Copy Markdown

Closes #1893

What's changed

OAuth2 login did not set an authenticationSuccessHandler, so it fell back to Spring Security's default handler, which is not aware of the servlet context path (SERVER_SERVLET_CONTEXT_PATH / server.servlet.context-path).

When kafka-ui is hosted under a base path (e.g. /kafka), the post-login 302 redirect omits the context path. The browser is sent to a location that doesn't include /kafka, so the user is stranded after authenticating instead of being routed back into the UI. Manually navigating to /kafka after login works, confirming auth itself succeeds — only the redirect target is wrong.

The LOGIN_FORM and LDAP configs already attach emptyRedirectSuccessHandler() (which prepends the context path via EmptyRedirectStrategy):

// BasicAuthSecurityConfig / LdapSecurityConfig
.formLogin(form -> form
    .loginPage(LOGIN_URL)
    .authenticationSuccessHandler(emptyRedirectSuccessHandler()))

This PR applies the same handler to oauth2Login for parity:

.oauth2Login(oauth2 -> oauth2
    .authenticationManager(delegatingAuthManager)
    .authenticationSuccessHandler(emptyRedirectSuccessHandler()))

emptyRedirectSuccessHandler() is already defined in the shared AbstractAuthSecurityConfig base class — it simply wasn't wired into the OAuth2 flow.

Tests

Adds EmptyRedirectStrategyTest covering the context-path behavior the fix depends on (and which was previously untested):

  • prepends context path to a root (/) redirect → /kafka/
  • prepends context path to an absolute-path redirect → /kafka/ui/clusters
  • leaves the location unchanged when no context path is configured
  • does not rewrite absolute-URL redirects (e.g. external IdP URLs)

How to reproduce

  1. Configure OAuth2/OIDC auth (auth.type=OAUTH2).
  2. Set SERVER_SERVLET_CONTEXT_PATH=/kafka.
  3. Log in — the post-login 302 redirect does not point to /kafka, leaving the user on a blank/unrouted page. Manually visiting /kafka works.

Affects main and the latest release (v1.5.0).

Summary by CodeRabbit

  • Bug Fixes

    • Improved OAuth login redirect handling by ensuring successful authentication uses the configured “empty redirect” behavior, keeping redirects consistent with the app’s context path.
  • Tests

    • Added JUnit coverage for redirect rewriting behavior (root redirects, context-path-prefixed paths, unchanged locations when context path is empty, and no rewriting for absolute external URLs).

OAuth2 login did not set an authenticationSuccessHandler, so it fell
back to Spring Security's default handler which is not aware of the
servlet context path (SERVER_SERVLET_CONTEXT_PATH). When the app is
hosted under a base path (e.g. /kafka), the post-login 302 redirect
omitted the context path, leaving the user stranded instead of being
routed back into the UI.

The LOGIN_FORM and LDAP configs already attach emptyRedirectSuccessHandler()
(which prepends the context path via EmptyRedirectStrategy); apply the
same handler to oauth2Login for parity.

Also adds unit coverage for EmptyRedirectStrategy.
@omerlebo omerlebo requested a review from a team as a code owner June 23, 2026 13:50
@kapybro kapybro Bot added status/triage/manual Manual triage in progress and removed status/triage/manual Manual triage in progress labels Jun 23, 2026
@coderabbitai

coderabbitai Bot commented Jun 23, 2026

Copy link
Copy Markdown

Review Change Stack

📝 Walkthrough

Walkthrough

OAuthSecurityConfig.configure(...) is updated to explicitly set authenticationSuccessHandler(emptyRedirectSuccessHandler()) on the oauth2Login DSL, in addition to the existing authenticationManager. A new JUnit 5 test class, EmptyRedirectStrategyTest, validates the EmptyRedirectStrategy redirect URI rewriting logic across four scenarios.

Changes

OAuth Empty Redirect Handler

Layer / File(s) Summary
OAuth2 login success handler wiring and redirect strategy tests
api/src/main/java/io/kafbat/ui/config/auth/OAuthSecurityConfig.java, api/src/test/java/io/kafbat/ui/util/EmptyRedirectStrategyTest.java
oauth2Login now calls authenticationSuccessHandler(emptyRedirectSuccessHandler()). Four tests assert that EmptyRedirectStrategy prepends the context path to root and path-based redirects, leaves Location unchanged for an empty context path, and does not rewrite absolute external URLs.

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~8 minutes

Poem

🐇 Hop, hop, hooray — no more wild redirect!
The context path now leads where we expect,
oauth2Login wired just so,
The empty handler guides the flow,
Four tests confirm no wrong turn's checked! ✅

🚥 Pre-merge checks | ✅ 4 | ❌ 1

❌ Failed checks (1 warning)

Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (4 passed)
Check name Status Explanation
Title check ✅ Passed The title 'Fix OAuth2 login redirect when using context path' directly and concisely summarizes the main change: fixing OAuth2 login redirect handling for context paths.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

@github-actions github-actions Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi omerlebo! 👋

Welcome, and thank you for opening your first PR in the repo!

Please wait for triaging by our maintainers.

Please take a look at our contributing guide.

@kapybro

kapybro Bot commented Jun 23, 2026

Copy link
Copy Markdown

AI Summary

OAuth2 login redirects ignore the servlet context path, causing users to be redirected to a URL without the base path (e.g., /kafka) after authentication, which results in a broken navigation experience. The fix applies the existing emptyRedirectSuccessHandler() to the OAuth2 flow, ensuring the context path is properly prepended to redirect URLs, consistent with how form and LDAP logins are handled. A new test confirms the redirect strategy correctly handles context paths for various redirect scenarios.

@kapybro kapybro Bot changed the title BE: Fix OAuth2 login redirect ignoring context path Fix OAuth2 login redirect when using context path Jun 23, 2026
@kapybro kapybro Bot added area/auth App authentication related issues impact/api A PR with changes which affect API scope/backend Related to backend changes type/bug Something isn't working labels Jun 23, 2026

@coderabbitai coderabbitai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 1

🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.

Inline comments:
In `@api/src/test/java/io/kafbat/ui/util/EmptyRedirectStrategyTest.java`:
- Around line 48-57: The test method doesNotRewriteAbsoluteUrlRedirects only
covers the case where an absolute URL has an explicit path segment (like
/login), but the EmptyRedirectStrategy.createLocation() method has a separate
code path for URLs with empty paths. Add a new test method to
EmptyRedirectStrategyTest that tests the scenario with an absolute URL that has
no path (e.g., https://auth.example.com without the /login suffix). In this new
test, create an exchange with a context path, pass an absolute URI with an empty
path to the STRATEGY.sendRedirect() method, and verify the actual behavior when
the path is empty string to ensure the implementation correctly handles this
case.
🪄 Autofix (Beta)

Fix all unresolved CodeRabbit comments on this PR:

  • Push a commit to this branch (recommended)
  • Create a new PR with the fixes

ℹ️ Review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ffcf7470-538d-4406-b826-30086b792b84

📥 Commits

Reviewing files that changed from the base of the PR and between e78f6a6 and a59c107.

📒 Files selected for processing (2)
  • api/src/main/java/io/kafbat/ui/config/auth/OAuthSecurityConfig.java
  • api/src/test/java/io/kafbat/ui/util/EmptyRedirectStrategyTest.java

Comment thread api/src/test/java/io/kafbat/ui/util/EmptyRedirectStrategyTest.java
Addresses review feedback: add a case for an absolute URL with an empty
path, which hits the createLocation() empty-path branch and is made
context-relative. Documents/verifies actual behavior so both branches
are covered.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/auth App authentication related issues impact/api A PR with changes which affect API scope/backend Related to backend changes type/bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

OAuth2 login redirect ignores servlet context path

1 participant