SpringBoot项目去除druid监控的底部广告
Posted XiaoMaPro
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot项目去除druid监控的底部广告相关的知识,希望对你有一定的参考价值。
说明
底部有广告,是因为其引入的druid包的中的upport\\http\\resources\\jscommon.js文件中包含了广告内容。
添加Druid的Starter依赖
- 对于SpringBoot项目.阿里druid有其专门的druid-spring-boot-starter版本,在这里我使用的是1.1.10版本,根据自己已经阿里更新的版本自行调整。
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.13</version>
</dependency>
去除广告
- 在SpringBoot项目中编写一个RemoveDruidAdvertisingConfig配置类,去除druid监控页面广告
import java.io.IOException;
import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.util.Utils;
/**
* @Title: RemoveDruidAdvertisingConfig.java
* @Package com.gx.obe.server.config
* @Description: 去掉druid底部广告
* @author: XiaoMaPro
* @date: 2018年08月15日 下午2:12:46
* @version V1.0
* @Copyright: 2017 - 2018 www.myspro.com Inc. All rights reserved.
*
*/
@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true)
public class RemoveDruidAdvertisingConfig
/**
* @Title: removeDruidAdFilterRegistrationBean
* @Description: 除去页面底部的广告
* @param: @param properties
* @param: @return
* @return: FilterRegistrationBean
* @throws
*/
@Bean
public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties)
/**
* 获取监控页面参数
*/
DruidStatProperties.StatViewServlet druidConfig = properties.getStatViewServlet();
/**
* 获取common.js位置
*/
String pattern = druidConfig.getUrlPattern() != null ? druidConfig.getUrlPattern() : "/druid/*";
String commonJsPattern = pattern.replaceAll("\\\\*", "js/common.js");
final String filePath = "support/http/resources/js/common.js";
/**
* 利用Filter进行过滤
*/
Filter filter = new Filter()
@Override
public void init(FilterConfig filterConfig) throws ServletException
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException
chain.doFilter(request, response);
response.resetBuffer();
/**
* 获取common文件内容
*/
String text = Utils.readFromResource(filePath);
/**
* 利用正则表达式删除<footer class="footer">中的<a>标签
*/
text = text.replaceAll("<a.*?banner\\"></a><br/>", "");
text = text.replaceAll("powered.*?shrek.wang</a>", "");
response.getWriter().write(text);
@Override
public void destroy()
;
FilterRegistrationBean registrationBean = new FilterRegistrationBean();
registrationBean.setFilter(filter);
registrationBean.addUrlPatterns(commonJsPattern);
return registrationBean;
测试
以上是关于SpringBoot项目去除druid监控的底部广告的主要内容,如果未能解决你的问题,请参考以下文章