com.pongsky.kit.config.DruidConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of config-core Show documentation
Show all versions of config-core Show documentation
Java 增强套件 之 config-core 模块
package com.pongsky.kit.config;
import com.alibaba.druid.spring.boot.autoconfigure.DruidDataSourceAutoConfigure;
import com.alibaba.druid.util.Utils;
import org.springframework.boot.autoconfigure.AutoConfigureAfter;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import javax.servlet.Filter;
import java.io.IOException;
/**
* druid 配置
*
* @author pengsenhao
*/
@Configuration
@ConditionalOnWebApplication
@AutoConfigureAfter(DruidDataSourceAutoConfigure.class)
@ConditionalOnClass(DruidDataSourceAutoConfigure.class)
@ConditionalOnProperty(name = "spring.datasource.druid.stat-view-servlet.enabled", havingValue = "true", matchIfMissing = true)
public class DruidConfig {
/**
* 去除 druid 监控底部广告
*
* @return 拦截器
* @throws IOException IOException
* @author pengsenhao
*/
@Bean
public FilterRegistrationBean removeDruidAdvertiseFilter() throws IOException {
String text = Utils.readFromResource("support/http/resources/js/common.js");
text = text.replaceAll("this.buildFooter\\(\\);", "");
FilterRegistrationBean registration = new FilterRegistrationBean<>();
registration.addUrlPatterns("/druid/js/common.js");
String finalText = text;
registration.setFilter((servletRequest, servletResponse, filterChain) -> {
servletResponse.resetBuffer();
servletResponse.setContentType("text/javascript;charset=utf-8");
servletResponse.getWriter().write(finalText);
});
return registration;
}
}