Springboot项目配置druid监控页面

文章介绍了如何在项目中引入阿里巴巴的Druid数据源,包括添加Maven依赖,配置后台监控的账号密码,以及通过编写Filter去除监控页面的广告。此外,还展示了前端HTML和JavaScript代码用于展示数据监控页面。
摘要由CSDN通过智能技术生成

 引入maven依赖

<!-- 引用阿里巴巴的druid数据源 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid-spring-boot-starter</artifactId>
            <version>1.2.16</version>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>druid</artifactId>
            <version>1.2.16</version>
        </dependency>

config文件添加账号密码配置

 //后台监控
    @Bean
    public ServletRegistrationBean statViewServlet(){
        ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");

        //后台需要有人登录,账号密码配置
        HashMap<String, String> initParameters = new HashMap<>();
        //增加配置
        initParameters.put("loginUsername","admin");
        initParameters.put("loginPassword","123456");

        //允许谁可以访问
        initParameters.put("allow","");

        //禁止谁访问     initParameters.put("xiaowu","192.168.1.1");


        bean.setInitParameters(initParameters);//设置初始化参数
        return bean;
    }

去除监控页面广告(阿里广告)

 /**
     * 去除监控页面底部的广告
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    @Bean
    @ConditionalOnProperty(name = "spring.datasource.druid.statViewServlet.enabled", havingValue = "true")
    public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties)
    {
        // 获取web监控页面的参数
        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
        // 提取common.js的配置路径
        String commonJsPattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/js/common.js";
        //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(javax.servlet.FilterConfig filterConfig) throws ServletException
            {
            }
            @Override
            public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
                    throws IOException, ServletException
            {
                chain.doFilter(request, response);
                // 重置缓冲区,响应头不会被重置
                response.resetBuffer();
                // 获取common.js
                String text = Utils.readFromResource(filePath);
                // 正则替换banner, 除去底部的广告信息
                text = text.replace("this.buildFooter();", "");
                //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;
    }

前端HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>数据监控</title>
    <link rel="stylesheet" href="../plugin/layui-v2.5.5/css/layui.css">
    <link rel="stylesheet" href="../plugin/font-awesome/css/font-awesome.css">

</head>
<style>
    /*防止文档渲染时有明显颜色差异*/

    .layui-container{width: 100%;height: 100% ;padding: 15px;}

</style>
<body>
<div class="layui-container" style="height: 100%;width: 100%;overflow: hidden">
    <div class="layui-row"> <iframe style="height: 100%;width: 100%;" id="iframeId" frameborder="0" class="ifr"></iframe></div>

</div>


<script src="../plugin/jquery/jquery-2.1.1.min.js"></script>
<script src="../plugin/jquery/jquery-ui.js"></script>
<script src="../plugin/jquery/jquery.cookie.js"></script>
<script src="../plugin/layui-v2.5.5/layui.all.js"></script>
<script src="../js/statics.js"></script>
<script src="js/statics.js"></script>
<script src="js/index.js"></script>
<script src="js/sjjk.js"></script>

</body>
</html>

前端js


$(document).ready(function () {
    showDataDb();
});

function showDataDb(){
    let iframe = document.getElementById("iframeId");
    iframe.src = SERVICE_URL+"druid/login.html";
    let body = document.body;
    //body.style.backgroundColor = "#ffffff";//设置背景色
    /*$.ajax({
            url:SERVICE_URL+"monitor/data",
        //url:SERVICE_URL+"monitor/getInfo",
            type:"get",
            async:true,
            //contentType:"multipart/form-data",
            success:function (result){
                window.location.href = 'about:blank';
                document.write(result);
              
                document.close();
               

            }
        }
    )*/
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值