Skip to content

Commit

Permalink
Forward all headers
Browse files Browse the repository at this point in the history
  • Loading branch information
louptheron committed Oct 23, 2024
1 parent 4079257 commit 7055b79
Showing 1 changed file with 21 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package fr.gouv.cnsp.monitorfish.infrastructure.api.proxy

import fr.gouv.cnsp.monitorfish.config.OIDCProperties
import fr.gouv.cnsp.monitorfish.infrastructure.monitorenv.APIControlUnitRepository
import jakarta.servlet.http.HttpServletRequest
import org.slf4j.Logger
import org.slf4j.LoggerFactory
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty
import org.springframework.cloud.gateway.mvc.ProxyExchange
import org.springframework.http.ResponseEntity
Expand All @@ -21,6 +24,8 @@ import org.springframework.web.bind.annotation.RestController
class KeycloakProxyController (
private val oidcProperties: OIDCProperties,
) {
private val logger: Logger = LoggerFactory.getLogger(KeycloakProxyController::class.java)

@GetMapping("/realms/**")
@Throws(Exception::class)
fun get(
Expand All @@ -36,6 +41,8 @@ class KeycloakProxyController (
"$key=${values.joinToString(",")}"
}.let { targetUri.append(it) }
}
logger.info("Forwarding $request to $targetUri")
logger.info("With cookies ${request.cookies}")

// Extract cookies from the request
val cookies = request.cookies
Expand All @@ -45,6 +52,20 @@ class KeycloakProxyController (
proxy.header("Cookie", cookieHeader)
}

// Forward all headers from the incoming request to the proxy
val headerNames = request.headerNames
logger.info("Headers are $headerNames")
while (headerNames.hasMoreElements()) {
val headerName = headerNames.nextElement()
val headerValues = request.getHeaders(headerName)

// Forward all values for each header
while (headerValues.hasMoreElements()) {
proxy.header(headerName, headerValues.nextElement())
}
}

logger.info("Sending $proxy")
return proxy.uri(targetUri.toString()).get()
}

Expand Down

0 comments on commit 7055b79

Please sign in to comment.