diff --git a/pom.xml b/pom.xml index 42d961f2..cd2825f0 100644 --- a/pom.xml +++ b/pom.xml @@ -141,11 +141,6 @@ ${oshi.version} - - com.alibaba - druid-spring-boot-starter - 1.1.10 - @@ -225,4 +220,4 @@ - \ No newline at end of file + diff --git a/src/main/java/cn/stylefeng/guns/config/web/DruidConfiguration.java b/src/main/java/cn/stylefeng/guns/config/web/DruidConfiguration.java deleted file mode 100644 index 0409c48c..00000000 --- a/src/main/java/cn/stylefeng/guns/config/web/DruidConfiguration.java +++ /dev/null @@ -1,157 +0,0 @@ -package cn.stylefeng.guns.config.web; - -import com.alibaba.druid.filter.stat.StatFilter; -import com.alibaba.druid.pool.DruidDataSource; -import com.alibaba.druid.spring.boot.autoconfigure.properties.DruidStatProperties; -import com.alibaba.druid.support.http.StatViewServlet; -import com.alibaba.druid.support.http.WebStatFilter; -import com.alibaba.druid.support.spring.stat.BeanTypeAutoProxyCreator; -import com.alibaba.druid.support.spring.stat.DruidStatInterceptor; -import com.alibaba.druid.util.Utils; -import org.springframework.aop.Advisor; -import org.springframework.aop.support.DefaultPointcutAdvisor; -import org.springframework.aop.support.JdkRegexpMethodPointcut; -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 javax.servlet.Filter; -import javax.servlet.FilterChain; -import javax.servlet.ServletException; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import java.io.IOException; - -/** - * Druid配置 - * - * @author fengshuonan - * @date 2020/4/11 10:23 - */ -@Configuration -public class DruidConfiguration { - - /** - * druidServlet注册 - */ - @Bean - public ServletRegistrationBean druidServletRegistration() { - ServletRegistrationBean registration = new ServletRegistrationBean(new StatViewServlet()); - registration.addUrlMappings("/druid/*"); - return registration; - } - - /** - * druid监控 配置URI拦截策略 - */ - @Bean - public FilterRegistrationBean druidStatFilter() { - FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean(new WebStatFilter()); - //添加过滤规则. - filterRegistrationBean.addUrlPatterns("/*"); - //添加不需要忽略的格式信息. - filterRegistrationBean.addInitParameter( - "exclusions", "/static/*,*.js,*.gif,*.jpg,*.png,*.css,*.ico,/druid,/druid/*"); - //用于session监控页面的用户名显示 需要登录后主动将username注入到session里 - filterRegistrationBean.addInitParameter("principalSessionName", "username"); - filterRegistrationBean.addInitParameter("profileEnable", "true"); - return filterRegistrationBean; - } - - /** - * druid数据库连接池监控 - */ - @Bean - public DruidStatInterceptor druidStatInterceptor() { - return new DruidStatInterceptor(); - } - - /** - * druid慢sql监控 - */ - @Bean - public StatFilter druidSlowSQL(){ - StatFilter statFilter = new StatFilter(); - statFilter.setLogSlowSql(true); - statFilter.setMergeSql(true); - statFilter.setSlowSqlMillis(1000); - return statFilter; - } - - - - @Bean - public JdkRegexpMethodPointcut druidStatPointcut() { - JdkRegexpMethodPointcut druidStatPointcut = new JdkRegexpMethodPointcut(); - String patterns = "cn.stylefeng.guns.modular.*.service.*"; - //可以set多个 - druidStatPointcut.setPatterns(patterns); - return druidStatPointcut; - } - - /** - * druid数据库连接池监控 - */ - @Bean - public BeanTypeAutoProxyCreator beanTypeAutoProxyCreator() { - BeanTypeAutoProxyCreator beanTypeAutoProxyCreator = new BeanTypeAutoProxyCreator(); - beanTypeAutoProxyCreator.setTargetBeanType(DruidDataSource.class); - beanTypeAutoProxyCreator.setInterceptorNames("druidStatInterceptor"); - return beanTypeAutoProxyCreator; - } - - /** - * druid 为druidStatPointcut添加拦截 - * - * @return - */ - @Bean - public Advisor druidStatAdvisor() { - return new DefaultPointcutAdvisor(druidStatPointcut(), druidStatInterceptor()); - } - - /** - * 去除监控页面广告 - * - * @author chenli - * @Date 2021/1/4 20:57 - */ - @SuppressWarnings({"rawtypes", "unchecked"}) - @Bean - public FilterRegistrationBean removeDruidFilterRegistrationBean(DruidStatProperties properties) { - // 获取web监控页面的参数 - DruidStatProperties.StatViewServlet config = properties.getStatViewServlet(); - // 提取common.js的配置路径 - String pattern = config.getUrlPattern() != null ? config.getUrlPattern() : "/druid/*"; - String commonJsPattern = pattern.replaceAll("\\*", "js/common.js"); - final String filePath = "support/http/resources/js/common.js"; - Filter filter = new Filter() { - @Override - public void init(javax.servlet.FilterConfig filterConfig) throws ServletException { - } - - @Override - public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) - throws IOException, ServletException { - chain.doFilter(request, response); - String text = Utils.readFromResource(filePath); - // 正则替换banner, 除去底部的广告信息 - text = text.replaceAll("
", ""); - text = text.replaceAll("powered.*?shrek.wang", ""); - text = text.replaceAll("", ""); - text = text.replaceAll("", ""); - response.getWriter().write(text); - } - - @Override - public void destroy() { - } - }; - FilterRegistrationBean registrationBean = new FilterRegistrationBean(); - registrationBean.setFilter(filter); - registrationBean.addUrlPatterns(commonJsPattern); - return registrationBean; - } - -}