fix(gateway): only label reads skip the user token, not message writes

Any URL containing /message/ skipped the user token, saves included. Only the
catalog reads that services and the sign-in page make stay open now, each an
exact GET path, plus the enabled-language list.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs
This commit is contained in:
ngqileandClaude Opus 5.5 committed 2026-12-02 12:44:38 +08:00
1 parent ebfee582c2
commit 45bd81cdbb
3 files changed
+100 -3

No files matched your search

@@ -0,0 +1,89 @@
package com.solvway.gateway.filter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.solvway.gateway.model.AuthAppKey;
import com.solvway.gateway.model.AuthAppKeyMap;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.mock.http.server.reactive.MockServerHttpRequest;
import org.springframework.mock.web.server.MockServerWebExchange;
import org.springframework.test.util.ReflectionTestUtils;
import reactor.core.publisher.Mono;
import java.net.InetSocketAddress;
import java.util.concurrent.atomic.AtomicBoolean;
import static org.assertj.core.api.Assertions.assertThat;
/** Message and language reads that services and the sign-in page make stay open; saves, search and full listings need a user token. */
class JWTAuthFilterMessagePathTest {
private static final String KEY = "gateway-test-key";
private JWTAuthFilter filter;
@BeforeEach
void setUp() throws Exception {
AuthAppKey key = new ObjectMapper().readValue(
"{\"key\":\"" + KEY + "\",\"type\":\"BO\",\"ipList\":[\"0.0.0.0/0\"],\"allowList\":[\"/\"]}",
AuthAppKey.class);
AuthAppKeyMap map = new AuthAppKeyMap();
map.add(KEY, key);
filter = new JWTAuthFilter();
ReflectionTestUtils.setField(filter, "authAppKeyMap", map);
}
private MockServerWebExchange call(HttpMethod method, String path) {
return MockServerWebExchange.from(MockServerHttpRequest.method(method, path)
.header("api-key", KEY)
.remoteAddress(new InetSocketAddress("127.0.0.1", 40000)));
}
private boolean passes(MockServerWebExchange exchange) {
AtomicBoolean called = new AtomicBoolean(false);
GatewayFilterChain chain = ex -> {
called.set(true);
return Mono.empty();
};
filter.filter(exchange, chain).block();
return called.get();
}
@Test
void catalogReadsNeedNoUserToken() {
for (String path : new String[] {
"/message/catalogVersion",
"/message/getAllMessages/A",
"/message/getMessageJsonByAreaAndLocale/A/zh_CN",
"/message/getMessagesByCode/Leads",
"/message/getMessagesByCodeAndArea/Leads/A",
"/message/findByCodeAndLocale/Leads/zh_CN",
"/message/getMessageJsonForMobile/zh_CN",
"/locale/findAllEnabled" }) {
assertThat(passes(call(HttpMethod.GET, path))).as(path).isTrue();
}
}
@Test
void writesSearchAndFullListingsNeedAUserToken() {
for (String[] c : new String[][] {
{"POST", "/message/save"},
{"POST", "/message/search"},
{"GET", "/message/findAll"},
{"GET", "/message/findById/7"},
{"GET", "/message/findAllPageable"},
{"POST", "/message/catalogVersion"},
{"GET", "/message/catalogVersion/x"},
{"POST", "/locale/save"},
{"POST", "/locale/update"},
{"GET", "/locale/findAll"},
{"POST", "/locale/findAllEnabled"} }) {
MockServerWebExchange ex = call(HttpMethod.valueOf(c[0]), c[1]);
assertThat(passes(ex)).as(c[0] + " " + c[1]).isFalse();
assertThat(ex.getResponse().getStatusCode()).as(c[0] + " " + c[1]).isEqualTo(HttpStatus.UNAUTHORIZED);
}
}
}