|
| 1 | +package top.cadecode.uniboot.framework.config; |
| 2 | + |
| 3 | +import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure; |
| 4 | +import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; |
| 5 | +import com.alibaba.druid.util.Utils; |
| 6 | +import org.springframework.boot.autoconfigure.AutoConfigureAfter; |
| 7 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; |
| 8 | +import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; |
| 9 | +import org.springframework.boot.web.servlet.FilterRegistrationBean; |
| 10 | +import org.springframework.context.annotation.Bean; |
| 11 | +import org.springframework.context.annotation.Configuration; |
| 12 | + |
| 13 | +import javax.servlet.*; |
| 14 | +import java.io.IOException; |
| 15 | + |
| 16 | +/** |
| 17 | + * druid 连接池配置 |
| 18 | + * |
| 19 | + * @author Cade Li |
| 20 | + * @date 2023/2/22 |
| 21 | + */ |
| 22 | +@Configuration |
| 23 | +@ConditionalOnWebApplication |
| 24 | +@AutoConfigureAfter(DruidDataSourceAutoConfigure.class) |
| 25 | +@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true) |
| 26 | +public class DruidConfig { |
| 27 | + |
| 28 | + /** |
| 29 | + * 去除 druid 监控页面底部广告的过滤器 |
| 30 | + */ |
| 31 | + @Bean |
| 32 | + public FilterRegistrationBean<Filter> removeDruidAdFilterRegistrationBean(DruidStatProperties properties) { |
| 33 | + // 获取配置的 druid 监控页面的路径 |
| 34 | + DruidStatProperties.StatViewServlet config = properties.getStatViewServlet(); |
| 35 | + String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*"; |
| 36 | + String commonJsPattern = pattern.replaceAll("\\*", "js/common.js"); |
| 37 | + String filePath = "support/http/resources/js/common.js"; |
| 38 | + Filter filter = new Filter() { |
| 39 | + @Override |
| 40 | + public void init(FilterConfig filterConfig) { |
| 41 | + } |
| 42 | + |
| 43 | + @Override |
| 44 | + public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { |
| 45 | + chain.doFilter(request, response); |
| 46 | + // 重置缓冲区,响应头不会被重置 |
| 47 | + response.resetBuffer(); |
| 48 | + // 获取 common.js |
| 49 | + String text = Utils.readFromResource(filePath); |
| 50 | + // 替换 js buildFooter 方法,让其不执行 |
| 51 | + text = text.replace("this.buildFooter();", ""); |
| 52 | + response.getWriter().write(text); |
| 53 | + } |
| 54 | + |
| 55 | + @Override |
| 56 | + public void destroy() { |
| 57 | + } |
| 58 | + }; |
| 59 | + FilterRegistrationBean<Filter> registrationBean = new FilterRegistrationBean<>(); |
| 60 | + registrationBean.setFilter(filter); |
| 61 | + registrationBean.addUrlPatterns(commonJsPattern); |
| 62 | + return registrationBean; |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | + |
0 commit comments