feat: ASW-31 asw controller 增加 GUIHistoryRouterFilter

This commit is contained in:
zhangshuai
2024-08-08 13:40:50 +08:00
parent f7caf6262c
commit a32327ad22

View File

@@ -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);
}
}