durid监控权限

前言

项目上使用了druid数据源,可以监控sql,慢sql等,一般只在测试环境下使用,生成环境是需要关掉或者加权限控制的,最近线上项目漏洞扫描发现了这个问题,druid的监控页面默认是没有权限的,可以随意进入,下面说下如何修复这个问题

解决

两种方式,一是加权限就是登录名和密码,二是直接关掉sql监控功能,如非必要,建议线上环境直接关掉sql监控

1、加权限,设置druid访问页面的用户名和密码,只需要在springboot启动类中加入@bean配置即可

1
2
3
4
5
6
7
8
9
10
@Bean
public ServletRegistrationBean<StatViewServlet> druidStatViewServlet() {
ServletRegistrationBean<StatViewServlet> registrationBean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*");
registrationBean.addInitParameter("allow", "127.0.0.1");// IP白名单 (没有配置或者为空,则允许所有访问)
registrationBean.addInitParameter("deny", "");// IP黑名单 (存在共同时,deny优先于allow)
registrationBean.addInitParameter("loginUsername", "root");
registrationBean.addInitParameter("loginPassword", "wejori@23dskSS");
registrationBean.addInitParameter("resetEnable", "false");
return registrationBean;
}

2、直接关闭,再配置文件中配置一下

1
2
3
spring.datasource.druid.stat-view-servlet.enabled=false
spring.datasource.druid.web-stat-filter.enabled=false
spring.datasource.druid.filter.config.enabled=false