From faf14e5fac4d72d84ee1b6057d0baa53af06dee2 Mon Sep 17 00:00:00 2001 From: Janis Blatsios Date: Wed, 17 Apr 2024 09:46:46 +0200 Subject: [PATCH] =?UTF-8?q?WIP=20-=20jako=C5=9B=20dzia=C5=82a?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../janis/komornik/filter/SpaWebFilter.java | 32 +++++++++++++++++++ 1 file changed, 32 insertions(+) create mode 100644 src/main/java/pl/janis/komornik/filter/SpaWebFilter.java diff --git a/src/main/java/pl/janis/komornik/filter/SpaWebFilter.java b/src/main/java/pl/janis/komornik/filter/SpaWebFilter.java new file mode 100644 index 0000000..2f77b51 --- /dev/null +++ b/src/main/java/pl/janis/komornik/filter/SpaWebFilter.java @@ -0,0 +1,32 @@ +package pl.janis.komornik.filter; + +import jakarta.servlet.FilterChain; +import jakarta.servlet.ServletException; +import jakarta.servlet.http.HttpServletRequest; +import jakarta.servlet.http.HttpServletResponse; +import org.springframework.lang.NonNull; +import org.springframework.web.filter.OncePerRequestFilter; + +import java.io.IOException; + +public class SpaWebFilter extends OncePerRequestFilter { + + /** + * Forwards any unmapped paths (except those containing a period) to the client {@code index.html}. + */ + @Override + protected void doFilterInternal(HttpServletRequest request, @NonNull HttpServletResponse response, + @NonNull FilterChain filterChain) throws ServletException, IOException { + String path = request.getRequestURI(); + if (!path.startsWith("/api") && + !path.startsWith("/login") && + !path.startsWith("/auth") && + !path.contains(".") && + path.matches("/(.*)")) { + request.getRequestDispatcher("/index.html").forward(request, response); + return; + } + + filterChain.doFilter(request, response); + } +}