前言
访问项目中druid监控的页面,发现存在广告和超链接,需要我们来去除或者替换成自己企业定制的banner等。一、配置druid监控
1、引入druid的依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.16</version>
</dependency>
2、注册StatViewServlet
@Bean
public ServletRegistrationBean druidStatViewServlet(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new StatViewServlet(),"/druid2/*");
return servletRegistrationBean;
}
二、druid监控的广告
进入druid监控页面,
三、广告的来源
监控页面是通过StatViewServlet来获取的,当浏览器访问/druid2/*的url时,通通过父类ResourceServlet的service方法,最终读取support/http/resources目录下的html文件,返回给浏览器来展示监控页面。
而广告的来源,是在support/http/resources/js目录中的common.js文件中:
buildFooter : function() {
var html ='<footer class="footer">'+
' <div class="container">'+
'<a href="https://render.alipay.com/p/s/taobaonpm_click/druid_banner_click" target="new"><img src="https://render.alipay.com/p/s/taobaonpm_click/druid_banner"></a><br/>' +
' powered by <a href="https://github.com/alibaba/" target="_blank">Alibaba</a> & sandzhang & <a href="http://melin.iteye.com/" target="_blank">melin</a> & <a href="https://github.com/shrekwang" target="_blank">shrek.wang</a>'+
' </div>'+
' </footer>';
$(document.body).append(html);
},
顶部Druid Monitor超链接是在support/http/resources下的header.html文件中:
<a href="https://github.com/alibaba/druid/wiki" target="_blank" class="brand lang" langKey="">Druid Monitor</a>
四、广告超链接的去除
1、思路
在代码的分析中可以发现ResourceServlet的service方法,最终调用returnResourceFile(path, uri, response)方法来返回监控页面,
protected void returnResourceFile(String fileName, String uri, HttpServletResponse response)
throws ServletException,
IOException {
String filePath = getFilePath(fileName);
if (filePath.endsWith(".html")) {
response.setContentType("text/html; charset=utf-8");
}
if (fileName.endsWith(".jpg")) {
byte[] bytes = Utils.readByteArrayFromResource(filePath);
if (bytes != null) {
response.getOutputStream().write(bytes);
}
return;
}
String text = Utils.readFromResource(filePath);
if (text == null) {
response.sendRedirect(uri + "/index.html");
return;
}
if (fileName.endsWith(".css")) {
response.setContentType("text/css;charset=utf-8");
} else if (fileName.endsWith(".js")) {
response.setContentType("text/javascript;charset=utf-8");
}
response.getWriter().write(text);
}
最终找出资源文件,通过 response.getWriter().write(text)返回给浏览器,那我们可以通过该方法下手,将我们自己定制的页面返回给浏览器。
2、实现
(1) 方式一,修改common.js文件
1、注释或删除this.buildFooter();
return {
init : function() {
//this.buildFooter();
druid.lang.init();
},
2、去除超链接
buildHead : function(index) {
$.get('header.html',function(html) {
$(document.body).prepend(html);
druid.lang.trigger();
$(".navbar .nav li").eq(index).addClass("active");
let aLabel = $(".container").find("a").first();
aLabel.attr("href", "javascript:void(0);");
aLabel.text("数据监控");
aLabel.removeAttr("target");
},"html");
},
3、后台代码
public class CustomStatViewServlet extends StatViewServlet {
@Override
protected void returnResourceFile(String fileName, String uri, HttpServletResponse response) throws ServletException, IOException {
String filePath = getFilePath(fileName);
//********定制一些信息*********
//方式1:返回修改后的common.js文件
String content = "";
if (fileName.endsWith("common.js")){
try {
ClassPathResource classPathResource = new ClassPathResource("static/js/common.js");
File file = classPathResource.getFile();
content = FileUtil.readUtf8String(file);
response.getWriter().write(content);
return;
}catch (Exception e){
}
}
super.returnResourceFile(fileName,uri,response);
}
}
4、注册CustomStatViewServlet
@Bean
public ServletRegistrationBean druidStatViewServlet(){
ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean(new CustomStatViewServlet(),"/druid2/*");
return servletRegistrationBean;
}
(2)方式二,动态修改druid的common.js
@Override
protected void returnResourceFile(String fileName, String uri, HttpServletResponse response) throws ServletException, IOException {
String filePath = getFilePath(fileName);
if (filePath.endsWith(".html")) {
response.setContentType("text/html; charset=utf-8");
}
if (fileName.endsWith(".jpg")) {
byte[] bytes = Utils.readByteArrayFromResource(filePath);
if (bytes != null) {
response.getOutputStream().write(bytes);
}
return;
}
String text = Utils.readFromResource(filePath);
if (text == null) {
response.sendRedirect(uri + "/index.html");
return;
}
if (fileName.endsWith(".css")) {
response.setContentType("text/css;charset=utf-8");
} else if (fileName.endsWith(".js")) {
response.setContentType("text/javascript;charset=utf-8");
}
//********定制一些信息*********
if (fileName.endsWith("common.js")) {
//方式2:去除footer信息,动态删除this.buildFooter()
text = text.replaceAll(".*this.buildFooter().*", "");
//去除顶部超链接,则动态的往 buildHead : function(index) { 函数中加上:
// let aLabel = $(".container").find("a").first();
// aLabel.attr("href", "javascript:void(0);");
// aLabel.text("数据监控");
// aLabel.removeAttr("target");
}
response.getWriter().write(text);
}
最终,界面如下
总结
本文介绍了定制druid监控的方式,当然可以用同样的方式来根据自己的需求定制其他页面。本文仅供参考、学习和交流使用。