springboot+Druid

时间:2025-03-29 10:31:06
@Configuration public class DruidConfig { @Bean @ConfigurationProperties(prefix = "") public DataSource getDruidDataSource(){ return new DruidDataSource(); } /** * 后台监控功能 */ @Bean public ServletRegistrationBean statViewServlet(){ ServletRegistrationBean<StatViewServlet> bean = new ServletRegistrationBean<>(new StatViewServlet(), "/druid/*"); //后台登录的账号设置 Map<String,String> initParameters = new HashMap<>(); initParameters.put("loginUsername","admin"); initParameters.put("loginPassword","123456"); //允许谁可以访问 如果value为空字符串,则表示都可以访问;localhost表示本机才能访问 initParameters.put("allow","localhost"); //禁止谁访问 initParameters.put("yyb","192.168.1.77"); bean.setInitParameters(initParameters); return bean; } /** * 过滤器 * @return */ @Bean public FilterRegistrationBean webStatFilter(){ FilterRegistrationBean bean = new FilterRegistrationBean(); bean.setFilter(new WebStatFilter()); //可以过滤哪些请求 Map<String,String> initParameters = new HashMap<>(); //这些东西不进行统计 initParameters.put("exclusions","*.js,*.css,/druid/*"); bean.setInitParameters(initParameters); return bean; } }