Skip to content

Instantly share code, notes, and snippets.

@Vrael
Created November 5, 2020 12:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Vrael/67440baf190289eed965813475370bc1 to your computer and use it in GitHub Desktop.
Save Vrael/67440baf190289eed965813475370bc1 to your computer and use it in GitHub Desktop.
Spring Cloud Gateway filter for SPAs nested routes would be redirected to index.html and resolve them properly.
@Component
class SPAGatewayFilter : GatewayFilter {
val logger: Logger = LoggerFactory.getLogger(SPAGatewayFilter::class.java)
companion object { const val INDEX_PATH = "/index.html" }
override fun filter(exchange: ServerWebExchange, chain: GatewayFilterChain): Mono<Void> {
val req = exchange.request
val ex = if(req.headers.accept.contains(MediaType.TEXT_HTML)) {
exchange.mutate().request {
logger.debug("Replace path: ${req.path} by $INDEX_PATH")
it.path(INDEX_PATH)
}.build()
} else exchange
return chain.filter(ex)
}
}
@Component
class SPAGatewayFilterFactory(
private val spaGatewayFilter: SPAGatewayFilter
) : AbstractGatewayFilterFactory<SPAGatewayFilterFactory.Config>(Config::class.java) {
class Config { }
override fun apply(config: Config) = spaGatewayFilter
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment