Created
November 5, 2020 12:53
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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