Springboot项目配置druid监控页面

Springboot项目配置druid监控页面_第1张图片

 引入maven依赖


        
            com.alibaba
            druid-spring-boot-starter
            1.2.16
        
        
            com.alibaba
            druid
            1.2.16
        

config文件添加账号密码配置

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

        //后台需要有人登录,账号密码配置
        HashMap 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("
", ""); //text = text.replaceAll("powered.*?shrek.wang", ""); response.getWriter().write(text); } @Override public void destroy() { } }; FilterRegistrationBean registrationBean = new FilterRegistrationBean(); registrationBean.setFilter(filter); registrationBean.addUrlPatterns(commonJsPattern); return registrationBean; }

前端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();
               

            }
        }
    )*/
}

你可能感兴趣的:(spring,boot,java,后端,javascript,html5)