时间:2021-07-01 10:21:17 帮助过:24人阅读
3.最后,还需要增加一个druid的基本配置,这里直接上代码。
import javax.sql.DataSource; import org.springframework.boot.context.properties.ConfigurationProperties; 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 com.alibaba.druid.pool.DruidDataSource; import com.alibaba.druid.support.http.StatViewServlet; import com.alibaba.druid.support.http.WebStatFilter; @Configuration public class DruidConfig { @Bean public ServletRegistrationBean druidServlet() {// 主要实现web监控的配置处理 ServletRegistrationBean servletRegistrationBean = new ServletRegistrationBean( new StatViewServlet(), "/druid/*");//表示进行druid监控的配置处理操作 servletRegistrationBean.addInitParameter("allow", "127.0.0.1,129.168.1.11");//白名单 servletRegistrationBean.addInitParameter("deny", "129.168.1.12");//黑名单 servletRegistrationBean.addInitParameter("loginUsername", "root");//用户名 servletRegistrationBean.addInitParameter("loginPassword", "root");//密码 servletRegistrationBean.addInitParameter("resetEnable", "false");//是否可以重置数据源 return servletRegistrationBean; } @Bean //监控 public FilterRegistrationBean filterRegistrationBean(){ FilterRegistrationBean filterRegistrationBean=new FilterRegistrationBean(); filterRegistrationBean.setFilter(new WebStatFilter()); filterRegistrationBean.addUrlPatterns("/*");//所有请求进行监控处理 filterRegistrationBean.addInitParameter("exclusions", "*.js,*.gif,*.jpg,*.css,/druid/*");//排除 return filterRegistrationBean; } @Bean @ConfigurationProperties(prefix = "spring.datasource") public DataSource druidDataSource() { return new DruidDataSource(); } }
我们可以看到这里的几个基本配置,白名单,黑名单,顾名思义就是设置谁可以访问,谁不能访问。用户名, 密码就不用多说了。最后我们登录druid监控,看一下,访问地址:http://localhost:port/druid/login.html,会看到如下界面
使用上面配置好的用户名密码进行登录,便实现了druid监控
SpringBoot配置MySql数据库和Druid连接池
标签:use servlet 就是 ref com 容器 spring容器 view 帮助