0


Druid监控页面配置用户密码、去除Ad

1.druid依赖

<!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>druid-spring-boot-starter</artifactId>
    <version>1.2.16</version>
</dependency>

监控页面访问路径:http://localhost:21018/context-path/druid/index.html

2.Druid监控页面设置用户密码

@Bean
    public ServletRegistrationBean statViewServlet() {
        final ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        // 配置druid参数
        Map<String, String> initParams = new HashMap<>(16);
        // 默认访问用户密码 admin:admin
        initParams.put("loginUsername", "admin");
        initParams.put("loginPassword", "qwer1234!@#$");
        // 允许访问的ip
        initParams.put("allow", "127.0.0.1");
        // 黑名单ip,优先级比allow配置高
        initParams.put("deny", "");
        servletRegistrationBean.setInitParameters(initParams);
        return servletRegistrationBean;
    }

3.Druid去除底部Ad(广告)

@Bean
    public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {
        // 获取web监控页面的参数
        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
        // 提取common.js的配置路径
        String pattern = config.getUrlPattern() != null ? config.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 doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                filterChain.doFilter(servletRequest, servletResponse);
                // 重置缓冲区,响应头不会被重置
                servletResponse.resetBuffer();
                // 获取common.js 
                // import com.alibaba.druid.util.Utils;
                String text = Utils.readFromResource(filePath);

                // 正则替换banner, 除去底部的广告信息
                text = text.replaceAll("<a.*?banner\"></a><br/>", "");
                text = text.replaceAll("powered.*?shrek.wang</a>", "");
                servletResponse.getWriter().write(text);
            }

        };

        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(filter);
        registrationBean.addUrlPatterns(commonJsPattern);

        return registrationBean;
    }

嵌入的广告:

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);
        },

4. 源码

import com.alibaba.druid.filter.logging.Slf4jLogFilter;
import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import com.alibaba.druid.util.Utils;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.servlet.*;
import java.io.IOException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Benjamin
 * @version 1.0
 * @date 2023/3/6 10:24
 */
@Configuration
public class DruidConfig {

    /**
    * @author Benjamin
    * @date 2023/3/6 10:42:51
    * @version 1.0.0
    * @description 配置Druid监控的Servlet
    * @return ServletRegistrationBean
    */
    @Bean
    public ServletRegistrationBean statViewServlet() {
        final ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
        // 配置druid参数
        Map<String, String> initParams = new HashMap<>(16);
        // 默认访问用户密码 admin:admin
        initParams.put("loginUsername", "admin");
        initParams.put("loginPassword", "qwer1234!@#$");
        // 允许访问的ip
        initParams.put("allow", "127.0.0.1");
        // 黑名单ip,优先级比allow配置高
        initParams.put("deny", "");
        servletRegistrationBean.setInitParameters(initParams);
        return servletRegistrationBean;
    }

    /**
    * @author Benjamin
    * @date 2023/3/6 11:07:47
    * @version 1.0.0    
    * @description 配置druid监控的Filter
    * @return FilterRegistrationBean
    */
    @Bean
    public FilterRegistrationBean webStatFilter() {
        FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
        filterRegistrationBean.setFilter(new WebStatFilter());
        Map<String, String> initParams = new HashMap<>(16);
        initParams.put("exclusions", "/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");

        filterRegistrationBean.setInitParameters(initParams);

        filterRegistrationBean.setUrlPatterns(Arrays.asList("/druid/*"));
        filterRegistrationBean.setEnabled(true);
        return filterRegistrationBean;
    }

    /**
    * @author Benjamin
    * @date 2023/3/6 13:58:21
    * @version 1.0.0
    * @description 配置慢sql拦截器
    * @return StatFilter
    */
    @Bean
    public StatFilter statFilter() {
        StatFilter statFilter = new StatFilter();
        // 慢sql时间设置,即执行时间大于2000毫秒都是慢sql
        statFilter.setSlowSqlMillis(2000L);
        statFilter.setLogSlowSql(true);
        statFilter.setMergeSql(true);
        return statFilter;
    }

    /**
    * @author Benjamin
    * @date 2023/3/6 16:18:48
    * @version 1.0.0
    * @description 配置Druid日志
    * @return com.alibaba.druid.filter.logging.Slf4jLogFilter
    */
    @Bean
    public Slf4jLogFilter logFilter() {
        Slf4jLogFilter slf4jLogFilter = new Slf4jLogFilter();
        slf4jLogFilter.setDataSourceLogEnabled(true);
        slf4jLogFilter.setStatementExecutableSqlLogEnable(true);
        return slf4jLogFilter;
    }

    /**
    * @author Benjamin
    * @date 2023/3/8 10:27:24
    * @version 1.0.0
    * @description 过滤掉Druid监控页面Ad(广告)
    * @param properties
    * @return org.springframework.boot.web.servlet.FilterRegistrationBean
    */
    @Bean
    public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {
        // 获取web监控页面的参数
        DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
        // 提取common.js的配置路径
        String pattern = config.getUrlPattern() != null ? config.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 doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
                filterChain.doFilter(servletRequest, servletResponse);
                // 重置缓冲区,响应头不会被重置
                servletResponse.resetBuffer();
                // 获取common.js
                String text = Utils.readFromResource(filePath);

                // 正则替换banner, 除去底部的广告信息
                text = text.replaceAll("<a.*?banner\"></a><br/>", "");
                text = text.replaceAll("powered.*?shrek.wang</a>", "");
                servletResponse.getWriter().write(text);
            }

        };

        FilterRegistrationBean registrationBean = new FilterRegistrationBean();
        registrationBean.setFilter(filter);
        registrationBean.addUrlPatterns(commonJsPattern);

        return registrationBean;
    }

}
标签: spring boot java spring

本文转载自: https://blog.csdn.net/m0_48983233/article/details/129402203
版权归原作者 Benjamin℡ 所有, 如有侵权,请联系我们删除。

“Druid监控页面配置用户密码、去除Ad”的评论:

还没有评论