当前位置:   article > 正文

Druid监控页面配置用户密码、去除Ad_druid默认账号密码

druid默认账号密码

1.druid依赖

  1. <!-- https://mvnrepository.com/artifact/com.alibaba/druid-spring-boot-starter -->
  2. <dependency>
  3. <groupId>com.alibaba</groupId>
  4. <artifactId>druid-spring-boot-starter</artifactId>
  5. <version>1.2.16</version>
  6. </dependency>

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

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

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

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

  1. @Bean
  2. public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {
  3. // 获取web监控页面的参数
  4. DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
  5. // 提取common.js的配置路径
  6. String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
  7. String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
  8. final String filePath = "support/http/resources/js/common.js";
  9. // 创建filter进行过滤
  10. Filter filter = new Filter() {
  11. @Override
  12. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  13. filterChain.doFilter(servletRequest, servletResponse);
  14. // 重置缓冲区,响应头不会被重置
  15. servletResponse.resetBuffer();
  16. // 获取common.js
  17. // import com.alibaba.druid.util.Utils;
  18. String text = Utils.readFromResource(filePath);
  19. // 正则替换banner, 除去底部的广告信息
  20. text = text.replaceAll("<a.*?banner\"></a><br/>", "");
  21. text = text.replaceAll("powered.*?shrek.wang</a>", "");
  22. servletResponse.getWriter().write(text);
  23. }
  24. };
  25. FilterRegistrationBean registrationBean = new FilterRegistrationBean();
  26. registrationBean.setFilter(filter);
  27. registrationBean.addUrlPatterns(commonJsPattern);
  28. return registrationBean;
  29. }

 

 嵌入的广告:

  1. buildFooter : function() {
  2. var html ='<footer class="footer">'+
  3. ' <div class="container">'+
  4. '<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/>' +
  5. ' 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>'+
  6. ' </div>'+
  7. ' </footer>';
  8. $(document.body).append(html);
  9. },

4. 源码

  1. import com.alibaba.druid.filter.logging.Slf4jLogFilter;
  2. import com.alibaba.druid.filter.stat.StatFilter;
  3. import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties;
  4. import com.alibaba.druid.support.http.StatViewServlet;
  5. import com.alibaba.druid.support.http.WebStatFilter;
  6. import com.alibaba.druid.util.Utils;
  7. import org.springframework.boot.web.servlet.FilterRegistrationBean;
  8. import org.springframework.boot.web.servlet.ServletRegistrationBean;
  9. import org.springframework.context.annotation.Bean;
  10. import org.springframework.context.annotation.Configuration;
  11. import javax.servlet.*;
  12. import java.io.IOException;
  13. import java.util.Arrays;
  14. import java.util.HashMap;
  15. import java.util.Map;
  16. /**
  17. * @author Benjamin
  18. * @version 1.0
  19. * @date 2023/3/6 10:24
  20. */
  21. @Configuration
  22. public class DruidConfig {
  23. /**
  24. * @author Benjamin
  25. * @date 2023/3/6 10:42:51
  26. * @version 1.0.0
  27. * @description 配置Druid监控的Servlet
  28. * @return ServletRegistrationBean
  29. */
  30. @Bean
  31. public ServletRegistrationBean statViewServlet() {
  32. final ServletRegistrationBean<StatViewServlet> servletRegistrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
  33. // 配置druid参数
  34. Map<String, String> initParams = new HashMap<>(16);
  35. // 默认访问用户密码 admin:admin
  36. initParams.put("loginUsername", "admin");
  37. initParams.put("loginPassword", "qwer1234!@#$");
  38. // 允许访问的ip
  39. initParams.put("allow", "127.0.0.1");
  40. // 黑名单ip,优先级比allow配置高
  41. initParams.put("deny", "");
  42. servletRegistrationBean.setInitParameters(initParams);
  43. return servletRegistrationBean;
  44. }
  45. /**
  46. * @author Benjamin
  47. * @date 2023/3/6 11:07:47
  48. * @version 1.0.0
  49. * @description 配置druid监控的Filter
  50. * @return FilterRegistrationBean
  51. */
  52. @Bean
  53. public FilterRegistrationBean webStatFilter() {
  54. FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
  55. filterRegistrationBean.setFilter(new WebStatFilter());
  56. Map<String, String> initParams = new HashMap<>(16);
  57. initParams.put("exclusions", "/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid/*");
  58. filterRegistrationBean.setInitParameters(initParams);
  59. filterRegistrationBean.setUrlPatterns(Arrays.asList("/druid/*"));
  60. filterRegistrationBean.setEnabled(true);
  61. return filterRegistrationBean;
  62. }
  63. /**
  64. * @author Benjamin
  65. * @date 2023/3/6 13:58:21
  66. * @version 1.0.0
  67. * @description 配置慢sql拦截器
  68. * @return StatFilter
  69. */
  70. @Bean
  71. public StatFilter statFilter() {
  72. StatFilter statFilter = new StatFilter();
  73. // 慢sql时间设置,即执行时间大于2000毫秒都是慢sql
  74. statFilter.setSlowSqlMillis(2000L);
  75. statFilter.setLogSlowSql(true);
  76. statFilter.setMergeSql(true);
  77. return statFilter;
  78. }
  79. /**
  80. * @author Benjamin
  81. * @date 2023/3/6 16:18:48
  82. * @version 1.0.0
  83. * @description 配置Druid日志
  84. * @return com.alibaba.druid.filter.logging.Slf4jLogFilter
  85. */
  86. @Bean
  87. public Slf4jLogFilter logFilter() {
  88. Slf4jLogFilter slf4jLogFilter = new Slf4jLogFilter();
  89. slf4jLogFilter.setDataSourceLogEnabled(true);
  90. slf4jLogFilter.setStatementExecutableSqlLogEnable(true);
  91. return slf4jLogFilter;
  92. }
  93. /**
  94. * @author Benjamin
  95. * @date 2023/3/8 10:27:24
  96. * @version 1.0.0
  97. * @description 过滤掉Druid监控页面Ad(广告)
  98. * @param properties
  99. * @return org.springframework.boot.web.servlet.FilterRegistrationBean
  100. */
  101. @Bean
  102. public FilterRegistrationBean removeDruidAdFilterRegistrationBean(DruidStatProperties properties) {
  103. // 获取web监控页面的参数
  104. DruidStatProperties.StatViewServlet config = properties.getStatViewServlet();
  105. // 提取common.js的配置路径
  106. String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*";
  107. String commonJsPattern = pattern.replaceAll("\\*", "js/common.js");
  108. final String filePath = "support/http/resources/js/common.js";
  109. // 创建filter进行过滤
  110. Filter filter = new Filter() {
  111. @Override
  112. public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
  113. filterChain.doFilter(servletRequest, servletResponse);
  114. // 重置缓冲区,响应头不会被重置
  115. servletResponse.resetBuffer();
  116. // 获取common.js
  117. String text = Utils.readFromResource(filePath);
  118. // 正则替换banner, 除去底部的广告信息
  119. text = text.replaceAll("<a.*?banner\"></a><br/>", "");
  120. text = text.replaceAll("powered.*?shrek.wang</a>", "");
  121. servletResponse.getWriter().write(text);
  122. }
  123. };
  124. FilterRegistrationBean registrationBean = new FilterRegistrationBean();
  125. registrationBean.setFilter(filter);
  126. registrationBean.addUrlPatterns(commonJsPattern);
  127. return registrationBean;
  128. }
  129. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/498967
推荐阅读
相关标签
  

闽ICP备14008679号