From bf6f2c869677344019625246b8adce1f500e041c Mon Sep 17 00:00:00 2001 From: ngqile Date: Wed, 2 Dec 2026 15:45:12 +0800 Subject: [PATCH] feat(pages): open the three public landing-page reads in the gateway Only GET of a published page, a page image and an active company's branding, each anchored to its exact shape. Writes still need a token. Co-Authored-By: Claude Opus 5.5 Claude-Session: https://claude.ai/code/session_01EFUzfiCVCgCVJZq4NDEqZs --- openspec/changes/add-landing-pages/tasks.md | 2 +- .../solvway/gateway/filter/JWTAuthFilter.java | 4 + .../filter/JWTAuthFilterPagesPathTest.java | 82 +++++++++++++++++++ 3 files changed, 87 insertions(+), 1 deletion(-) create mode 100644 solvway-api-gateway/src/test/java/com/solvway/gateway/filter/JWTAuthFilterPagesPathTest.java diff --git a/openspec/changes/add-landing-pages/tasks.md b/openspec/changes/add-landing-pages/tasks.md index 57594a7..92ec67f 100644 --- a/openspec/changes/add-landing-pages/tasks.md +++ b/openspec/changes/add-landing-pages/tasks.md @@ -46,7 +46,7 @@ Verify commands: ## 4. Gateway -- [ ] 4.1 Open paths for the published page read, the image read and the branding read. Tests show that page writes and image uploads still need a token. +- [x] 4.1 Open paths for the published page read, the image read and the branding read. Tests show that page writes and image uploads still need a token. - Security code: read the whole diff. - Verify: `mvn -T 1C -B -q -pl solvway-api-gateway -am verify` diff --git a/solvway-api-gateway/src/main/java/com/solvway/gateway/filter/JWTAuthFilter.java b/solvway-api-gateway/src/main/java/com/solvway/gateway/filter/JWTAuthFilter.java index 8d21843..d3e9868 100644 --- a/solvway-api-gateway/src/main/java/com/solvway/gateway/filter/JWTAuthFilter.java +++ b/solvway-api-gateway/src/main/java/com/solvway/gateway/filter/JWTAuthFilter.java @@ -56,6 +56,10 @@ public class JWTAuthFilter implements GlobalFilter, Ordered { publicPath(HttpMethod.GET, "/counter/checkin/findEvent/[A-Za-z0-9_-]{22}"), publicPath(HttpMethod.POST, "/counter/checkin/lookup"), publicPath(HttpMethod.POST, "/counter/checkin/byGuest"), + // Public landing pages: a published page, a page image and an active company's logo and colour. Writes need a token. + publicPath(HttpMethod.GET, "/counter/cmsPage/findPublished/\\d+/[A-Za-z0-9-]{1,80}"), + publicPath(HttpMethod.GET, "/counter/pageImage/\\d+"), + publicPath(HttpMethod.GET, "/company/branding/[A-Za-z0-9-]{2,20}"), // Password hint on public password screens: switched-on rule names and numbers only, no user data publicPath(HttpMethod.GET, "/passwordRule/adminActive"), // WhatsApp webhook forward from mobile-api: no user token; the service re-verifies Meta's signature itself diff --git a/solvway-api-gateway/src/test/java/com/solvway/gateway/filter/JWTAuthFilterPagesPathTest.java b/solvway-api-gateway/src/test/java/com/solvway/gateway/filter/JWTAuthFilterPagesPathTest.java new file mode 100644 index 0000000..f23ca06 --- /dev/null +++ b/solvway-api-gateway/src/test/java/com/solvway/gateway/filter/JWTAuthFilterPagesPathTest.java @@ -0,0 +1,82 @@ +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; + +/** Public landing pages open three reads without a user token: a published page, an image and a company's branding. Writes stay closed. */ +class JWTAuthFilterPagesPathTest { + + 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 publicPageReadsNeedNoUserToken() { + for (String path : new String[] {"/counter/cmsPage/findPublished/1/hotel-technology", "/counter/pageImage/42", "/company/branding/INS", "/company/branding/cv-2"}) { + assertThat(passes(call(HttpMethod.GET, path))).as(path).isTrue(); + } + } + + @Test + void pageWritesAndOtherReadsStillNeedAToken() { + for (String[] c : new String[][] { + {"POST", "/counter/cmsPage/save"}, + {"POST", "/counter/cmsPage/search"}, + {"POST", "/counter/cmsPage/delete"}, + {"GET", "/counter/cmsPage/findById/3"}, + {"POST", "/counter/pageImage/upload"}, + {"GET", "/counter/pageImage/findCompany/42"}, + {"POST", "/counter/pageImage/42"}, + {"GET", "/counter/pageImage/42/x"}, + {"GET", "/counter/cmsPage/findPublished/1/hotel-technology/x"}, + {"GET", "/counter/cmsPage/findPublished/x/hotel-technology"}, + {"POST", "/company/1/branding"}, + {"GET", "/company/branding/INS/x"} }) { + MockServerWebExchange exchange = call(HttpMethod.valueOf(c[0]), c[1]); + assertThat(passes(exchange)).as(c[0] + " " + c[1]).isFalse(); + assertThat(exchange.getResponse().getStatusCode()).as(c[0] + " " + c[1]).isEqualTo(HttpStatus.UNAUTHORIZED); + } + } +}