matrix.boot.jdbc.config.DruidMonitorAutoConfiguration Maven / Gradle / Ivy
The newest version!
package matrix.boot.jdbc.config;
import com.alibaba.druid.support.http.StatViewServlet;
import com.alibaba.druid.support.http.WebStatFilter;
import matrix.boot.jdbc.properties.JdbcProperties;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import javax.servlet.Filter;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
/**
* 德鲁伊监控配置
* @author wangcheng
* date 2021/8/15
*/
@EnableConfigurationProperties(JdbcProperties.class)
@ConditionalOnProperty(value = {"matrix.jdbc.enabled", "matrix.jdbc.druid-monitor.enabled"})
public class DruidMonitorAutoConfiguration {
/**
* 数据库配置
*/
private final JdbcProperties jdbcProperties;
public DruidMonitorAutoConfiguration(JdbcProperties jdbcProperties) {
this.jdbcProperties = jdbcProperties;
}
/**
* 德鲁伊监控视图
* @return Servlet
*/
@Bean
public ServletRegistrationBean registerDruidServlet() {
JdbcProperties.DruidMonitor druidMonitor = jdbcProperties.getDruidMonitor();
ServletRegistrationBean registration = new ServletRegistrationBean<>();
registration.setServlet(new StatViewServlet());
registration.setName("DruidStatView");
registration.setEnabled(true);
Map params = new HashMap<>();
params.put("loginUsername", druidMonitor.getUsername());
params.put("loginPassword", druidMonitor.getPassword());
registration.setInitParameters(params);
registration.setUrlMappings(Collections.singletonList(druidMonitor.getContextPath()));
return registration;
}
/**
* 注册德鲁伊过滤器,放过一些资源
* @return 过滤器注册bean
*/
@Bean
public FilterRegistrationBean registerDruidFilter() {
JdbcProperties.DruidMonitor druidMonitor = jdbcProperties.getDruidMonitor();
FilterRegistrationBean registration = new FilterRegistrationBean<>();
registration.setFilter(new WebStatFilter());
registration.addUrlPatterns("/*");
registration.setName("druidFilter");
Map params = new HashMap<>();
params.put("exclusions", "*.js,*.gif,*.jpg,*.bmp,*.png,*.css,*.ico," + druidMonitor.getContextPath());
registration.setInitParameters(params);
registration.setOrder(1);
return registration;
}
}