1

目前druid-1.1.14的web控制台依然存在阿里云的广告,本文通过过滤器将广告文本拦截。

定位问题

产生广告的JS文件在 druid-1.1.14.jar/support/http/resources/js/common.js

查看源码可知是buildFooter方法进行植入,由init方法调用。

解决问题

/**
 * Druid的配置类
 *
 * @author BBF
 */
@Configuration
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
public class DruidConfig {

  /**
   * 带有广告的common.js全路径,druid-1.1.14
   */
  private static final String FILE_PATH = "support/http/resources/js/common.js";
  /**
   * 原始脚本,触发构建广告的语句
   */
  private static final String ORIGIN_JS = "this.buildFooter();";
  /**
   * 替换后的脚本
   */
  private static final String NEW_JS = "//this.buildFooter();";

  /**
   * 去除Druid监控页面的广告
   *
   * @param properties DruidStatProperties属性集合
   * @return {@link org.springframework.boot.web.servlet.FilterRegistrationBean}
   */
  @Bean
  @ConditionalOnWebApplication
  @ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true")
  public FilterRegistrationBean<RemoveAdFilter> removeDruidAdFilter(
      DruidStatProperties properties) throws IOException {
    // 获取web监控页面的参数
    DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
    // 提取common.js的配置路径
    String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
    String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
    // 获取common.js
    String text = Utils.readFromResource(FILE_PATH);
    // 屏蔽 this.buildFooter(); 不构建广告
    final String newJs = text.replace(ORIGIN_JS, NEW_JS);
    FilterRegistrationBean<RemoveAdFilter> registration = new FilterRegistrationBean<>();
    registration.setFilter(new RemoveAdFilter(newJs));
    registration.addUrlPatterns(commonJsPattern);
    return registration;
  }

  /**
   * 删除druid的广告过滤器
   *
   * @author BBF
   */
  private class RemoveAdFilter implements Filter {

    private final String newJs;

    public RemoveAdFilter(String newJS) {
      this.newJs = newJS;
    }

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
        throws IOException, ServletException {
      chain.doFilter(request, response);
      // 重置缓冲区,响应头不会被重置
      response.resetBuffer();
      response.getWriter().write(newJs);
    }
  }
}

bluesbruce
215 声望17 粉丝