This is an automated email from the ASF dual-hosted git repository.
lizhimins pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/rocketmq-dashboard.git
The following commit(s) were added to refs/heads/master by this push:
new 4673fb61f fix(auth): clear request identity at async handoff (#7218)
4673fb61f is described below
commit 4673fb61f0834f20b8b389d6fa3c20bd5b2d19be
Author: aias00 <[email protected]>
AuthorDate: Fri Nov 27 12:04:57 2026 +0800
fix(auth): clear request identity at async handoff (#7218)
---
.../rocketmq/studio/auth/AuthInterceptor.java | 12 +-
.../studio/auth/AuthInterceptorAsyncTest.java | 128 +++++++++++++++++++++
.../pages/instance/__tests__/ConsumerPage.test.tsx | 7 ++
3 files changed, 145 insertions(+), 2 deletions(-)
diff --git
a/server/src/main/java/org/apache/rocketmq/studio/auth/AuthInterceptor.java
b/server/src/main/java/org/apache/rocketmq/studio/auth/AuthInterceptor.java
index 6f9eee0ff..d3da7c366 100644
--- a/server/src/main/java/org/apache/rocketmq/studio/auth/AuthInterceptor.java
+++ b/server/src/main/java/org/apache/rocketmq/studio/auth/AuthInterceptor.java
@@ -27,13 +27,13 @@ import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.web.cors.CorsUtils;
-import org.springframework.web.servlet.HandlerInterceptor;
+import org.springframework.web.servlet.AsyncHandlerInterceptor;
import java.net.URLDecoder;
import java.nio.charset.StandardCharsets;
import java.util.Set;
-public class AuthInterceptor implements HandlerInterceptor {
+public class AuthInterceptor implements AsyncHandlerInterceptor {
/**
* The paths a session with an outstanding password change may still call:
rotating the password
@@ -239,6 +239,14 @@ public class AuthInterceptor implements HandlerInterceptor
{
+ ",\"message\":\"" + message + "\",\"data\":null}");
}
+ @Override
+ public void afterConcurrentHandlingStarted(HttpServletRequest request,
HttpServletResponse response,
+ Object handler) {
+ // MVC skips afterCompletion on the original thread when a handler
starts async work.
+ // Redispatch authenticates again in preHandle; the servlet pool must
not retain this identity.
+ AuthenticatedUserContext.clear();
+ }
+
@Override
public void afterCompletion(HttpServletRequest request,
HttpServletResponse response,
Object handler, Exception ex) throws Exception
{
diff --git
a/server/src/test/java/org/apache/rocketmq/studio/auth/AuthInterceptorAsyncTest.java
b/server/src/test/java/org/apache/rocketmq/studio/auth/AuthInterceptorAsyncTest.java
new file mode 100644
index 000000000..0d8ea45cb
--- /dev/null
+++
b/server/src/test/java/org/apache/rocketmq/studio/auth/AuthInterceptorAsyncTest.java
@@ -0,0 +1,128 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.rocketmq.studio.auth;
+
+import org.apache.rocketmq.studio.ops.ai.tool.catalog.ToolCatalog;
+import org.apache.rocketmq.studio.settings.SettingsRepository;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Test;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.test.web.servlet.MockMvc;
+import org.springframework.test.web.servlet.MvcResult;
+import org.springframework.test.web.servlet.setup.MockMvcBuilders;
+import org.springframework.web.bind.annotation.GetMapping;
+import org.springframework.web.bind.annotation.RestController;
+import org.springframework.web.servlet.mvc.method.annotation.SseEmitter;
+
+import java.util.Optional;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.asyncDispatch;
+import static
org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
+import static
org.springframework.test.web.servlet.result.MockMvcResultMatchers.request;
+import static
org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
+
+class AuthInterceptorAsyncTest {
+ private final AuthService authService = mock(AuthService.class);
+ private final StreamController controller = new StreamController();
+
+ @AfterEach
+ void clearContext() {
+ controller.emitter.complete();
+ AuthenticatedUserContext.clear();
+ }
+
+ @Test
+ void asyncHandoffClearsOriginalThreadAndRedispatchAuthenticatesAgainTest()
throws Exception {
+ AuthProperties properties = new AuthProperties();
+ properties.setLoginRequired(true);
+ when(authService.getAuthenticatedUser("Bearer token"))
+ .thenReturn(Optional.of(new LoginVO.UserInfo(17L,
"stream-admin", true)));
+ AuthInterceptor interceptor = new AuthInterceptor(properties,
authService,
+ mock(SettingsRepository.class), mock(ToolCatalog.class));
+ MockMvc mvc =
MockMvcBuilders.standaloneSetup(controller).addInterceptors(interceptor).build();
+
+ MvcResult pending = mvc.perform(get("/api/context-test/stream")
+ .header(HttpHeaders.AUTHORIZATION, "Bearer token"))
+ .andExpect(request().asyncStarted())
+ .andReturn();
+
+ assertThat(controller.username).isEqualTo("stream-admin");
+ assertThat(controller.userId).isEqualTo("17");
+ assertThat(controller.admin).isTrue();
+ assertCleanContext();
+
+ // Async redispatch must reauthenticate rather than relying on the
initial principal.
+ when(authService.getAuthenticatedUser("Bearer
token")).thenReturn(Optional.empty());
+ controller.emitter.complete();
+
mvc.perform(asyncDispatch(pending)).andExpect(status().isUnauthorized());
+ assertCleanContext();
+ }
+
+ @Test
+ void synchronousCompletionStillClearsRequestThreadTest() throws Exception {
+ AuthProperties properties = new AuthProperties();
+ properties.setLoginRequired(true);
+ when(authService.getAuthenticatedUser("Bearer token"))
+ .thenReturn(Optional.of(new LoginVO.UserInfo(17L,
"stream-admin", true)));
+ MockMvc mvc = MockMvcBuilders.standaloneSetup(controller)
+ .addInterceptors(new AuthInterceptor(properties, authService,
+ mock(SettingsRepository.class),
mock(ToolCatalog.class))).build();
+
+
mvc.perform(get("/api/context-test/sync").header(HttpHeaders.AUTHORIZATION,
"Bearer token"))
+ .andExpect(status().isOk());
+
+ assertThat(controller.username).isEqualTo("stream-admin");
+ assertCleanContext();
+ }
+
+ private static void assertCleanContext() {
+
assertThat(AuthenticatedUserContext.currentUsernameOrSystem()).isEqualTo(AuthenticatedUserContext.SYSTEM_ACTOR);
+ assertThat(AuthenticatedUserContext.currentUserId()).isNull();
+ assertThat(AuthenticatedUserContext.currentUserIsAdmin()).isFalse();
+ }
+
+ @RestController
+ static class StreamController {
+ private final SseEmitter emitter = new SseEmitter(60_000L);
+ private String username;
+ private String userId;
+ private boolean admin;
+
+ @GetMapping(value = "/api/context-test/stream", produces =
MediaType.TEXT_EVENT_STREAM_VALUE)
+ SseEmitter stream() {
+ captureIdentity();
+ return emitter;
+ }
+
+ @GetMapping("/api/context-test/sync")
+ String sync() {
+ captureIdentity();
+ return "ok";
+ }
+
+ private void captureIdentity() {
+ username = AuthenticatedUserContext.currentUsernameOrSystem();
+ userId = AuthenticatedUserContext.currentUserId();
+ admin = AuthenticatedUserContext.currentUserIsAdmin();
+ }
+ }
+}
diff --git a/web/src/pages/instance/__tests__/ConsumerPage.test.tsx
b/web/src/pages/instance/__tests__/ConsumerPage.test.tsx
index cd5ec98e9..4fe8cbb24 100644
--- a/web/src/pages/instance/__tests__/ConsumerPage.test.tsx
+++ b/web/src/pages/instance/__tests__/ConsumerPage.test.tsx
@@ -454,6 +454,7 @@ describe('Consumer page', () => {
cleanup();
Modal.destroyAll();
});
+ vi.useRealTimers();
// 静态提示由全局清理卸载,避免冷调用 destroy 再创建异步 root。
});
@@ -1415,6 +1416,9 @@ describe('Consumer page', () => {
});
it('keeps the latest client stack when an older request resolves last',
async () => {
+ // Keep the modal’s 2s diagnostic poll from consuming the request fixtures
while
+ // userEvent is interacting with the modal on a busy CI worker. Timeouts
stay real.
+ vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
const firstStack = {
groupName: 'remote-cg',
clientId: 'client-1',
@@ -1631,6 +1635,9 @@ describe('Consumer page', () => {
});
it('highlights inconsistent subscriptions and refreshes the check result',
async () => {
+ // Keep the modal’s 2s diagnostic poll from consuming the request fixtures
while
+ // userEvent is interacting with the modal on a busy CI worker. Timeouts
stay real.
+ vi.useFakeTimers({ toFake: ['setInterval', 'clearInterval'] });
vi.mocked(consumerService.getConsumerSubscriptions)
.mockResolvedValueOnce([
{