From 012c3ec9bb3ad6f83c167c9427f6821636e81923 Mon Sep 17 00:00:00 2001 From: fengshuonan Date: Tue, 4 Jun 2024 18:33:15 +0800 Subject: [PATCH] =?UTF-8?q?=E3=80=908.1.7=E3=80=91=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E4=B8=80=E4=B8=AAdevops=E9=83=A8=E7=BD=B2=E7=9A=84=E6=8E=A5?= =?UTF-8?q?=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 +- .../guns/config/web/EmptyResourceFilter.java | 114 ++++++++++++++++++ .../config/web/SpringMvcConfiguration.java | 15 +++ .../guns/modular/deploy/IndexController.java | 49 ++++++++ 4 files changed, 180 insertions(+), 2 deletions(-) create mode 100644 src/main/java/cn/stylefeng/guns/config/web/EmptyResourceFilter.java create mode 100644 src/main/java/cn/stylefeng/guns/modular/deploy/IndexController.java diff --git a/pom.xml b/pom.xml index 4c722130..b47bd118 100644 --- a/pom.xml +++ b/pom.xml @@ -12,7 +12,7 @@ cn.stylefeng guns - 8.1.2 + 8.1.7 guns Guns主项目 @@ -24,7 +24,7 @@ UTF-8 1.8 8.0.21 - 8.1.2 + 8.1.7 7.1.1 diff --git a/src/main/java/cn/stylefeng/guns/config/web/EmptyResourceFilter.java b/src/main/java/cn/stylefeng/guns/config/web/EmptyResourceFilter.java new file mode 100644 index 00000000..9a822b88 --- /dev/null +++ b/src/main/java/cn/stylefeng/guns/config/web/EmptyResourceFilter.java @@ -0,0 +1,114 @@ +package cn.stylefeng.guns.config.web; + +import cn.hutool.core.io.resource.ResourceUtil; +import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; + +import javax.servlet.*; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +/** + * 资源文件为空的过滤,针对css样式无法加载的问题 + * + * @author fengshuonan + * @since 2024-04-23 16:12 + */ +public class EmptyResourceFilter implements Filter { + + /** + * 需要做缓存的资源,key是请求资源路径,value是否有空的资源文件,空的资源文件会直接返回空 + */ + private static final Map NEED_TO_CACHE_FLAG = new HashMap<>(); + + /** + * 资源样式前缀 + */ + public static final String GUNS_DEVOPS_ASSETS_PREFIX = "/guns-devops/assets"; + + @Override + public void init(FilterConfig filterConfig) throws ServletException { + + } + + @Override + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { + + // 1. 获取请求类 + HttpServletRequest httpServletRequest = (HttpServletRequest) request; + + // 2. 获取请求的路径,例如:/guns-devops/assets/css/style.css + String requestURI = httpServletRequest.getRequestURI(); + + // 3. 获取资源文件前缀,如果是资源文件前缀,则直接返回空 + if (requestURI.startsWith(GUNS_DEVOPS_ASSETS_PREFIX)) { + + // 先判断是否需要缓存 + Boolean urlHasEmpty = NEED_TO_CACHE_FLAG.get(requestURI); + if (urlHasEmpty != null) { + if (urlHasEmpty) { + this.doEmptyResourceReturn(response); + } else { + chain.doFilter(request, response); + } + return; + } + + // 获取资源文件路径 + String resourcePath = StrUtil.removePrefix(requestURI, "/"); + URL resource = ResourceUtil.getResource(resourcePath); + + // 1. 资源文件不存在,直接返回,直接走404的逻辑 + if (resource == null) { + chain.doFilter(request, response); + + // 缓存一下结果 + NEED_TO_CACHE_FLAG.put(requestURI, false); + + return; + } + + // 2. 资源文件存在,但是内容为空,直接返回空字符串 + String content = ResourceUtil.readUtf8Str(resourcePath); + if (ObjectUtil.isEmpty(content)) { + doEmptyResourceReturn(response); + + // 缓存一下结果 + NEED_TO_CACHE_FLAG.put(requestURI, true); + } + + // 3. 资源文件存在,内容不为空,直接返回内容 + else { + chain.doFilter(request, response); + + // 缓存一下结果 + NEED_TO_CACHE_FLAG.put(requestURI, false); + } + + return; + } + + chain.doFilter(request, response); + } + + /** + * 给http请求响应空字符串 + * + * @author fengshuonan + * @since 2024-04-23 18:23 + */ + private void doEmptyResourceReturn(ServletResponse response) throws IOException { + HttpServletResponse httpServletResponse = (HttpServletResponse) response; + httpServletResponse.setStatus(HttpServletResponse.SC_OK); + httpServletResponse.getWriter().write(""); + } + + @Override + public void destroy() { + // Cleanup code + } +} diff --git a/src/main/java/cn/stylefeng/guns/config/web/SpringMvcConfiguration.java b/src/main/java/cn/stylefeng/guns/config/web/SpringMvcConfiguration.java index 325d1a03..400b8d48 100644 --- a/src/main/java/cn/stylefeng/guns/config/web/SpringMvcConfiguration.java +++ b/src/main/java/cn/stylefeng/guns/config/web/SpringMvcConfiguration.java @@ -5,6 +5,7 @@ import cn.stylefeng.guns.core.security.TokenAndPermissionInterceptor; import cn.stylefeng.roses.kernel.wrapper.field.jackson.CustomJacksonIntrospector; import com.fasterxml.jackson.databind.ser.std.ToStringSerializer; import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer; +import org.springframework.boot.web.servlet.FilterRegistrationBean; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Import; @@ -76,4 +77,18 @@ public class SpringMvcConfiguration implements WebMvcConfigurer { registry.addResourceHandler("/guns-devops/**").addResourceLocations("classpath:/guns-devops/"); } + /** + * 资源样式为空的过滤器 + * + * @author fengshuonan + * @since 2024-04-23 16:12 + */ + @Bean + public FilterRegistrationBean filterRegistrationBean() { + FilterRegistrationBean registrationBean = new FilterRegistrationBean<>(); + registrationBean.setFilter(new EmptyResourceFilter()); + registrationBean.addUrlPatterns(EmptyResourceFilter.GUNS_DEVOPS_ASSETS_PREFIX + "/*"); + return registrationBean; + } + } diff --git a/src/main/java/cn/stylefeng/guns/modular/deploy/IndexController.java b/src/main/java/cn/stylefeng/guns/modular/deploy/IndexController.java new file mode 100644 index 00000000..658b13ea --- /dev/null +++ b/src/main/java/cn/stylefeng/guns/modular/deploy/IndexController.java @@ -0,0 +1,49 @@ +package cn.stylefeng.guns.modular.deploy; + + +import cn.hutool.core.io.IoUtil; +import cn.hutool.core.io.resource.ResourceUtil; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.GetMapping; + +import javax.servlet.ServletOutputStream; +import javax.servlet.http.HttpServletResponse; +import java.io.IOException; +import java.nio.charset.StandardCharsets; + +/** + * 项目部署的控制器,直接jar包启动 + * + * @author fengshuonan + * @since 2024-04-22 18:07 + */ +@Controller +public class IndexController { + + /** + * devops访问的渲染 + * + * @author fengshuonan + * @since 2024/6/4 18:31 + */ + @GetMapping("/guns-devops") + public void index(HttpServletResponse response) throws IOException { + renderIndex(response); + } + + /** + * 渲染主页的操作 + * + * @author fengshuonan + * @date 2022/10/23 15:02 + */ + private void renderIndex(HttpServletResponse response) throws IOException { + ServletOutputStream outputStream = response.getOutputStream(); + response.addHeader("Content-Type", "text/html"); + response.addHeader("Cache-Control", "no-cache"); + response.addHeader("Cache-Control", "no-cache, no-store, private, must-revalidate, proxy-revalidate"); + String indexContent = ResourceUtil.readStr("classpath:/guns-devops/index.html", StandardCharsets.UTF_8); + IoUtil.write(outputStream, StandardCharsets.UTF_8, true, indexContent); + } + +}