From 45419f23c11ae7a87ba36a3aeb785a974d0957ab Mon Sep 17 00:00:00 2001 From: litoral05 Date: Wed, 3 Jun 2026 14:35:41 +0100 Subject: [PATCH] Route proxy using authenticated client --- .../client/ClientRepository.java | 1 + .../client/ClientResolver.java | 33 +++++++++++++++++++ .../gateway/BackendProxyService.java | 24 +++++++++----- 3 files changed, 49 insertions(+), 9 deletions(-) create mode 100644 src/main/java/com/litoralregas/backend_gateway/client/ClientResolver.java diff --git a/src/main/java/com/litoralregas/backend_gateway/client/ClientRepository.java b/src/main/java/com/litoralregas/backend_gateway/client/ClientRepository.java index bb43487..6a4b059 100644 --- a/src/main/java/com/litoralregas/backend_gateway/client/ClientRepository.java +++ b/src/main/java/com/litoralregas/backend_gateway/client/ClientRepository.java @@ -7,4 +7,5 @@ import java.util.Optional; public interface ClientRepository extends JpaRepository { Optional findByName(String name); + Optional findByIdAndEnabledTrue(Long id); } \ No newline at end of file diff --git a/src/main/java/com/litoralregas/backend_gateway/client/ClientResolver.java b/src/main/java/com/litoralregas/backend_gateway/client/ClientResolver.java new file mode 100644 index 0000000..f052731 --- /dev/null +++ b/src/main/java/com/litoralregas/backend_gateway/client/ClientResolver.java @@ -0,0 +1,33 @@ +package com.litoralregas.backend_gateway.client; + +import com.litoralregas.backend_gateway.security.AuthenticatedUser; +import org.springframework.security.core.Authentication; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Service; + +@Service +public class ClientResolver { + + private final ClientRepository clientRepository; + + public ClientResolver(ClientRepository clientRepository) { + this.clientRepository = clientRepository; + } + + public ClientEntity resolveCurrentClient() { + + Authentication authentication = + SecurityContextHolder.getContext().getAuthentication(); + + if (authentication == null) { + throw new RuntimeException("Not authenticated"); + } + + AuthenticatedUser user = + (AuthenticatedUser) authentication.getPrincipal(); + + return clientRepository + .findByIdAndEnabledTrue(user.clientId()) + .orElseThrow(() -> new RuntimeException("Client not found")); + } +} \ No newline at end of file diff --git a/src/main/java/com/litoralregas/backend_gateway/gateway/BackendProxyService.java b/src/main/java/com/litoralregas/backend_gateway/gateway/BackendProxyService.java index 97c0c27..1bb9f84 100644 --- a/src/main/java/com/litoralregas/backend_gateway/gateway/BackendProxyService.java +++ b/src/main/java/com/litoralregas/backend_gateway/gateway/BackendProxyService.java @@ -1,5 +1,6 @@ package com.litoralregas.backend_gateway.gateway; +import com.litoralregas.backend_gateway.client.ClientResolver; import jakarta.servlet.http.HttpServletRequest; import org.springframework.http.HttpMethod; import org.springframework.http.HttpStatus; @@ -12,19 +13,18 @@ import org.springframework.web.reactive.function.client.WebClientResponseExcepti public class BackendProxyService { private final WebClient webClient; - private final ProxyProperties proxyProperties; + private final ClientResolver clientResolver; - public BackendProxyService(WebClient webClient, ProxyProperties proxyProperties) { + public BackendProxyService( + WebClient webClient, + ClientResolver clientResolver + ) { this.webClient = webClient; - this.proxyProperties = proxyProperties; + this.clientResolver = clientResolver; } public String getHealth() { - return webClient.get() - .uri(proxyProperties.getBackendBaseUrl() + "/actuator/health") - .retrieve() - .bodyToMono(String.class) - .block(); + return "UP"; } public ResponseEntity proxy(HttpServletRequest request, String body) { @@ -33,7 +33,7 @@ public class BackendProxyService { String query = request.getQueryString(); String targetUrl = - proxyProperties.getBackendBaseUrl() + resolveBackendUrl() + path + (query != null ? "?" + query : ""); @@ -76,4 +76,10 @@ public class BackendProxyService { .body("Backend unavailable"); } } + + private String resolveBackendUrl() { + return clientResolver + .resolveCurrentClient() + .getBackendBaseUrl(); + } } \ No newline at end of file