summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorzhangshuai <[email protected]>2024-08-08 13:40:50 +0800
committerzhangshuai <[email protected]>2024-08-08 13:40:50 +0800
commita32327ad22b8067616222dcc63886b831401a46f (patch)
tree93978cd0d5d420b2d47feb5bce792389b522092a
parentf7caf6262cc42e902d4c1894e6a840cf2149a98b (diff)
feat: ASW-31 asw controller 增加 GUIHistoryRouterFilter
-rw-r--r--src/main/java/net/geedge/asw/common/config/GUIHistoryRouterFilter.java49
1 files changed, 49 insertions, 0 deletions
diff --git a/src/main/java/net/geedge/asw/common/config/GUIHistoryRouterFilter.java b/src/main/java/net/geedge/asw/common/config/GUIHistoryRouterFilter.java
new file mode 100644
index 0000000..a6e2a6f
--- /dev/null
+++ b/src/main/java/net/geedge/asw/common/config/GUIHistoryRouterFilter.java
@@ -0,0 +1,49 @@
+package net.geedge.asw.common.config;
+
+import jakarta.servlet.FilterChain;
+import jakarta.servlet.ServletException;
+import jakarta.servlet.http.HttpServletRequest;
+import jakarta.servlet.http.HttpServletResponse;
+import org.springframework.beans.factory.annotation.Value;
+import org.springframework.core.io.Resource;
+import org.springframework.core.io.ResourceLoader;
+import org.springframework.stereotype.Component;
+import org.springframework.web.filter.OncePerRequestFilter;
+import java.io.IOException;
+import java.nio.file.Files;
+import java.util.Arrays;
+import java.util.List;
+
+@Component
+public class GUIHistoryRouterFilter extends OncePerRequestFilter {
+
+ @Value("${router.prefixes:/static/,/api/}")
+ private String pathPrefixes;
+
+ private final ResourceLoader resourceLoader;
+
+ public GUIHistoryRouterFilter(ResourceLoader resourceLoader) {
+ this.resourceLoader = resourceLoader;
+ }
+
+ @Override
+ protected void doFilterInternal(HttpServletRequest request,
+ HttpServletResponse response,
+ FilterChain filterChain) throws ServletException, IOException {
+
+ List<String> prefixes = Arrays.asList(pathPrefixes.split(","));
+ String path = request.getRequestURI();
+ boolean matches = prefixes.stream().anyMatch(path::startsWith);
+
+ if (!matches) {
+ // If the path does not start with any of the specified prefixes, return index.html
+ Resource resource = resourceLoader.getResource("classpath:public/index.html");
+ response.setContentType("text/html");
+ response.setCharacterEncoding("UTF-8");
+ Files.copy(resource.getFile().toPath(), response.getOutputStream());
+ return;
+ }
+ // If the path matches any of the prefixes, continue the filter chain
+ filterChain.doFilter(request, response);
+ }
+}